Codeforces Round #503 (by SIS, Div. 2) C. Elections(枚举,暴力)
2 seconds
256 megabytes
standard input
standard output
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.
Elections are coming. You know the number of voters and the number of parties — nn and mm respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give ii-th voter cicibytecoins you can ask him to vote for any other party you choose.
The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.
The first line of input contains two integers nn and mm (1≤n,m≤30001≤n,m≤3000) — the number of voters and the number of parties respectively.
Each of the following nn lines contains two integers pipi and cici (1≤pi≤m1≤pi≤m, 1≤ci≤1091≤ci≤109) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision.
The United Party of Berland has the index 11.
Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.
1 2
1 100
0
5 5
2 100
3 200
4 300
5 400
5 900
500
5 5
2 100
3 200
4 300
5 800
5 900
600
In the first sample, The United Party wins the elections even without buying extra votes.
In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 33, 44 and 55 get one vote and party number 22 gets no votes.
In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
题意:
题意概述:有n个学生,m个政党,每个学生有支持的政党,但是如果你给他一些钱,他就可以给你想让他投的党投票,现在想付出最少的钱使得1政党有绝对优势(票数严格大于其他党)。1<=n,m<=30001<=n,m<=3000
有一种贪心策略是一直收买所需钱最少的学生直到符合条件,但是这样显然是有点问题的,有可能其实只用收买一个收钱多的使得他的政党失败就可以了,但是却收买了许多所需钱虽然少但是无关紧要的人。关键是1号的票数没有确定使得难以贪心,所以考虑枚举最终票数。枚举完票数就开始处理,把每个党超过这个票数且收钱最少的人收买过来,如果这些人都收买完了可是还没有达到预定的票数,就一直收买之前还没有收买过的学生直到人数达标。开long long。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=3300;
const ll inf=0x3f3f3f3f3f3f;
ll n,m;
vector<int>a[maxn];
ll calc(int cnt){
int left=cnt-a[1].size();//还要多少个人
ll ans=0;//花费的钱
int tot=0;//
int val[maxn];
for(int i=2;i<=m;i++){
int tmp=0;//2>=2 0=2-2+1如果减去不够就无穷大
if(a[i].size()>=cnt)tmp=a[i].size()-cnt+1;
if(tmp>left)return inf;
left-=tmp;//需要的人数剪掉
for(int j=0;j<tmp;j++){
ans+=a[i][j];
}
for(int j=tmp;j<a[i].size();j++){
val[++tot]=a[i][j];
}
}
sort(val+1,val+tot+1);
for(int i=1;i<=left;i++)ans+=val[i];
return ans; }
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
ll ans=inf;
cin>>n>>m;
for(int i=1;i<=n;i++){
ll one,two;
cin>>one>>two;
a[one].push_back(two);
}
for(int i=1;i<=m;i++)sort(a[i].begin(),a[i].end());
for(int cnt=a[1].size();cnt<=n;cnt++)
ans=min(ans,calc(cnt));
cout<<ans<<endl;
return 0;
}
Codeforces Round #503 (by SIS, Div. 2) C. Elections(枚举,暴力)的更多相关文章
- Codeforces Round #503 (by SIS, Div. 2) C. Elections (暴力+贪心)
[题目描述] Elections are coming. You know the number of voters and the number of parties — n and m respe ...
- Codeforces Round #503 (by SIS, Div. 2)-C. Elections
枚举每个获胜的可能的票数+按照花费排序 #include<iostream> #include<stdio.h> #include<string.h> #inclu ...
- Codeforces Round #503 (by SIS, Div. 2) Solution
从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat P ...
- Codeforces Round #503 (by SIS, Div. 2)
连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...
- Codeforces Round #503 (by SIS, Div. 1)E. Raining season
题意:给一棵树每条边有a,b两个值,给你一个m,表示从0到m-1,假设当前为i,那么每条边的权值是a*i+b,求该树任意两点的最大权值 题解:首先我们需要维护出(a,b)的凸壳,对于每个i在上面三分即 ...
- Codeforces Round #503 (by SIS, Div. 2) D. The hat
有图可以直观发现,如果一开始的pair(1,1+n/2)和pair(x, x+n/2)大小关系不同 那么中间必然存在一个答案 简单总结就是大小关系不同,中间就有答案 所以就可以使用二分 #includ ...
- Codeforces Round #503 (by SIS, Div. 2)B 1020B Badge (拓扑)
题目大意:每个同学可以指定一个人,然后构成一个有向图.1-n次查询,从某个人开始并放入一个东西,然后循环,直到碰到一个人已经放过了,就输出. 思路:直接模拟就可以了,O(n^2) 但是O(n)也可以实 ...
- Codeforces Round #503 (by SIS, Div. 2) D. The hat -交互题,二分
cf1020D 题意: 交互题目,在有限的询问中找到一个x,使得数列中的第x位和第(x+n/2)位的值大小相同.数列保证相邻的两个差值为1或-1: 思路: 构造函数f(x) = a[x] - a[x ...
- Codeforces Round #503 (by SIS, Div. 2) E. Sergey's problem
E. Sergey's problem [题目描述] 给出一个n个点m条边的有向图,需要找到一个集合使得1.集合中的各点之间无无边相连2.集合外的点到集合内的点的最小距离小于等于2. [算法] 官方题 ...
随机推荐
- shell脚本批量下载资源并保留路径
示例资源列表 如url.txt: http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png http://su.bdimg.co ...
- 超链接标签的CSS伪类link,visited,hover,active
CSS伪类,是一种特殊的类,它针对到CSS选择器起作用,使选中的标签或元素产生特定的效果. CSS伪类的语法就是: 选择器 : 伪类名 { 属性 : 属性值 } 用的最多的伪类就是超链接a的伪类,有: ...
- python-成员修饰符
python的面相对象中,拥有3个成员,字段.方法.属性 class Foo: def __init__(self,name): #公有字段name在类中与类外均能调用 self.name = nam ...
- 立体匹配之Census Transform
1.立体匹配算法主要可分为两大类:基于局部约束和基于全局约束的立体匹配算法. (一)基于全局约束的立体匹配算法:在本质上属于优化算法,它是将立体匹配问题转化为寻找全局能量函数的最优化问题,其代表算法主 ...
- C++ Primer 第3章 字符串、向量和数组
C++ Primer 第3章 字符串.向量和数组 C Primer 第3章 字符串向量和数组 1 命名空间的using声明 2 标准库类型string 3 标准库类型vector 4 迭代器介绍 5 ...
- 软工实践Beta冲刺前装备
过去存在的问题 组员之间缺乏沟通,前后端缺乏沟通协作 组员积极性不高 基础知识不够扎实 手动整合代码效率过低 我们已经做了哪些调整/改进 通过会议加强组员之间的交流 组长请喝了奶茶提高大家积极性 努力 ...
- RabbitMQ-Java客户端API指南-上
RabbitMQ-Java客户端API指南-上 客户端API严格按照AMQP 0-9-1协议规范进行建模,并提供了易于使用的附加抽象. RabbitMQ Java客户端使用com.rabbitmq.c ...
- vue实现多个元素或多个组件之间动画效果
多个元素的过渡 <style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-acitve,.v-leave-active{ opacity: ...
- BZOJ1823 [JSOI2010]满汉全席 【2-sat】
题目 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而能够烹饪出经过专家 ...
- oracle的隐式游标
游标的概念: 游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理, ...