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

タイトル:
シーケンスを与える a a ことができます 任意の位置で複数の要素に 2 バツ 1 2 ^ {x-1} 、ハンドルを尋ねる a a 非減少シーケンスなるための最小秒数はいくつですか?
アイデア
シーケンスを非減少シーケンスに変える最小時間なので、逆順のペアであり、非減少状態との差が最も大きい2つの数値を補完するのに必要な時間でなければなりません(任意の数を選択できるため、最大差の逆順が補完されると、他のすべてがこのプロセスで実行できます)。

時間を決めたら入れて 合計と秒の合計を差と比較できます。合計の合計が初めて差以上である場合は、それが答えです。

#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;
}
元の記事を141件公開 71 件を賞賛 60,000回以上の閲覧

おすすめ

転載: blog.csdn.net/sinat_40872274/article/details/105485726