【Codeforces 1419 C】Killjoy,构造方案,结论题,贪心

problem

C. Killjoy
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
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.

solution

/*
题意:
+ 给出n个人的评级,和初始感染x。如果与被感染的人评级一样则被感染。
+ 每场比赛可以有1-n个人参加并修改评级,保证评级更改总和为0
+ 求最少几次比赛可以让所有人被感染
思路:
+ 可以构造这样的方案:最多两次肯定可以感染所有人,第一次n-1个人改为x,第n个人承担全部差值,然后第二次把它改成x,剩余的均摊。如果第n个人承担差值后为x,一场就行。
+ 或者原数组中已经有x(可以构造让他来承担全部差值,其他人改为x),则一场比赛就行。如果全为x就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;
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页