CF1267E Elections 题解

CF1267E Elections 题解

题目描述

n ( n ≤ 100 ) n(n \le 100) n(n100) 个人, m ( m ≤ 100 ) m(m \le 100) m(m100)个投票站,每个投票站对每个人都有一定的票数 ( ≤ 1000 ) (\le 1000) (1000).
n n n 个是坏蛋,请问去除掉哪几个投票站才能使他的票数不大于其他每个人中的一个,也就是不能让第 n n n 个人的总票数最多。

输入格式

第一行是两个整数,分别是 n n n m m m
接下来 m m m n n n 列,代表每个投票站对这 n n n 个人的票数。

输出格式

第一行一个整数 k k k,代表需要去掉几个投票站。
第二行有 k k k 个数,代表投票站的号数,不要求字典序.

题目描述

Byteburg Senate elections are coming. Usually “United Byteland”, the ruling Byteland party, takes all the seats in the Senate to ensure stability and sustainable development. But this year there is one opposition candidate in one of the constituencies. Even one opposition member can disturb the stability in the Senate, so the head of the Party asks you to ensure that the opposition candidate will not be elected.

There are n n n candidates, numbered from 1 1 1 to n n n . Candidate n n n is the opposition candidate. There are m m m polling stations in the constituency, numbered from 1 1 1 to m m m . You know the number of votes cast for each candidate at each polling station. The only thing you can do to prevent the election of the opposition candidate is to cancel the election results at some polling stations. The opposition candidate will be elected if the sum of the votes cast in their favor at all non-canceled stations will be strictly greater than the analogous sum for every other candidate.

Your task is to prevent the election of the opposition candidate by canceling the election results at the minimal possible number of polling stations. Notice that solution always exists, because if you cancel the elections at all polling stations, the number of votes for each candidate will be 0 0 0, and the opposition candidate will not be elected.

输入格式

The first line of the input contains two integers n n n and m m m ( 2 ≤ n ≤ 100 2\le n\le 100 2n100 ; 1 ≤ m ≤ 100 1\le m \le 100 1m100 ) — the number of candidates and the number of polling stations. The next m m m lines contain the election results at each polling station with n n n numbers on each line. In the i i i -th line the j j j -th number is a i , j a_{i,j} ai,j — the number of votes cast for the candidate j$ at the station i i i ( 0 ≤ a i , j ≤ 1   000 0\le a_{i,j} \le 1\,000 0ai,j1000 ).

输出格式

In the first line output integer k k k — the minimal number of the polling stations in which you need to cancel the election results. In the second line output k k k integers — the indices of canceled polling stations, in any order. If there are multiple ways to cancel results at k k k stations, output any one of them.

样例 #1

样例输入 #1

5 3
6 3 4 2 8
3 7 5 6 7
5 2 4 7 9

样例输出 #1

2
3 1

样例 #2

样例输入 #2

2 1
1 1

样例输出 #2

0

样例 #3

样例输入 #3

3 3
2 3 8
4 2 9
3 1 7

样例输出 #3

3
1 2 3

提示

In the first example, the candidates from 1 1 1 to 5 5 5 received 14 , 12 , 13 , 15 14, 12, 13, 15 14,12,13,15 and 24 24 24 votes correspondingly. The opposition candidate has the most votes. However, if you cancel the election results at the first and the third polling stations, then only the result from the second polling station remains and the vote sums become 3 , 7 , 5 , 6 3, 7, 5, 6 3,7,5,6 and 7 7 7, without the opposition candidate being in the lead anymore.

算法:贪心

先枚举第 x x x 位为最终票数超过反叛者的人,然后计算反叛者与第 x x x 位在每轮投票中票数之差,再从大到小排序。之后贪心地取差的最大值,将第 x x x 位与反叛者票数的差距逐渐缩小,更新答案即可。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=110;
ll n,m,a[N][N],ans=LONG_LONG_MAX,t,p2[N],ct,p[N];
struct Vote{
	ll v,id;
}a2[N];
bool cmp(Vote l,Vote r){
	return l.v>r.v;//按差从大到小排序 
}
ll solve(ll x){
	t=ct=0;
	for(ll i=1;i<=m;i++) a2[i].v=a[i][n]-a[i][x],t+=a2[i].v,a2[i].id=i;
	//数组a2存的是反叛者与第x个人每轮中的票数之差
	//id用来存放投票站编号 
	sort(a2+1,a2+m+1,cmp);//排序 
	while(t>0) t-=a2[++ct].v,p2[ct]=a2[ct].id;//贪心取差的最大值 
	return ct;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++) cin>>a[i][j];
	for(int i=1;i<n;i++){//枚举哪个人票数超过反叛者 
		t=solve(i);
		if(t<ans){
			ans=t;
			for(int j=1;j<=ans;j++) p[j]=p2[j];
			//更新答案 
		}
	}		
	cout<<ans<<"\n";
	for(int i=1;i<=ans;i++) cout<<p[i]<<" ";
	return 0;
}

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值