codeforces 1020 C Elections(枚举+贪心)
题意:
有 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(枚举+贪心)的更多相关文章
- Codeforces #503 C. Elections(贪心,逆向
我的参考的博客地址 题目 逆向考虑. 暴力遍历 k(k是1到n/2+1 范围内的),挑出对于每一个k,记对于党派 i,num[ i ]为其票数.num[ i ]小于k-1的就不用改变投票了(这部分是比 ...
- Codeforces C. Elections(贪心枚举三分)
题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
- 枚举+贪心 HDOJ 4932 Miaomiao's Geometry
题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...
- CodeForces - 1020C C - Elections(贪心+枚举)
题目: 党派竞争投票 有n个人,m个党派,这n个人每个人有一个想要投的党派的编号Pi,如果想要这个人改变他的想法,那么就需要花费Ci元钱. 现在你是编号为1的党派,如果你想要赢(你的票数严格大于其他党 ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- 【移动测试】你的测试用例中,是否包含App前后台切换
App前后台切换是我们平时常用的一个操作,比如:按手机的home键将应用置于后台.直接按手机电源键关闭屏幕或者通过最近打开的应用列表切换应用等,由此,我们可以得出结论:当app置于前台时,它的页面对我 ...
- 微信小程序----日期时间选择器(自定义精确到分秒或时段)
声明 bug:由于此篇博客是在bindcolumnchange事件中做的值的改变处理,因此会出现当你选择时,没有点击确定,直接取消返回后,会发现选择框的值依然改变.造成原因:这一点就是由于在bindc ...
- HTML中使用Vue+Dhtmlxgantt制作任务进度图
HTML中使用Vue+Dhtmlxgantt制作任务进度图 Dhtmlxgantt官网: https://dhtmlx.com/docs/products/dhtmlxGantt/ 参考文章 甘特图配 ...
- 前端笔记6-js2
1.break 和continue用法 break结束本次循环,如果想结束外层循环,可以通过这个label来指定要结束的循环. continue可以用来跳过当次循环,如果想跳过外次循环,也可以通过这个 ...
- GitHub进阶之利用Git远程仓库篇
#在上一篇文章,相信大家对GitHub已经有了一个基础的理解 接下来我们来学习一下如何利用git来远程仓库 一,git是什么 git:一个免费的开源版本控制软件 用途:利用Git管理GitHub上的代 ...
- web网页设计五种布局
1.大框套小框布局 2.通栏布局 3.导航栏在主视觉下方的布局 4.左中右布局 5.环绕式布局
- Activiti 规则任务(businessRuleTask)
Activiti 规则任务(businessRuleTask) 作者:Jesai 目前国内研究Activiti规则任务businessRuleTask)的文章在网上应该不超出3篇 小觑夜漫酒作伴,破晓 ...
- python 父类方法重写
class Bird: def isWing(self): print("鸟有翅膀") def fly(self): print("鸟会飞") class Os ...
- PSP第一次总结
项目计划总结 周活动总结表 姓名:王金萱 ...
- 机器学习环境配置系列三之Anaconda
1.下载Anaconda文件 进入anaconda的官网 选择对应的系统 选择希望下载的版本(本人下载的是Anaconda 5.3 For Linux Installer Python 3.7 ver ...