题目大意:
  有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. Windows2008 – Task Scheduler – ‘Action “C:\Windows\SYSTEM32\cmd.exe” with return code 1’

    Remediation Edit Task Let us make the necessary changes, which is to specify the Start folder. Here ...

  2. vue+koa+mysql简易demo

    功能支持网址收藏编辑 代码: https://github.com/lanleilin/lanOdyssey/tree/master/vueKoa/webCollection1 运行方法: 在serv ...

  3. oracle导入和导出和授权

    导入数据库: imp demo@orcl file=d:/bak_1023.dmp full=y ignore=y 导出数据库: @orcl file=d:/bak_1023.dmpexp yhtj/ ...

  4. NodeJS概述

    NodeJS中文API 一.概述 Node.js 是一种建立在Google Chrome’s v8 engine上的 non-blocking (非阻塞), event-driven (基于事件的) ...

  5. PHP文件操作函数一

    <?php/*Created on 2013-6-26*///判断文件的类型echo filetype("array.php")."<br />&quo ...

  6. Intelij idea新窗口打开项目设置

    1.idea 打开后 file->setting   2.appearance&behave->system setting->open project in new win ...

  7. 【LA4670-Dominating Patterns】AC自动机

    http://acm.hust.edu.cn/vjudge/problem/19224 题意:给定n个单词,一个字符串,问哪些单词在字符串中出现的次数最多.单词aba,文本ababa,则aba出现了2 ...

  8. linux基础 -nginx和nfs代理 开发脚本自动部署及监控

    开发脚本自动部署及监控 1.编写脚本自动部署反向代理.web.nfs: (1).部署nginx反向代理三个web服务,调度算法使用加权轮询:  (2).所有web服务使用共享存储nfs,保证所有web ...

  9. Linux内核官方文档atomic_ops.txt【摘自Linux 内核文档】

    摘自Linux内核文档 Documentation/atomic_ops.txt,不是本人原创 Semantics and Behavior of Atomic and Bitmask Operati ...

  10. Linux makefile 教程 非常详细,且易懂【转】

    转自:   http://blog.csdn.net/liang13664759/article/details/1771246 最近在学习Linux下的C编程,买了一本叫<Linux环境下的C ...