【codeforces】Heidi and Library (贪心STL)
来源:互联网 发布:淘宝卖家都在哪里进货 编辑:程序博客网 时间:2024/06/11 20:01
题目:A. Heidi and Library
(40000)
题意:
小H有一份来看书的人的顺序表(一天一人),他的柜子是有空间限制的,当一个人来到书馆,H的柜子里如果没有这本书就花¥1买,但如果柜子满了,就必须选择扔掉一本书,问最小的花费
题解:
当你要扔一本书的时候,选择后面所有书中离你最远的一本扔掉。
但这个题目的亮点在于STL的使用(毕竟小萌新
优先队列需要重载运算符
代码:
#include <cstdio>#include <queue>#include <iostream>#define INF 1e9+7using namespace std;queue <int> q[400005];struct hh{ int v,bh;}nxt;priority_queue <hh> sj; int n,k,i;int a[400005];bool c[400005];bool operator < (const hh &a,const hh &b){ return a.v<b.v;}int main(){ int ke=0; scanf("%d%d",&n,&k); for (i=1;i<=n;i++) { scanf("%d",&a[i]); q[a[i]].push(i); } for (i=1;i<=n;i++)//不存在的书压箱底! if (!q[i].empty()) q[i].pop(),q[i].push(INF); int ans=0; for (i=1;i<=n;i++) { if (c[a[i]]) { nxt.v=q[a[i]].front(); q[a[i]].pop(); nxt.bh=a[i]; sj.push(nxt); continue; } else if (ke<k) ke++; else { nxt=sj.top(); sj.pop(); while (!c[nxt.bh]) nxt=sj.top(),sj.pop(); c[nxt.bh]=false; } c[a[i]]=true; ans++; nxt.v=q[a[i]].front(); q[a[i]].pop(); nxt.bh=a[i]; sj.push(nxt); } printf("%d",ans);}
阅读全文
0 0
- 【codeforces】Heidi and Library (贪心STL)
- Heidi and Library (medium)
- Codeforces 802A Heidi and Library (easy) 题解
- codeforces802C Heidi and Library (hard) -- 费用流
- Heidi and Library (easy)(cf5.18团队赛模拟水题)
- codeforces802 A&&B Heidi and Library (easy&&medium)
- Codeforces 611E 贪心 STL
- Codeforces Round #410 (Div. 2)-B. Mike and strings【STL+略贪心】
- HDU 4268 Alice and Bob(贪心+STL)
- 贪心,STL,multiset(Alice and Bob,HDU 4268)
- HDU 4268 Alice and Bob(贪心+STL)
- CodeForces 534D Handshakes 【STL】+【贪心】
- CodeForces 732E Sockets 【贪心】【STL】
- Codeforces 387B George and Round(贪心)
- Codeforces 387C George and Number(贪心)
- Codeforces 388A Fox and Box Accumulation(贪心)
- Codeforces 388C Fox and Card Game(贪心)
- Codeforces 390B Inna, Dima and Song(贪心)
- OpenFOAM4.0安装教程
- Linux进程间通信之消息队列
- json应用场景与实战
- eclipse引入maven工程的正确姿势
- tfRecord写入
- 【codeforces】Heidi and Library (贪心STL)
- 17. Letter Combinations of a Phone Number
- Linux-脚本安装软件
- 【YOLO学习】召回率(Recall),精确率(Precision),平均正确率(Average_precision(AP) ),交除并(Intersection-over-Union(IoU))
- Maven学习(七)
- Lintcode64 Merge Sorted Array solution 题解
- 【拓扑排序模板题DFS方法】UVA
- 【DP】TEST20170429
- 莲子百合银耳羹