Codeforces 1339C - Powered Addition


Obviously a simple question, but because the initial value of mx is not set to negative infinity, it took an hour to write a 0 directly, and there is no time to write the following


Title

1339C




Title

Given an array of length n

In the kth second, you can add 2 k-1 to any element of the array , and you can choose it or not.

Ask at least how many seconds it takes to make this array into a non-decreasing sequence




Problem-solving ideas

Because only positive values ​​can be added to the elements

Therefore, the condition that a certain position x must be changed is that there is a value between 1 ~ x-1 that is greater than the value of the position x, resulting in a decrease

Then the value of position x must be changed to a number greater than or equal to the previous maximum

If you look at this problem from a binary perspective, you can find

If the number of a position must be at least d to be the same as the previous maximum value

After converting d to binary, the number of digits in the highest bit of d is the total time required to complete this step

The 0 and 1 from low to high indicate whether it needs to be changed in that second.

Therefore, the optimal solution is to find all the reverse-order pairs, and the number of digits in the highest difference of the reverse-order pairs is the answer.




program

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;

void solve()
{
    int n,ans=0,ar,mx=-INF,mxd=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ar;
        mx=max(mx,ar);
        mxd=max(mxd,mx-ar);
    }
    while(mxd)
    {
        mxd>>=1;
        ans++;
    }
    cout<<ans<<'\n';
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;cin>>T;
    for(int t=1;t<=T;t++)
        solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/stelayuri/p/12689098.html