先给大家分享一道CF的题,这个题用结构体搞很复杂,看了题解用优先队列感觉很巧妙,枚举变换个数,削弱比他强的队伍的个数,然后剩下的再按从小到大弹出。

C. Elections
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 votercici bytecoins 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.

Input

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.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples
input

Copy

1 21 100

output

Copy

0

input

Copy

5 52 1003 2004 3005 4005 900

output

Copy

500

input

Copy

5 52 1003 2004 3005 8005 900

output

Copy

600

Note

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.

#include<bits/stdc++.h>
using namespace std;
const int MAXN=3007;
int n,m,i,j,k;
long long ans=1e18,cnt;
priority_queue<int,vector<int>,greater<int> >e[MAXN],s[MAXN],S;
int main()
{cin>>n>>m;for(i=1; i<=n; ++i){scanf("%d%d",&j,&k);s[j].push(k);}for(i=1; i<=n; ++i){while(!S.empty())S.pop();cnt=s[1].size();for(j=2; j<=m; ++j)e[j]=s[j];long long v=0;for(j=2; j<=m; ++j){while(e[j].size()>=i){v+=e[j].top();e[j].pop();cnt++;}}for(j=2; j<=m; ++j){while(!e[j].empty()){S.push(e[j].top());e[j].pop();}}for(; cnt<i&&!S.empty(); ++cnt){v+=S.top();S.pop();}if(cnt>=i) ans=min(ans,v);}cout<<ans<<endl;return 0;
}

这道题,我用的栈做的,感觉是一道好题

我的代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
stack<int>kepa;
int main()
{int n,m,j,a;int t=1;string s;cin>>n>>m;kepa.push(0);for(int i=1;i<=m;i++){int it=kepa.top();int res=1;int temp=0;cin>>s;if(s[0]=='u'){cin>>a;while(a--){kepa.pop();}}else{if(s[0]=='-'){res=-1;}for(j=0;j<s.length();j++){if(j==0&&res==-1){continue;}temp=temp*10+s[j]-'0';}temp=temp*res;it+=temp;while(it<0)it+=n;it%=n;kepa.push(it);}}cout<<kepa.top()<<endl;return 0;
}

然后看了pzr的做法,他用数组做的,atoi是字符串转成整数,s.c_str()专用str

pzr的代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
int main()
{int n,m;int t=1;int num[1000];num[0]=0;string s;int a,b;int sw;cin>>n>>m;for(int i=1;i<=m;i++){cin>>s;if(s[0]=='u'){cin>>a;t-=a+1;}else{a=atoi(s.c_str());sw=num[t-1];sw+=a;sw%=n;if(sw<0) sw+=n;num[t]=sw;}t++;}cout<<num[t-1]<<endl;return 0;
}

这道题,用dp求解每一步的最优子结构,dp[i][j]表示前i天的情况,j表示第i天的情况

#include <iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[105][105],w[105],pre[105];
int main()
{
int n,m,i,j;
cin>>n>>m;
for(i=1;i<=n;i++)cin>>w[i];
pre[0]=m;
for(i=1;i<=n;i++)
{pre[i]=pre[i-1]*2/3;
}
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(i=1;i<=n;i++)
{dp[i][0]=min(w[i],pre[0]);
}
for(i=0;i<=n;i++)
{for(j=0;j<=n;j++){if(dp[i][j]==-1)continue;dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+min(w[i+1],pre[j+1]));dp[i+2][j]=max(dp[i+2][j],dp[i][j]+min(w[i+2],pre[j]));dp[i+3][0]=max(dp[i+3][0],dp[i][j]+min(w[i+3],pre[0]));}
}
int ans=0;for(j=0;j<=n;j++){ans=max(ans,dp[n][j]);}cout<<ans<<endl;
return 0;
}

转载于:https://www.cnblogs.com/kepa/p/9480053.html

