题解【CF1338A Powered Addition】

\[\texttt{Description} \]

Give a sequence \ (a \) of length \ (n \) .

For the second \ (i \) second, we can choose the number at several positions plus \ (2 ^ {i-1} \) .

Ask at least a few seconds, you can make \ (a_1 \ leq a_2 \ leq ... \ leq a_n \) .

\[\texttt{Solution} \]

  • It is not difficult to notice first that for the first \ (i \) seconds, we can add \ (x \ in [1,2 ^ i-1] \) to a number .

  • In other words, for the first \ (i \) seconds, we can make a number \ (x \) into any number in \ ([x, x + 2 ^ i-1] \) .

  • Consider this equation \ (A_1 \ Leq A_2 \ Leq ... \ Leq A_N \) , obviously, \ (a_i \) is smaller, \ (a_ {i + 1} \) to \ (A_N \) more There is "operation space".

  • And because the operation will only cause the number to become larger, we obviously don't want to move this \ (a_1 \) .

  • Then consider \ (a_2 \) :

    • If \ (a_1 \ leq a_2 \) , then the requirements of the problem have been met, in order to leave "operation space" for the following numbers, so don't move it.
    • If \ (a_1> a_2 \) , then the minimum dichotomy \ (x \) is satisfied \ (a_1 \ leq a_2 + 2 ^ x-1 \) , use \ (x \) to update the answer, and then make \ (a_2 = a_1 \) .
  • By analogy, we then consider \ (a_3, a_4, ..., a_n \) , assuming that we now consider the \ (i \) position:

    • If \ (a_ {i-1} \ leq a_i \) , then the requirements of the problem have been met, in order to leave "operation space" for the following numbers, so don't move it.
    • If \ (a_ {i-1}> a_i \) , then the minimum dichotomy \ (x \) is satisfied \ (a_ {i-1} \ leq a_i + 2 ^ x-1 \) , use \ (x \) Update the answer, and then make \ (a_i = a_ {i-1} \) .
  • In this way , the answer can be found after considering the \ (n \) bits, \ (\ mathcal {O (n \ log \ log size)} \) , where \ (size \) represents the size of the range.

\[\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} \]

Guess you like

Origin www.cnblogs.com/cjtcalc/p/12688959.html