Codeforces Round #392(div 2) 758D (贪心)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #392(div 2) 758D (贪心)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
orz 最近被水題卡+FST,各種掉rating
題目大意
一個數s它是n進制的,但是每一位不是用'A','B'....表示的,而是用10,11等等表示的,將它還原成十進制
這種表示方法顯然會產生多解,然后求所有的解中最小的那個
?
這題一想就是貪心,但是一開始算法寫渣了,改變思路以后才A的
簡單來說就是從右邊開始,把盡量多的數壓到一位里面,這樣會使數更小
壓的思路可以這么考慮
每次新加進來一個數,如果加進來以后,已經大于原來的那個數,那么就找截取一段可行的最大數
這樣考慮的目的是處理前導0帶來的影響
0也有可能是單獨的一位,也有可能與其他數壓到同一位里
想清楚這個以后就不難寫了
?
#include <iostream> #include <cmath> #include <cstring> using namespace std; typedef long long ll; string str, n; ll last, w; bool compare(int x, int y) {if(y-x > n.length()) return true;if(y-x < n.length()) return false;for(int i = x; i < y; i++){if(str[i] == n[i-x]) continue;if(str[i] > n[i-x]) return true;else return false;}return true; } ll cal(string str, int x, int y) {ll ans = 0;for(int i = x; i < y; i++)ans = ans*10 + str[i] - '0';return ans; } int main() {cin>>n;cin>>str;ll ans = 0, k = 1;last = str.length();for(int i = str.length()-1; i >= 0; i--){while(compare(i, last)){int j;for(j = i; j < last; j++){if(str[j] == '0') continue;if(compare(j, last)) continue;j++; break;}j--;ans += cal(str, j, last)*k;last = j;k *= cal(n, 0, n.length());}}if(last != 0) ans += cal(str, 0, last)*k;cout<<ans<<endl; }?
轉載于:https://www.cnblogs.com/Saurus/p/6322598.html
總結
以上是生活随笔為你收集整理的Codeforces Round #392(div 2) 758D (贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nodejs学习事件模块
- 下一篇: urllib2设置代理