跳到正文

题解

题解:CF2232B Cake Leveling

Segment_Fenwick_Tree约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年5月31日
收录
2026年6月1日
更新
2026年11月13日
篇幅
约 3 分钟
题目
CF2232B

版本

  1. v1当前

签到题。

对于每个 1≤i≤n1 \le i \le n,设上一轮的答案为 rr,当前面的推平时,这里的高度首先会变成 ai+ai−1−ra_i + a_{i-1} - r,之后当 rr 变成 r−kr - k 时,ai+ai−1−ra_i + a_{i-1} - r 会增加 k×(i−1)k \times (i - 1),很明显我们要让 kk 为最大的满足 ai+ai−1−r+k×(i−1)≥r−ka_i + a_{i-1} - r + k \times (i-1) \ge r - k,直接二分即可,当然也可以推式子线性做。

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,ll>
#define dgd priority_queue<int>
#define xgd priority_queue<int,vector<int>,greater<int> >
#define fi first
#define se second
#define pbk push_back
#define all(_c) _c.begin(),_c,end()
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define nl cout << "\n"
const int INF=1e9;
const ll Inf=4e18;
const int abc=26;
const double pi=3.1415926;
int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}
    return x*f;
}
ll n,r,a[200010];
void solve(){
	cin >> n;
	rep(i,1,n) cin >> a[i];
	r=a[1];
	cout << r << " ";
	rep(i,2,n){
		a[i]+=(a[i-1]-r);
		a[i-1]=r;
		if(a[i]<r){
			ll left=1,right=r,ans;
			while(left<=right){
				ll mid=(left+right)>>1;
				if(a[i]+mid*(i-1)>=r-mid){
					right=mid-1;
					ans=mid;
				}else{
					left=mid+1;
				}
			}
			r-=ans;
			a[i]+=ans*(i-1);
		}
		cout << r << " "; 
	}
    nl;
    return;
}
int main(){
    ios::sync_with_stdio(false);
    cout.tie(0),cin.tie(0);
    int t=1;
    cin >> t;
    while(t--) solve();
    return 0;
}
原文