CF:Elections

Elections

题面翻译

有两个人小A和小B参加选举,共有n个人参与投票,每人可以投k票。小A通过暗箱操作知道了每个人一定会投ai票给小B。
他很想赢,所以决定通过操作来改变每个人可以投的票数k。问k最小为多少时,小A能获胜。

输入:第一行,包含一个整数n,投票的人数。
第二行,n个整数ai,ai为第i个人投给小B的票数。

输出:仅一行,包含一个整数k,每个人可投的票数。

题目描述

Awruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n n nstudents in the school. Each student has exactly k k kvotes and is obligated to use all of them. So Awruk knows that if a person gives a i a_i aivotes for Elodreip, than he will get exactly k − a i k - a_i kaivotes from this person. Of course 0 ≤ k − a i 0 \le k - a_i 0kaiholds.

Awruk knows that if he loses his life is over. He has been speaking a lot with his friends and now he knows a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an— how many votes for Elodreip each student wants to give. Now he wants to change the number k k kto win the elections. Of course he knows that bigger k k kmeans bigger chance that somebody may notice that he has changed something and then he will be disqualified.

So, Awruk knows a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an— how many votes each student will give to his opponent. Help him select the smallest winning number k k k. In order to win, Awruk needs to get strictly more votes than Elodreip.

输入格式

The first line contains integer n n n( 1 ≤ n ≤ 100 1 \le n \le 100 1n100) — the number of students in the school.

The second line contains n n nintegers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an( 1 ≤ a i ≤ 100 1 \leq a_i \leq 100 1ai100) — the number of votes each student gives to Elodreip.

输出格式

Output the smallest integer k k k( k ≥ max ⁡ a i k \ge \max a_i kmaxai) which gives Awruk the victory. In order to win, Awruk needs to get strictly more votes than Elodreip.

样例 #1

样例输入 #1

5
1 1 1 5 1

样例输出 #1

5

样例 #2

样例输入 #2

5
2 2 3 2 2

样例输出 #2

5

提示

In the first example, Elodreip gets 1 + 1 + 1 + 5 + 1 = 9 1 + 1 + 1 + 5 + 1 = 9 1+1+1+5+1=9votes. The smallest possible k k kis 5 5 5(it surely can’t be less due to the fourth person), and it leads to 4 + 4 + 4 + 0 + 4 = 16 4 + 4 + 4 + 0 + 4 = 16 4+4+4+0+4=16votes for Awruk, which is enough to win.

In the second example, Elodreip gets 11 11 11votes. If k = 4 k = 4 k=4, Awruk gets 9 9 9votes and loses to Elodreip.

错误思考:

一开始时我读题时没有仔细看样例就直接写代码,以为只要最小k时小A能赢过小B即可(以为如果多出票数ai-k直接不用管),所以我确定了k的边界是两倍ai中最大值+1,即2*max(ai)+1,以为k要从1开始遍历,所以测试代码时直接错了,然后再看了一下题才发现想法错了

错误思路下的代码:

#include <bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int i=0;i<n;i++){
		cin >> v[i];
	}
	sort(v.begin(),v.end());
	for(int i=1;i<=2*v[n-1]+1;i++){
		int a=0,b=0;
		for(int j=0;j<n;j++){
			if(v[j]>=i){
				b+=i;
			}else{
				b+=v[j];
				a+=i-v[j];
			}
		}
		if(a>b){
			cout << i;
			break;
		}
	}
}

正确思路:

票数k的初始值是受ai中最大值的影响的,因为能够投这么多票,所以k得从ai中的最大值开始遍历,边界同样是两倍ai中最大值+1,即2*max(ai)+1,改成这样就成功AC

正确代码:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int i=0;i<n;i++){
		cin >> v[i];
	}
	sort(v.begin(),v.end());
	for(int i=v[n-1];i<=2*v[n-1]+1;i++){
		int a=0,b=0;
		for(int j=0;j<n;j++){
			if(v[j]>=i){
				b+=i;
			}else{
				b+=v[j];
				a+=i-v[j];
			}
		}
		if(a>b){
			cout << i;
			break;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值