Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
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?
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.
For each test case, output a single integer ans in a line, which is the best place ZZZZSGW can get after his turn.
2
5 3
5 1 2 6 7
5 3
5 1 2 5 7
3
2
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 的人数即可
"Talk is Cheap. Show me the Code."
#include
#include
#include
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;//票数
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;
}