Codeforces Round #633 (Div. 1) A. Powered Addition(贪心)

https://codeforces.com/contest/1338/problem/A

题目大意:

给定一个长度为n的数组a,我们每次可以选择连续的一段区间然后加上2的x-1次方(也就是1 2 4 8 16......)

问我们能够把数组a凑成非递减的数列的最小操作是多少?
input 
3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4
output 
2
0
3

一开始我想的是既然要递增吗,那就全部变成和前面差距最大的那个数字,其实这个观点是错误的:hack数据:1 3 -1 -2 6 -1 -2.

  • 其实我们应该每次都和前面一个非下降的数字平齐,这样才能保证整个数组的非递减性。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=500200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        for(LL i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        LL last=a[1];
        LL maxn=0;
        for(LL i=2;i<=n;i++)
        {
            if(a[i]>=last) last=a[i];
            else if(a[i]<last) maxn=max(maxn,last-a[i]);
        }
        if(maxn==0) cout<<"0"<<endl;
        else
        {
            LL ans=0;
            while(maxn>0)
            {
                maxn/=2;
                ans++;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
posted @ 2022-10-20 20:50  高尔赛凡尔娟  阅读(25)  评论(0编辑  收藏  举报