吼 困 鸭相关推荐

  1. CCO2017 Vera and Trail Building 构造+图论

    正解:构造+图论 解题报告: 找了半天才找到的传送门! 先简要表达下题意 一个图上,如果存在(a,b)满足a<b且存在从a到b再回到a的路径,每条道路被经过至多一次,我们称(a,b)为完美点对 ...

  2. ctfshow-1024杯

    比赛期间,太菜,没做出多少,赛后wp,记录一下 文章目录 Web 1024_WEB签到 1024_fastapi 1024_hello_world 1024_图片代理 1024_柏拉图 Misc 10 ...

  3. (头冷巨制)数据结构学习日志2——(初级掉发向)--关于顺序表.单链表的核心操作实现(By Ivan小黄)

    又是在千家万户熟睡的深夜,老夫依然坐在寝室的硬椅子上,点一支烟(并没有),手指在键盘之间舞动,一根根发丝随着空调冷风从头皮缓缓剥落-咳咳····好了不贫啦!来来来,我们乖乖写完博客睡觉觉哈~ 今天带来 ...

  4. mysql小鸭子_可读代码编写炸鸡十一 - 小黄鸭从你的心里游到脑子里

    可读代码编写炸鸡十一 - 小黄鸭从你的心里游到脑子里 多选参数推荐搜索 数据结构与算法 可读代码编写 Java Redis MySQL 大家好,我是多选参数的大炮. 可读代码编写的炸鸡很快要写到头了, ...

  5. 晚上睡不着,最近熬夜了,经常困,怎么办?

    看我又开始立flat了,我要少熬夜,熬夜会影响明天,会犯困的,害处多多. 一.非特殊时期,尽量不熬夜. 先吓吓自己(反正我又不是小孩,多吓吓自己),短期记忆力衰退.学习能力下降.反应迟钝.精力不集中. ...

  6. 为什么程序员工位上总会摆着小黄鸭?

    经授权转载自:beebee星球 ID:beebeesub 程序员桌子上摆上一个甚至一堆小黄鸭,你可能见过,但你一定会忽视. 你谈论着程序员的秃头.365全季工服,以及和乙方般被动的恋爱态势,但你从未真 ...

  7. 卤味江湖混战:紫燕、德州扒鸡IPO,卤味下半场跳出“鸭圈“

    今年的夏天格外炎热,据国家气候中心监测,今年6月,全球平均气温较常年偏高约0.4℃,为1979年以来最高. 炎炎夏日,卤味和凉菜成为广大居民餐桌上的首选,各大卤味公司在资本市场上也是打得火热,继绝味食 ...

  8. 困住外卖骑手的系统,用的是什么算法?

    引言 最近外卖骑手.系统.算法已经是大热关键词.<外卖骑手,困在系统里>这篇文章将外卖骑手这个群体的生存现状做了一次详尽的展示,现在大家都知道了骑手们看似不错的收入背后,有着怎样的艰辛与痛 ...

  9. 《不困于心,不惑于情,真正的勇敢是懦弱,最高的智慧是愚拙-雾满拦江》

    (01) 有盆友要求说: 现在的书太多啦,读一辈子都读不完.什么都要学,什么都要懂,单只是国学历史,诸子百家,就博大精深.二十四史十三经,本本深奥,字字玄奇.看得人头晕目眩,无数次哭昏在洗手间. 生命 ...

最新文章

  1. iOS 富文本编辑工厂, 让书写更简便.
  2. 一处 ADO.NET Entity Framework 的逻辑BUG
  3. android 分段显示百分比,按百分比设置排名-Android DisplayMetrics
  4. linux lz4 lzo,Linux六大压缩算法横评:Ubuntu 19.10最终选择LZ4
  5. 牛客OI周赛15-普及组
  6. 当年要是早知道这4步框架,我就不会为数据管理发愁了
  7. php支持 的编码,php编码转换函数(自动转换字符集支持数组转换)
  8. 什么是端到端训练测试_为什么端到端测试对您的团队很重要
  9. php设置外键约束,关于php:禁用教义外键约束
  10. 力扣 比较退格的字符串
  11. Vue项目中使用ant-design时设置DatePicker日期控件中文显示
  12. 华为设备离线什么意思_华为手机中的P、Mate、nova分别是什么意思?看完你全懂了...
  13. 关于线性模型你可能还不知道的二三事
  14. STM8L 低功耗模式说明
  15. iOS12加密相册、保险箱、加密相册Pro、保险箱Pro打开就闪退的,不要删除app,关闭4g和WiFi即可正常使用。
  16. navicat premium 15 下载安装详细教程
  17. Python学习日志12 - 办公自动化
  18. html输入框密码颜色,css解决浏览器输入框记住账号密码后的背景色
  19. Others2_谈谈个人常用的软件
  20. rally功能分析与使用介绍

热门文章

  1. MRI脑影像分析——根据脑图谱获取感兴趣区域mask,以海马体与丘脑为例(matlab+nilearn+nibabel+REST1.8)
  2. 基于华为云区块链服务快速部署和搭建链上应用
  3. 带圈的11-15如何打出
  4. bzoj 4605 崂山白花蛇草水
  5. BrowserslistError:E\ysg_front contains both .browserslistrc and browserslist
  6. 图解JVM垃圾回收机制
  7. Android WebRTC实现音视频对讲
  8. [笔记分享] [Tools] QPST使用过程
  9. pos机骗局收取押金如何投诉-真实案列解答
  10. CTR调研——博客、论文、代码