CodeForces - 1338A

Powered Addition

题意:给一个序列,从时刻 x = 1 x = 1 x=1 开始,每个时刻 x,我们可以从序列中选择一个子序列(不必须连续),该子序列中的每个值都加上 2 x − 1 2^{x-1} 2x1 ,当然子序列可以为空。我们需要得到一个升序的序列,问需要最少的时间是多少?

思路:直接贪心使得每个 a i a_i ai 都等于序列 [ a 1 , a i ] [a_1, a_i] [a1,ai] 中的最大值。我们可以得到和原序列的差值,我们找到最大的那个差值 d i f dif dif,只要 2 x − 1 > = d i f 2^{x-1}>=dif 2x1>=dif,那么 x x x 就是我们要找的答案。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int maxN = 100005;

int n, a[maxN], dif[maxN];

int main()
{
    
    
    int t; cin >> t;
    while(t -- )
    {
    
    
        cin >> n;
        int mx = -INF;
        for(int i = 1; i <= n; ++ i ) {
    
    
            cin >> a[i];
            mx = max(a[i], mx);
            dif[i] = mx - a[i];
        }
        mx = 0;
        for(int i = 1; i <= n; ++ i) {
    
    
            mx = max(dif[i], mx);
        }
        int ans = 0;
        while(mx)
        {
    
    
            ++ ans;
            mx >>= 1;
        }
        cout << ans << endl;
    }
    return 0;
}

OS:怎么就错了呢?读错题啊!以为子序列必须连续的,写写写…搞半天不需要连续。不愧是我,之前还创下了读题读半个多小时的记录还没读懂,还问别个题意。英语蒟蒻认证555

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/105744515