Codeforces 1419C. Killjoy

A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).

Killjoy’s account is already infected and has a rating equal to 𝑥. Its rating is constant. There are 𝑛 accounts except hers, numbered from 1 to 𝑛. The 𝑖-th account’s initial rating is 𝑎𝑖. Any infected account (initially the only infected account is Killjoy’s) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.

Contests are regularly held on Codeforces. In each contest, any of these 𝑛 accounts (including infected ones) can participate. Killjoy can’t participate. After each contest ratings are changed this way: each participant’s rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.

Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.

It can be proven that all accounts can be infected in some finite number of contests.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤100) — the number of test cases. The next 2𝑡 lines contain the descriptions of all test cases.

The first line of each test case contains two integers 𝑛 and 𝑥 (2≤𝑛≤103, −4000≤𝑥≤4000) — the number of accounts on Codeforces and the rating of Killjoy’s account.

The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (−4000≤𝑎𝑖≤4000) — the ratings of other accounts.

Output
For each test case output the minimal number of contests needed to infect all accounts.

Example
inputCopy
3
2 69
68 70
6 4
4 4 4 4 4 4
9 38
-21 83 50 -59 -77 15 -71 -78 20
outputCopy
1
0
2
Note
In the first test case it’s possible to make all ratings equal to 69. First account’s rating will increase by 1, and second account’s rating will decrease by 1, so the sum of all changes will be equal to zero.

In the second test case all accounts will be instantly infected, because all ratings (including Killjoy’s account’s rating) are equal to 4.

题意:
一个序列,初始数字x被感染了。
当一个数和某个被感染数相同时,他就被感染了。
你可以选择序列的几个数,进行变换,要求数的和不变,求最少几次变换使得序列所有数被感染。

思路:
分类讨论!
如果初始所有数都是x,那就是0次。
如果初始数的和为n*x,那么直接一次全部变成x,1次。
如果初始有x,那么初始变换的时候将剩下不是x的数全部变成x,原本是x的数随便变,1次。
否则就要一开始将n-1个数变成x,然后将剩下一个数变成x,2次。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <unordered_map>
#include <bitset>

using namespace std;

typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 998244353;

int a[maxn];
int main() {
    int T;scanf("%d",&T);
    while(T--) {
        ll n,x;scanf("%lld%lld",&n,&x);
        ll sum = 0;
        int cnt = 0;
        for(int i = 1;i <= n;i++) {
            ll num;scanf("%lld",&num);
            sum += num;
            if(num == x) {
                cnt++;
            }
        }
        
        if(cnt == n) {
            printf("0\n");
        } else if(n * x == sum) {
            printf("1\n");
        } else if(cnt) {
            printf("1\n");
        } else {
            printf("2\n");
        }
    }
    return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值