【模拟&优先队列】UPC Contest2593 - 2020年秋季组队训练赛第十六场 问题 E: Elections

问题 E: Elections

时间限制: 3 Sec 内存限制: 128 MB Special Judge

题目描述

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 candidates, numbered from 1 to n. Candidate n is the opposition candidate. There are m polling stations in the constituency, numbered from 1 to 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, and the opposition candidate will not be elected.

输入

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

输出

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

样例输入

【样例1】
5 3
6 3 4 2 8
3 7 5 6 7
5 2 4 7 9
【样例2】
2 1
1 1
【样例3】
3 3
2 3 8
4 2 9
3 1 7

样例输出

【样例1】
2
3 1
【样例2】
0
【样例3】
3
1 2 3

提示

In the first example, the candidates from 1 to 5 received 14, 12, 13, 15, and 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, and 7, without the opposition candidate being in the lead anymore.

题目大意:给出 n n n m m m 列的元素,为了使得最后一列的元素和不是所有列中最大的,你可以执行“删除某一行所有元素的值”的操作,该操作可以被执行多次。求使得最后一列的元素和不是最大时,执行该操作的最小次数,输出执行的最小次数,如果次数不为0,请一并输出删除了哪些行,如果有多个方案,输出任意一种方案即可。

解题思路:由于给出的数据范围并不是很大,直接暴力模拟。枚举每一列,记录使得该列元素和满足大于等于最后一列的元素和需要删除哪些行,被删掉的行可用数组来进行标记。这里用优先队列做一个小小的优化,每次返回最后一列元素与当前枚举的列差值最大的一行,并删除改行,(算得上贪心的思路啦),其他的就是细节的处理了,下边附上AC代码。

上代码:

#include<bits/stdc++.h>
using namespace std;
int sum[110];//记录每一列的和 
struct people{
    int del;//删去的行数
    int vis[110];//标记被删去的行 
}st[110]; 
struct node{
    int row;//行号
    int div;//与最后一列的差值差值 
    bool operator<(const node &w) const
    {
        return div < w.div;  
    } 
};
int a[110][110];
bool cmp(people x,people y)
{
    return x.del < y.del;
}
int main()
{
    int n,m;
    scanf("%d%d",&m,&n);
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(int j = 1;j <= m;j++)
    {
        for(int i = 1;i <= n;i++)
        {
            sum[j] += a[i][j];
        }
    } 
    /*for(int i=1;i<=m;i++)
    {
        cout<<sum[i]<<" ";
    }
    puts("");*/
    int s=0;
    for(int j = 1;j < m;j++) //枚举每一列与最后一列进行比较 
    {
        int tmp_1 = sum[j],tmp_2 = sum[m];
        int vis[110];
        memset(vis,false,sizeof vis);
        int cnt = 0;//被删掉的个数 
        priority_queue<node> q;
        for(int i = 1;i <= n;i++)
        {
            q.push({i,a[i][m]-a[i][j]});
        }
        while(tmp_1 < tmp_2)
        {
            node t = q.top();q.pop();
            //cout<<t.row<<" "<<t.div<<endl;
            vis[t.row]=true;//这一行被删掉 
            cnt++;
            tmp_1-=a[t.row][j];
            tmp_2-=a[t.row][m]; 
        }
        st[++s].del = cnt;
        memcpy(st[s].vis,vis,sizeof vis);
    } 
    sort(st+1,st+s+1,cmp);
    /*for(int i=1;i<=s;i++)
    {
        cout<<st[i].del<<" ";
    }
    puts("");*/
    cout<<st[1].del<<endl;
    if(st[1].del)
    {
        for(int i=1;i<=n;i++)
        {
            if(st[1].vis[i])
            {
                cout<<i<<" ";
            }
        }
    }
     
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值