【Codeforces 1019A】Elections

【链接】 我是链接,点我呀:)
【题意】


每个人都有自己喜欢的队员
但是如果贿赂他们可以让他们更改自己喜欢的队员
问你最少要花多少钱贿赂队员才能让1号队员严格有最多的人喜欢?

【题解】


除了1号之外,其他队员最后喜欢的人数不太好确定。
我们可以这样,用up枚举其他人最后喜欢的人数的上限(即除了1之外所有人都不能超过up)
如果某个人的被喜欢人数超过了,则把所有喜欢这个人当中需要贿赂用的钱数最低的几个贿赂了.让他低于up
然后我们再在这种情况下,让1号队员成为最被喜欢的人(选择之前没被贿赂的且原本不是选择1的人贿赂它,直到1号队员被喜欢人数大于所有人
这样就能覆盖所有的情况了。
取所有情况下的最小值即可。

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = 3000;
    static class Task{
        
        int n,m,id;
        int p[],c[];
        
        
        static class Person implements Comparable<Person>{
            int prefer,cost,id;
            Person(int prefer,int cost,int id) {
                this.prefer = prefer;
                this.cost = cost;
                this.id = id;
            }
            @Override
            public int compareTo(Person o) {
                // TODO Auto-generated method stub
                return this.cost-o.cost;
            }
            
        }
        Person persons[];
        ArrayList voters[];
        boolean flag[];
        
        public void solve(InputReader in,PrintWriter out) {
            flag = new boolean[N+10];
            persons = new Person[N+10];
            voters = new ArrayList[N+10];
            p = new int[N+10];c = new int[N+10];
            n = in.nextInt();m = in.nextInt();
            for (int i = 1;i <= n;i++) {
                int pp,cc;
                pp = in.nextInt();cc = in.nextInt();
                p[i] = pp;c[i] = cc;
                persons[i] = new Person(pp,cc,i);
            }
            Arrays.sort(persons, 1,n+1);
            for (int i = 1;i <= m;i++) voters[i] = new ArrayList();
            for (int i = 1;i <= n;i++) {
                voters[persons[i].prefer].add(persons[i].id);
            }
            long ans = -1;
            for (int up = 1;up <= n;up++) {
                for (int j = 1;j <= n;j++) flag[j] = false;
                long temp = 0;
                int cnt = voters[1].size(),ma = 0;
                for (int j = 2;j <= m;j++) {
                    int len = voters[j].size();
                    //out.println("voters["+j+"].size="+len);
                    if (len>=up) {
                        for (int k = 0;k <= len-up;k++) {
                            flag[(int)voters[j].get(k)] = true;
                            temp = temp + c[(int)voters[j].get(k)];
                            cnt++;
                        }
                        ma = Math.max(ma, up-1);
                    }else {
                        ma = Math.max(ma, len);
                    }
                }
                if (cnt<=ma)
                    for (int j = 1;j <= n;j++)
                        if (flag[persons[j].id]==false && persons[j].prefer!=1) {
                            if (cnt<=ma) {
                                cnt++;
                                flag[persons[j].id] = true;
                                temp = temp + persons[j].cost;
                            }else break;
                        }
                if (cnt>ma) {
                    if (ans==-1) {
                        ans = temp;
                    }else {
                        ans = Math.min(ans, temp);
                    }
                }
            }
            out.println(ans);
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

转载于:https://www.cnblogs.com/AWCXV/p/10440066.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值