题意:

有 n个人,m个党派,第i个人开始想把票投给党派pi,而如果想让他改变他的想法需要花费ci元。你现在是党派1,问你最少花多少钱使得你的党派得票数大于其它任意党派。

n,m<3000

思路:

枚举“除了党派1之外其他党派最多有的票的数量”,贪心一下即可

坑点:

1.爆int

2.inf太小,要设成0x3f3f3f3f3f3f3f3f

3.sort忘了从第一个开始排(被Wa7支配)

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<list> #define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x))
#pragma Gcc optimize(2) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const int maxn = + ;
const int maxm = 5e3 + ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
int scan(){
int res=,ch,flag=;
if((ch=getchar())=='-')
flag=;
else if(ch>=''&&ch<='')
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return flag?-res:res;
}
//int vis[maxn];
struct vt{
int id;
int p;
ll c;
}v[maxn];
int num[maxn];
int cnt[maxn];
int vis[maxn];
/*bool cmp(vt a, vt b){
if(a.p==1)return false;
if(b.p==1)return true;
if(num[a.id] == num[b.id]){
return a.c < b.c;
}
else return num[a.id]<num[b.id];
}*/
bool cmp(vt a, vt b){
if(a.p==)return false;
if(b.p==)return true;
return a.c < b.c;
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
//mem(vis, 0);
mem(num, );
for(int i = ; i <= n; i++){
int p, c;
scanf("%d %lld",&v[i].p, &v[i].c);
v[i].id = i;
num[v[i].p]++; }
ll ans = 0x3f3f3f3f3f3f3f3f;
sort(v+, v++n, cmp);
for(int i = ; i < n; i++){
ll tmp = ;
mem(vis, );
for(int j = ; j <= m ;j++){
cnt[j] = num[j];
}
for(int j = ; j <= n && v[j].p != ; j++){
if(cnt[v[j].p] > i){
cnt[]++;
cnt[v[j].p]--;
tmp += v[j].c;
vis[j] = ;
} }
if(cnt[] <= i){
for(int j = ; j <= n; j++ ){
if(cnt[]>i)break;
if(!vis[j]){
tmp += v[j].c;
cnt[]++;
}
}
}
if(cnt[]>=i)ans = min(tmp, ans);
//printf("%lld %d\n", tmp, cnt[1]);
}
printf("%lld", ans);
return ;
}

codeforces 1020 C Elections(枚举+贪心)的更多相关文章

  1. Codeforces #503 C. Elections(贪心,逆向

    我的参考的博客地址 题目 逆向考虑. 暴力遍历 k(k是1到n/2+1 范围内的),挑出对于每一个k,记对于党派 i,num[ i ]为其票数.num[ i ]小于k-1的就不用改变投票了(这部分是比 ...

  2. Codeforces C. Elections(贪心枚举三分)

    题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  6. 51nod1625(枚举&贪心)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...

  7. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  8. CodeForces - 1020C C - Elections(贪心+枚举)

    题目: 党派竞争投票 有n个人,m个党派,这n个人每个人有一个想要投的党派的编号Pi,如果想要这个人改变他的想法,那么就需要花费Ci元钱. 现在你是编号为1的党派,如果你想要赢(你的票数严格大于其他党 ...

  9. codeforces 613B B. Skills(枚举+二分+贪心)

    题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. ssh免密登陆和加密解密

    一 丶实现无密码的远程管理 1.生成公钥 私钥 [root@room9pc14 桌面]# ssh-keygen [root@room9pc14 桌面]# ls /root/.ssh/ 2.上传公钥到虚 ...

  2. 对接百度地图API 实现地址转经纬度

    <?php class BaiduLBS { public static $_ak = '你的KEY值'; # Util::request 是我封装的一个请求URL类,自己可以写一个 可以提交 ...

  3. C#多线程与异步

    1.什么是异步同步 如果一个方法被调用,调用者需要等待该方法被执行完毕之后才能继续执行,则是同步. 如果方法被调用后立刻返回,即使该方法是一个耗时操作,也能立刻返回到调用者,调用者不需要等待该方法,则 ...

  4. css写斜角

    项目开发中遇到了这样的效果,百度了一波,可以使用css3的伪类实现: /*斜角公用*/1.外层的div加class='wrapper' 并需要设置相对定位 .wrapper:before { -moz ...

  5. hadoop搭建

    一.前期准备 1.1 静态ip,请查看虚拟机安装不放呢 1.2 hostname 以及 hosts文件修改 cat /etc/hostname 不同的机器设置不同的名字 cat /etc/hosts ...

  6. cogs 1440. [NOIP2013]积木大赛 贪心水题

    1440. [NOIP2013]积木大赛 ★★   输入文件:BlockNOIP2013.in   输出文件:BlockNOIP2013.out   简单对比时间限制:1 s   内存限制:128 M ...

  7. 大厂面试中三次握手延伸出来n连发你受得了?

    目录 一.这是一次有故事的对话 二.三次握手的客户端服务端状态 1 先画个图看看有哪些状态 2 tcp协议内容解析 3 通过工具wireshark来验证我们所述 三.说下Linux网络编程常用API ...

  8. 聊聊HTTP请求那一些事_HttpWebRequest_一篇就够了

    ​最近工作比较忙,很久没有写东西了,今天抽点时间整体一下最近工作的一个知识点小结.http请求对我们来说一点都不模式,程序员的我们有可能天天就是和这一些打交道.无论是BS架构的程序,前后端的数据交互, ...

  9. crawler 听课笔记 碎碎念 2 一些爬虫须知的基本常识和流程

    html的宗旨:      <标签 属性=”属性的值“></标签>        只是对于文本的一种解释划分吧 dom的宗旨:      就是一个大数组,处理方便,效率低 xm ...

  10. http的异步请求

    需要用到的包(包版本应该可能不同): httpcore-4.1.4.jar httpsayncclient-4.0-alpha3.jar httpcore-nio-4.2-alpha3.jar /** ...