题意:

有 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. day4(dict和set)

    dict 内置字典,使用key-value存储,具有极快的查找速度. >>>d = {'michael': 95, 'bob': 75, 'tracy': 85} >>& ...

  2. Java网络编程系列之TCP连接状态

    1.TCP连接状态 LISTEN:Server端打开一个socket进行监听,状态置为LISTEN SYN_SENT:Client端发送SYN请求给Server端,状态由CLOSED变为SYN_SEN ...

  3. zTree 节点勾选取消勾选 选中取消选中

    zTreeObj.cancelSelectedNode function 举例 取消当前所有被选中节点的选中状态 var treeObj = $.fn.zTree.getZTreeObj(" ...

  4. spring boot集成spring-boot-starter-mail邮件功能

    前情提要 以目前IT系统功能来看,邮件功能是非常重要的一个功能.例如:找回密码.邮箱验证,邮件动态码.忘记密码,邮件营销等,都需要用到邮件功能.结合当下最流行的spring boot微服务,推出了sp ...

  5. ASP.Net Core 3.0 中使用JWT认证

    JWT认证简单介绍     关于Jwt的介绍网上很多,此处不在赘述,我们主要看看jwt的结构.     JWT主要由三部分组成,如下: HEADER.PAYLOAD.SIGNATURE HEADER包 ...

  6. 2019牛客暑期多校第二场题解FH

    F.Partition problem 传送门 题意:有2n个人,分两组,每组n个,要求sum(vij)最大值. 题解:n并不大我们可以枚举每个人是在1组还是2组爆搜. 代码: #include &l ...

  7. vue的param和query两种传参方式及URL的显示

    路由配置: // 首页 { path: '/home', name:'home', component:Home }, // 行情 { path: '/markets', name:'market', ...

  8. 月薪30k的Java架构师JVM常见面试题解析

    在做程序员的路上经常会遇到的JVM一些经典面试题,今天给大家分享出我自己的解题思路,希望对大家有帮助,后续有空会持续更新. 1.什么情况下会发生栈内存溢出. 思路: 描述栈定义,再描述为什么会溢出,再 ...

  9. gitbub 基本使用

    一.环境 git:https://git-scm.com/ 申请github账号:https://github.com/ 二.安装git 一直next即可 三.创储存建库 1.选择New reposi ...

  10. item方法

    class Person: def __init__(self, name, age): self.name = name self.age = age def __getitem__(self, i ...