CodeForces - 1339C Powered Addition(思维+贪心)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1339C Powered Addition(思维+贪心)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個由 n 個數組成的數列 a,選擇一個最小的 k ,代表可以進行 k 次操作,對于第 t 次操作可以選擇任意個位置使得 a[ i ] = a[ i ] + 2^( t?- 1 ),問最少需要多少次操作才能使序列滿足非嚴格遞增
題目分析:讀完題后的第一反應是模擬+貪心,實際上不是的,需要想到的一個知識點是,任何數字都可以用二進制來表示,相應的題目中的操作可以轉換為二進制加法,進而轉換為:對于某個位置,都可以加上任何一個數
因為需要讓整個序列非嚴格遞增,所以取任意兩個位置 i 和 j 滿足 i < j 討論一下:
這樣一來我們只需要維護一下前綴的最大值,對于后續的每個 a[ i ] ,找出最大的差值就好了
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--){int n;scanf("%d",&n);int mmax=-inf,dif=-inf;for(int i=1;i<=n;i++){int num;scanf("%d",&num);mmax=max(mmax,num);dif=max(dif,mmax-num);}int ans=0;while((1LL<<ans)-1<dif)ans++;printf("%d\n",ans);}return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1339C Powered Addition(思维+贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1334D M
- 下一篇: CodeForces - 1339D E