BZOJ 1528 POI2005 sam-Toy Cars 堆+贪心
来源:互联网 发布:上古5捏脸数据怎么用 编辑:程序博客网 时间:2024/06/16 14:42
题目大意:有n个玩具,都放在架子上,地板上能放k个,要玩p次玩具,如果不在地板上就要去架子上拿,地板满了要放回去,求最少操作次数
贪心思想:每次放回玩具时选择下次玩的时间最靠后的玩具放回去
可以用堆来模拟这一贪心过程
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 500500using namespace std;typedef pair<int,int> abcd;int n,k,p,cnt,ans;int a[M],last[100100],next[M];bool on_floor[100100];namespace Priority_Queue{abcd heap[M];int top;void Insert(abcd x){heap[++top]=x;int t=top;while(t>1){if(heap[t]>heap[t>>1])swap(heap[t],heap[t>>1]),t>>=1;elsebreak;}}void Pop(){heap[1]=heap[top--];int t=2;while(t<=top){if(t<top&&heap[t+1]>heap[t])++t;if(heap[t]>heap[t>>1])swap(heap[t],heap[t>>1]),t<<=1;elsebreak;}}}int main(){int i;cin>>n>>k>>p;for(i=1;i<=p;i++){scanf("%d",&a[i]);if(last[a[i]]) next[last[a[i]]]=i;last[a[i]]=i;}for(i=1;i<=n;i++)if(last[i])next[last[i]]=0x3f3f3f3f;for(i=1;i<=p;i++){if(on_floor[a[i]]){Priority_Queue::Insert(abcd(next[i],a[i]));continue;}if(cnt==k){on_floor[Priority_Queue::heap[1].second]=0;Priority_Queue::Pop();--cnt;}Priority_Queue::Insert(abcd(next[i],a[i]));on_floor[a[i]]=1;++ans;++cnt;}cout<<ans<<endl;return 0;}
0 0
- BZOJ 1528 POI2005 sam-Toy Cars 堆+贪心
- 【BZOJ 1528】[POI2005]sam-Toy Cars 贪心+堆
- [bzoj1528/POI2005]sam-Toy Cars---贪心+堆
- bzoj 1528: [POI2005]sam-Toy Cars
- 【bzoj1528】 [POI2005]sam-Toy Cars
- 【bzoj1528】[POI2005]sam-Toy Cars
- A. Toy Cars
- 周赛-Toy Cars
- A. Toy Cars
- bzoj 1029 贪心+堆
- bzoj 2802 贪心+堆
- bzoj 1572 贪心+堆
- bzoj 1724 堆+贪心
- 贪心-BZOJ-1623-[Usaco2008 Open]Cow Cars 奶牛飞车
- bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车(贪心)
- CodeForces 303A Toy Cars
- Toy Cars (周赛2)
- BOZJ1528/POI 2005Toy Cars
- C#与Javascript变量、函数之间的相互调用
- C语言网络编程和跨平台代码安全性
- rownum用法详解
- OC计算代程序码量
- 从Bundle安全性谈软件和数据的安全性
- BZOJ 1528 POI2005 sam-Toy Cars 堆+贪心
- 第16周-完美数(效率低,求改进)
- 算法竞赛中常见的数学(一):Fibonacci数列
- Date与String之间相互转换
- 阿里巴巴速卖通曝安全漏洞,可能泄露百万用户个人信息
- Linux 驱动模型初探2——device_driver
- cron 表达式的误区
- 各种排序(一)
- AForge.net简介和认识