题解【CF1338A Powered Addition】

时间:2020-04-13 10:03:28   收藏:0   阅读:109

\[\texttt{Description} \]

给一个长度为 \(n\) 的序列 \(a\)

对于第 \(i\) 秒,我们可以选择若干个位置上的数加上 \(2^{i-1}\)

问最少在第几秒的时候,可以使得 \(a_1 \leq a_2\leq...\leq a_n\)

\[\texttt{Solution} \]

\[\texttt{Code} \]

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

namespace IO {
    static char buf[1 << 20], *fs, *ft;
    inline char gc() {
        if (fs == ft) {
			ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin);
			if (fs == ft) return EOF;
        }
        return *fs ++;
    }
    #define gc() getchar()
	inline int read() {
		int x = 0, f = 1; char s = gc();
		while (s < ‘0‘ || s > ‘9‘) {if (s == ‘-‘) f = -f; s = gc();}
		while (s >= ‘0‘ && s <= ‘9‘) {x = x * 10 + s - ‘0‘; s = gc();}
		return x * f;
	}
} using IO :: read;

const int N = 200100; 

int n;

long long a[N];

void work() {

	n = read();

	for (int i = 1; i <= n; i ++)
		a[i] = read();

	int ans = 0;

	for (int i = 2; i <= n; i ++) {
		if (a[i - 1] <= a[i]) continue;

		int l = 1, r = 33;
		while (l < r) {
			int mid = (l + r) / 2;
			long long delta = (1ll << mid) - 1;

			if (a[i - 1] <= a[i] + delta) r = mid; else l = mid + 1; 
		}

		ans = max(ans, l);

		a[i] = a[i - 1]; 
	}

	printf("%d\n", ans);
}

int main() {

	int T = read();

	while (T --)    work();

	return 0;
}

\[\texttt{Thanks} \ \texttt{for} \ \texttt{watching} \]

原文:https://www.cnblogs.com/cjtcalc/p/12688959.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!