题解归档 - cf2229A
本文最后由方少年更新于2026 年 6 月 28 日,已超过0天没有更新。如果文章内容或图片资源失效,请留言反馈,将会及时处理,谢谢!
题解归档 - cf2229A
本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。
- 本地编号:
cf2229A - 本地来源:
problems/cf2229A/idea.md - 题目链接:https://codeforces.com/contest/2229/problem/A
- 原始标题:cf2229A
思路
cf2229A
pattern: diameter shrink.
claim: The answer is ceil((max(a)-min(a))/2).
necessary: In one operation, the leftmost slime can move right by at most 1 and the rightmost slime can move left by at most 1, so the diameter decreases by at most 2.
sufficient: Repeatedly choose any integer between the current extremes, for example the midpoint. Then both extremes move toward each other whenever they differ, so after ceil(diameter/2) operations all slimes can meet.
brute/check: Simulate repeatedly choosing the midpoint on small arrays.
edge: Already equal gives 0; odd diameter rounds up.
代码
来源:problems/cf2229A/solution.cpp
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ll t,n,i,x,mn,mx;
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
mn=1000000000;
mx=0;
for(i=1;i<=n;i++){
scanf("%lld",&x);
mn=min(mn,x);
mx=max(mx,x);
}
printf("%lld\n",(mx-mn+1)/2);
}
return 0;
}
~ ~ The End ~ ~
文章标题:题解归档 - cf2229A
文章链接:https://www.fangshaonian.cn/archives/262/
最后编辑:2026 年 6 月 28 日 19:05 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)