Codeforces Round #633 (Div. 2) C. Powered Addition

Title:
Give a sequence a a can be i i seconds tomultiple elements atanyposition 2 x 1 2^{x-1} , ask the handle a a What is the minimum number of seconds for become anon-decreasingsequence?
Ideas
Since the minimum time to turn the sequence into a non-decreasing sequence, it must bethe time requiredto complement the two numbers that are in reverse order pairs and have the largest difference to the non-decreasing state(because you can choose any number, so When the reverse order of the maximum difference is complemented, everything else can be done in this process).

If you decide the time, just put i i The summable sum of seconds can be compared with the difference. When the summable sum is greater than or equal to the difference for the first time, it is the answer.

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

const int N = 1e5+7;
int n,m,cas;
ll b[N],a[100],s[100];
//求 i时刻 的可加数和
void init(){
	a[1] = 1;
	s[1] = 1;
	for(int i = 2; i <= 32; ++i){
		a[i] = a[i-1]*2; 
		s[i] = a[i]+s[i-1];
	}
}
int main(){
	init();
	scanf("%d",&cas); 
	while(cas--){
		int ans = 0;
		scanf("%d",&n);
		for(int i = 1;i <= n; ++i)
			scanf("%lld",b+i);
		ll maxx = b[1],sub=0;
		for(int i = 2; i <= n; ++i){
			maxx = max(maxx,b[i]);//maxx为b[1]~b[i]的最大值
			sub = max(sub,maxx-b[i]);//sub为b[1]~b[i]的最大差值
			for(int j = 0; j <= 33; ++j)
				if(sub <= s[j]){
					ans = max(ans,j);//找到b[1]~b[i]中补齐最大差值所需的时间,并取一个最大值
					break;
				}
		}
		printf("%d\n",ans);
	}
	return 0;
}
Published 141 original articles · praised 71 · 60,000+ views

Guess you like

Origin blog.csdn.net/sinat_40872274/article/details/105485726
Recommended