“Shopee杯” 武漢大學(正式賽)A - A Simple Problem about Election

“Shopee杯” 武漢大學(正式賽)A - A Simple Problem about Election

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes

Problem Statement

ZZZZSGW is a cute hamster living in a beautiful city named WWW. However, the COVID-19 spread like a wild fire in his city, many people infected. With great grief and motivation, he really wants to do something for his hometown. Therefore he applied for the volunteer of his community to help his neighbors.
There are n candidates and the voluntary team only needs several volunteers in total. To ensure the quality of the team, they decided to hold an election to pick out the volunteers. Every resident has to nominate exactly m candidates and mustn’t give more than one nomination to one candidate. (of course one can save one of the nominations for himself/herself). The candidates then will be ranked by the nominations they get in descending order, if there are candidates have the same number of nominations, they will be ranked by their names in lexicographically increasing order.
It’s evident that the name ZZZZSGW won’t take much chance, so when he has the same nominations with others, you can just suppose that he will always be the last! What’s worse, the order to nominate is also according to the names, which means that this poor guy will also be the last one to make his decision among the residents!
However, as each coin has two sides, the last to make decision also means the one who knows everything ------ since everyone can see the current result when making nominations! Now it’s ZZZZSGW’s turn, he knows the current result and the final result only depends on his own decision. But he is too nervous to make a clear judgment! Can you help him to get the highest place under this situation?

Input

The input consists of several test cases. The first line of the input is an integer T, the number of the test
cases.
For each test case, the first line will be two integers n, m. The number of candidates and the number of
nominations that each resident can make.
The next line will be n integers {a1, a2, …, an} separated by spaces, which are the current number
nominations of each candidate. The first integer always refers to the nominations of ZZZZSGW.
For each test case, 0 ≤ ai ≤ 109, 1 ≤ m ≤ n ≤ 105. And there is ∑n ≤ 2 ∗ 105 for all test cases.

Output

For each test case, output a single integer ans in a line, which is the best place ZZZZSGW can get after his turn.

Sample Input #1

2
5 3
5 1 2 6 7
5 3
5 1 2 5 7

Sample Output #1

3
2

Note

There are more hidden rules in the election than you think, so don’t be surprised that the sum of
nominations can’t be divided by m.

寫在前面

(難道不應該叫前言嘛)
咳咳,其實說來,這篇博客並沒有任何意義,按理來說應該要寫一寫補題的題解更好,但補題通道還未開放,所以就…
這篇博客只是我的倔強罷了,雖然名次很難看,沒過幾道題,但我還是希望這一刻能被銘記。
(Ever been here)
じゃあ、行くぞ

題意

啊,萬惡的英語閱讀理解(這題面還算短的了)
其實說白了,就是:

正在選舉,有 n 名候選人,你是其中之一,你是最後一個投票的,此時可以看到每個候選人目前的票數,如果票數相同,按名字排序(你的名字在同票裏是最後一個),你要投恰好 m 票,不能給一個人投多票,可以給自己投票,你要如何投票,才能令自己最後的名次最高,輸出名次。

(看這中文多簡潔,三行完事)

分析

首先,肯定要給自己投一票,但是剩下的票不能不投,此時我們只能想辦法儘量穩住局面
爲了方便敘述,我們將自己的原票數設爲 S

能保持名次不動最好:
做法是,給 票數<S票數>S 的人投票,這樣一來自己的相對排名不會發生變化
(因爲自己保有一票)

退而求其次,若票數還富餘,那隻能投給 票數==S的人:
此時名次必然後退,且無法避免,毫無策略,排名+=富餘票數
因爲自己總是同票的最後一名(這名字誰取的

接下來,只要分別求出 票數<S票數>S票數==S 的人數即可

"Talk is Cheap. Show me the Code."

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
int a[maxn];
int main(void)
{
	int T;
	scanf("%d", &T);
	while (T--) {
		int n, m;
		scanf("%d %d", &n, &m);
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		int Me = a[0];//ZZZZSGW原票數
		sort(a, a + n);
		int low = 0;//票數<Me的人數
		while (a[low] != Me)low++;
		int same = 0;//票數==Me的人數
		for (int i = low; i < n; same++,i++)
			if (a[i] != Me)
				break;
		int up = n - low - same;//票數>Me的人數
		int ans = 0;
		if (up + low + 1 >= m)//票數無富餘
			ans = up + 1;
		else {//票數富餘
			m -= up + low + 1;
			ans = up + 1 + m;
		}
		printf("%d\n", ans);
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章