【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());
}
}
}
【Codeforces 1019A】Elections的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- E20170610-hm
presence n. 出席; 仪表; 风度; 鬼魂,神灵; defence n. 防御; 辩护; 防御工事; 后卫; phyle n. 种族,宗族; race n. 赛跑; 民族; 人种; ...
- bzoj1085骑士精神(搜索)
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1893 Solved: 1051 Description ...
- Mybatis Generator插件升级版
一.目的: 1. *mapper.java 文件名称 改为*DAO.java2. mapper以及mapper.xml 重复执行,只会覆盖原模板方法,不会覆盖自定义方法3. 实体类添加中文注释 二.步 ...
- Paxos,Raft,Zab一致性协议-Raft篇
Raft是一个一致性算法,旨在易于理解.它提供了Paxos的容错和性能.不同之处在于它被分解为相对独立的子问题,它清楚地解决了实际系统所需的所有主要部分.我们希望Raft能够为更广泛的受众提供共识,并 ...
- 语音跟踪:信号分解、锁相、鸡尾酒会效应、基于PR的信号分离
NLP中关于语音的部分,其中重要的一点是语音信号从背景噪音中分离.比如在一个办公室场景中,有白天的底噪-类似于白噪音的噪音.空调的声音.键盘的啪啪声.左手边45度7米元的地方同事讨论的声音.右手边1. ...
- arx移植 及预处理器
来源:http://bbs.mjtd.com/thread-102486-1-1.html 另,ObjectARX编程参考:http://bbs.mjtd.com/forum-14-1.html 如果 ...
- C++标准库 vector排序
前天要做一个对C++ STL的vector容器做一个排序操作,之前一直把vector当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...
- 日常开发需要掌握的Git命令
本人待的两家公司,一直都是用的SVN,Git我只是自己私下学习和开发小项目的时候用过,工作一直没有使用过,但还是要学的... Git是最好的分布式版本控制系统 工作流程 SVN和Git的区别 SVN是 ...
- 牛客多校Round 8
Solved:2 rank:164 签了两个oeis,但这样真的开心嘛
- Python字符的转义
参考原文 廖雪峰Python教程 字符的转义 字符串是以单引号' 或双引号" 括起来的任意文本,比如'abc',"xyz".''或""本身只是一种表示 ...