题目大意:
  有n种物品,地上有k个格子,p次操作。
  每次操作要求将某一个指定的物品移动到任意一个格子中,同时你可以选择是否将格子中的某一个物品收起来,并消耗1的代价。
  如果下达指令时,这个物品刚好在格子上,那么就不会消耗代价。
  问至少消耗多少代价?

思路:
  贪心。
  每次移动如果时,如果地板上已经放慢了物品,那么就应该把第二次出现最晚的物品收起来。
  预处理每一次指令对应的物品第二次出现的时刻。
  用一个堆来维护当前地板上的物品即可。

 #include<cstdio>
#include<cctype>
#include<hash_set>
#include<ext/pb_ds/priority_queue.hpp>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,P=;
int a[P],cnt[N],pos[N],next[P];
__gnu_cxx::hash_set<int> set;
__gnu_pbds::priority_queue<std::pair<int,int> > q;
__gnu_pbds::priority_queue<std::pair<int,int> >::point_iterator p[N];
int main() {
const unsigned n=getint(),k=getint(),p=getint();
for(register unsigned i=;i<=p;i++) {
a[i]=getint();
cnt[a[i]]++;
}
for(register unsigned i=;i<=n;i++) pos[i]=p+;
for(register unsigned i=p;i;i--) {
next[i]=pos[a[i]];
pos[a[i]]=i;
}
unsigned ans=,i=;
for(;set.size()<k&&set.size()<n&&i<=p;i++) {
if(!set.count(a[i])) {
ans++;
set.insert(a[i]);
::p[a[i]]=q.push(std::make_pair(next[i],a[i]));
} else {
q.modify(::p[a[i]],std::make_pair(next[i],a[i]));
}
}
for(;i<=p;i++) {
if(!set.count(a[i])) {
ans++;
set.erase(q.top().second);
q.pop();
set.insert(a[i]);
::p[a[i]]=q.push(std::make_pair(next[i],a[i]));
} else {
q.modify(::p[a[i]],std::make_pair(next[i],a[i]));
}
}
printf("%u\n",ans);
return ;
}

[POI2005]Toy Cars的更多相关文章

  1. 洛谷 P3419 [POI2005]SAM-Toy Cars

    P3419 [POI2005]SAM-Toy Cars 题目描述 Johnny is a little boy - he is only three years old and enjoys play ...

  2. [POI2005]SAM-Toy Cars 贪心+堆

    [POI2005]SAM-Toy Cars 题目:Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到它们:为了让他的房间有足够的空 ...

  3. 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏

    Toy Cars time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. BZOJ1528: [POI2005]sam-Toy Cars

    1528: [POI2005]sam-Toy Cars Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 282  Solved: 129[Submit][S ...

  5. [POI2005]SAM-Toy Cars

    题目描述 Johnny is a little boy - he is only three years old and enjoys playing with toy cars very much. ...

  6. bzoj 1528 [POI2005]sam-Toy Cars 堆维护+贪心

    1528: [POI2005]sam-Toy Cars Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 716  Solved: 306[Submit][S ...

  7. Codeforces Round #303 (Div. 2) A. Toy Cars 水题

     A. Toy Cars Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...

  8. 【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)

    1528: [POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到 ...

  9. 【BZOJ1528】[POI2005]sam-Toy Cars 贪心

    [BZOJ1528][POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio ...

随机推荐

  1. springboot与dubbo结合

    转:http://www.cnblogs.com/Alandre/p/6490142.html  写的很好! 本文提纲 一.为啥整合 Dubbo 实现 SOA 二.运行 springboot-dubb ...

  2. zigbee芯片 - JN5169

    http://www.nxp.com/products/wireless-connectivity/2.4-ghz-wireless-solutions/support-resources-for-j ...

  3. js实现页面触摸滑动

    先设置一个div  高度不能设置100% . window.addEventListener("load",function(){ var addEventListener = ' ...

  4. Python爬虫学习笔记之爬取新浪微博

    import requests from urllib.parse import urlencode from pyquery import PyQuery as pq from pymongo im ...

  5. ()()()()x()=()()()()填1-9数字

    /** Function: ()()()()x()=()()()() Developer: Date: */ #include "iostream" #include " ...

  6. Spring - IoC(10): 生命周期

    Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即 ...

  7. 【51NOD】1717 好数

    [算法]数学 [题意]a数组初始为0,t=1~n,每次01翻转t的倍数,最终为0的数字定义为好数,求好数个数 [题解]一个数字为好数的条件是翻转偶数次,也即一个数是好数当且仅当有偶数个因子时. 因子都 ...

  8. bzoj 3212 线段树

    裸的线段树 /************************************************************** Problem: User: BLADEVIL Langua ...

  9. CSS3 3D 转换

    CSS3 允许您使用 3D 转换来对元素进行格式化. 在本章中,您将学到其中的一些 3D 转换方法: rotateX() rotateY() rotateX() 方法 rotateX()方法,围绕其在 ...

  10. linux USB HOST之EHCI和OHCI【转】

    转自:http://blog.csdn.net/ljzcom/article/details/8186914 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 2 关键数据结 ...