Codeforces Round #671 (Div. 2) C. 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 x. Its rating is constant. There are n accounts except hers, numbered from 1 to n. The i-th account's initial rating is ai. 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 n 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 t (1≤t≤100) — the number of test cases. The next 2t lines contain the descriptions of all test cases.

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

The second line of each test case contains n integers a1,a2,…,an (−4000≤ai≤4000) — the ratings of other accounts.

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

Example
input

3
2 69
68 70
6 4
4 4 4 4 4 4
9 38
-21 83 50 -59 -77 15 -71 -78 20

output

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.


题目大意

给定一个n和x,分别代表n个参加比赛的人数和初始感染者的评分,然后感染规则是:未被感染者的评分与感染者评分一样就会被感染,一个人被感染后就永远处于感染状态。在每一场比赛中参赛者的人数可以随意改变,但总的改变数的代数和必须为0(就是所有人的总和不变),现在要求最少比赛场数使得所有人被感染。


思路

一开始我做的时候一直想着去转化为初始感染者的评分,导致我一直弄不出来。其实在初始感染者感染一些人后可以通过这些人评分的变化再去感染另外的人,那么这道题就可以讨论了:
1.如果所有人的初值都等于x,那么就不用比赛了,因为所有人已经被感染;
2.如果所有人总和的平均值等于x,那么只需要1场比赛就可以让所有人的评分都变为x;
3.如果参赛者中有的初始值等于x,那么这些人在比赛开始前就全部被感染,然后根据被感染者状态不变这个性质,我们可以把剩下的所有未感染者的评分全部变为x,然后这些人的改变数总和全部算在第一批被感染者上面,这样仍然只需1场比赛就可以将所有人全部感染;
4.如果一开始所有参赛者的评分都不等于x,那么我们可以先进行1场比赛,至少可以感染1个,然后根据上面情况3可知再进行一场比赛就可以达到目的,所以总共需要2场比赛


代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1010];
int main(){
	int t,n;
	ll x;
	scanf("%d",&t);
	while (t--){
		scanf("%d %lld",&n,&x);
		int flag=1,flag2=0;
		ll pos=0,neg=0,sum=0;
		for (int i=0;i<n;i++){
			scanf("%lld",&a[i]);
			if (a[i]!=x)   flag=0;
			if (a[i]==x)   flag2=1;
			sum+=a[i];
		}
		if (flag)   printf("0\n");
		else if (x*n==sum||flag2)   printf("1\n");
		else   printf("2\n");
	}
	return 0;
}
上一篇:Codeforces Round #671 (Div. 2) C. Killjoy


下一篇:JdbcTemplate经典案例