CodeForces - 1419C Killjoy

题目链接

https://codeforces.com/problemset/problem/1419/C

解题思路

总共三种情况0,1,2
分情况讨论一下。
1.全都是x时,0
2.存在x或全部之和为n*x时,1
3.其他情况,2

代码

// 废物代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+10;

int T, a[N], f, sum, n, x, ff;

int main()
{
    cin>>T;
    while(T--) {
        f = 0;
        ff = 0;
        sum = 0;
        cin>>n>>x;
        for(int i = 1;i <= n;i ++) cin >> a[i], sum += x-a[i];
        for(int i = 1;i <= n;i ++) {
            if(a[i] != a[1]) {
                f = 1;
            }
            if(a[i] == x) {
                ff = 1;
            }
        }


        if(!sum) {
            if(f) cout << 1 << endl;
            else cout << 0 << endl;
        } else {
            if(ff) cout << 1 << endl;
            else cout << 2 << endl;
        }

    } 

    return 0;
}

/*
// 大佬代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+10;
int a[maxn];
int main(){
    ios::sync_with_stdio(false);
    int T;  cin>>T;
    while(T--){
        int n, x;  cin>>n>>x;
        int cnt = 0, sum = 0;
        for(int i = 1; i <= n; i++){
            cin>>a[i];  if(a[i]==x)cnt++;
            sum += a[i];
        }
        if(cnt==n)cout<<0<<"\n";
        else if(cnt>=1 || sum==n*x)cout<<1<<"\n";
        else cout<<2<<"\n";
    }
    return 0;
}
*/

总结

少考虑了存在x的情况了。

思维 文章被收录于专栏

思维题都会了,ACM金牌就稳了! 我骗你的!

全部评论

相关推荐

07-03 11:02
中山大学 C++
字节刚oc,但距离九月秋招很近了有两段互联网实习,非腾讯字节。不敢赌转正,现在在纠结去还是不去如果实习俩月离职会有什么后果吗
阿城我会做到的:不去后悔一辈子,能否转正取决于ld的态度,只要他不卡,答辩就是走流程,个人觉得可以冲一把
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务