题解归档 - cf2229A

题解归档 - cf2229A

本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。

思路

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  ~  ~


 赏 
感谢您的支持,我会继续努力哒!
支付宝收款码
tips
文章二维码 分类标签:归档TypechoAutoUpload
文章标题:题解归档 - cf2229A
文章链接:https://www.fangshaonian.cn/archives/262/
最后编辑:2026 年 6 月 28 日 19:05 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
(*) 4 + 3 =
快来做第一个评论的人吧~