“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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值