7-9 选举干预

Given an election with voting districts and majority rule, how many voters have to be bribed such that our party wins a majority of districts?
在有选区和多数决定原则的选举中,要贿赂多少选民才能使我们的政党赢得选区的多数?
Solution:For each district simulate how many votes are needed to achieve majority by taking one vote at a time from the currently highest voted party and adding it to our total.Greedily take the districts that need the fewest votes until our party has won the majority of districts. Can be sped up by taking enough votes at a time from the currently highest voted parties until they are equal to the next highest party. Repeat until our party has the majority of votes.
解决方案:对于每个选区,模拟需要多少张选票才能获得多数,每次从目前投票最多的政党中选出一票,然后把它加到我们的总数中。在我们的政党赢得大多数选区之前,贪婪地选择那些需要最少选票的选区。可以通过一次从当前票数最高的政党获得足够的选票,直到他们与第二高的政党旗鼓相当,从而加快投票速度。重复,直到我们党获得多数选票为止。

输入格式:

The input consists of: 输入包括:
• One line with two integers w and p ( 2 ≤ w,p ≤ 1000 ), the number of districts and the number of parties running in the election. The parties are numbered 1 to p and the ICPC is party 1.
•一行,两个整数w和p(2≤w,p≤1000),选区数和参选党派数。双方编号从1到p, ICPC为第一方。
• w lines, each with p integers v 1 ,…,v p ( 0 ≤ v i ≤ 1000 for each i ) giving the projected results for a district. vi denotes the number of votes that will be cast for party i.
• w行,每一行有p个整数v1,…, vp(c0≤vi≤1000)给出一个区域的投影结果。vi表示将投给一党的票数。
It is guaranteed that there is at least one voter in each district, i.e. the sum of all v i per district will always be at least one.
保证每个地区至少有一个选民,即每个地区的所有vi之和至少为一个。

输出格式:

Output the minimum number of voters that have to be bribed in order for the ICPC to win a majority of the seats in the council.
输出必须贿赂的最低选民人数,以便ICPC在委员会中赢得多数席位。

输入样例1:

3 3
0 2 2
1 2 3
0 2 3

输出样例1:

4

输入样例2:

2 4
0 1 2 9
1 0 0 0

输出样例2:

5

输入样例3:

3 3
1 0 0
0 1000 0
0 9 5

输出样例3:

6

思路:模拟即可,特别注意,当1000个席位,你拥有0而其他999个单位拥有999个人,你只需向每个单位拉拢一人即可

AC代码:
#include<bits/stdc++.h>
using namespace std;
int b[1010];
int main()
{
    
    
 	int w,p;
 	long long sum=0;
 	scanf("%d %d",&w,&p);
 	for(int i=1;i<=w;i++)
 	{
    
    
 		int a[1010];
  		for(int j=1;j<=p;j++)
   			scanf("%d",&a[j]);
  		sort(a+2,a+p+1);
  		int t=0;
  		while(a[1]<=a[p])
  		{
    
    
   			a[1]++,a[p]--,t++;
      			for(int j=p;j>=2;j--)//mark
				if(a[j]>a[p])
          			{
    
    
              				int temp=a[j];
              				a[j]=a[p];
              				a[p]=temp;
              				break;
          			}
		}
		b[i]=t;
	}
	sort(b+1,b+w+1);
	int v=w/2+1;
	for(int i=1;i<=v;i++)
  		sum+=b[i];
 	printf("%lld",sum);
}

猜你喜欢

转载自blog.csdn.net/weixin_45989486/article/details/109369265