洛谷 P3419 [POI2005]SAM-Toy Cars
题目描述
Johnny is a little boy - he is only three years old and enjoys playing with toy cars very much. Johnny has nn different cars. They are kept on a shelf so high, that Johnny cannot reach it by himself. As there is little space in his room, at no moment may there be more than kk toy cars on the floor.
Johnny plays with one of the cars on the floor. Johnny's mother remains in the room with her son all the time. When Johnny wants to play with another car that is on the floor, he reaches it by himself. But when the toy is on the shelf, his mummy has to hand it to him. When she gives Johnny one car, she can at the same time pick any car from the floor and put it back on the shelf (so that there remains sufficient space on the floor).
The mother knows her child very well and therefore can predict perfectly which cars Johnny will want to play with. Having this knowledge, she wants to minimize the number of times she has to hand Johnny a toy from the shelf. Keeping that in mind, she has to put the toys off on the shelf extremely thoughtfully.
TaskWrite a programme that:
reads from the standard input the sequence of toy cars in order in which Johnny will want to play with them,calculates the minimal number of times the mother has to pick cars from the shelf,writes the result to the standard output.
Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到它们. 为了让他的房间有足够的空间,在任何时刻地板上都不会有超过k 个玩具. Jasio 在地板上玩玩具. Jasio'的妈妈则在房间里陪他的儿子. 当Jasio 想玩地板上的其他玩具时,他会自己去拿,如果他想玩的玩具在架子上,他的妈妈则会帮他去拿,当她拿玩具的时候,顺便也会将一个地板上的玩具放上架子使得地板上有足够的空间. 他的妈妈很清楚自己的孩子所以他能够预料到Jasio 想玩些什么玩具. 所以她想尽量的使自己去架子上拿玩具的次数尽量的少,应该怎么安排放玩具的顺序呢?
输入输出格式
输入格式:
In the first line of the standard input there are three integers: nn,kk,pp (1\le k\le n\le 100\ 0001≤k≤n≤100 000, 1\le p\le 500\ 0001≤p≤500 000), separated by single spaces. These denote respectively: the total number of cars, the number of cars that can remain on the floor at once and the length of the sequence of cars which Johnny will want to play with. Each of the following pp lines contains one integer. These integers are the numbers of cars Johnny will want to play with (the cars are numbered from 11 to nn).
输出格式:
In the first and only line of the standard output one integer should be written - the minimal number of times his mother has to pick a car from the shelf.
输入输出样例
3 2 7
1
2
3
1
3
1
2
4
思路:经典的贪心
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 500001
using namespace std;
struct nond{
int net,id;
}heap[MAXN];
int ans;
int vis[MAXN];
int n,k,m,size;
int a[MAXN],net[MAXN],last[MAXN];
void add(int net,int x){
size++;
int place=size,parent=size>>;
while(parent>=&&heap[parent].net<net){
heap[place]=heap[parent];
place=parent;
parent>>=;
}
heap[place].net=net;
heap[place].id=x;
}
void deletee(){
nond now=heap[size--];
int place=,child=;
while(child<size){
if(child<size&&heap[child].net<heap[child+].net)
child++;
if(heap[child].net>now.net){
heap[place]=heap[child];
place=child;
child<<=;
}
else break;
}
heap[place]=now;
return ;
}
int main(){
scanf("%d%d%d",&n,&k,&m);
for(int i=;i<=m;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++) last[i]=MAXN+;
for(int i=m;i>=;i--){
net[i]=last[a[i]];
last[a[i]]=i;
}
int cnt=;
for(int i=;i<=m;i++){
if(vis[a[i]]){
add(net[i],a[i]);
continue;
}
if(cnt<k){
ans++;
cnt++;
add(net[i],a[i]);
vis[a[i]]=;
}
else{
while(vis[heap[].id]==) deletee();
vis[heap[].id]=;
deletee();
ans++;
vis[a[i]]=;
add(net[i],a[i]);
}
}
cout<<ans;
}
洛谷 P3419 [POI2005]SAM-Toy Cars的更多相关文章
- P3419 [POI2005]SAM-Toy Cars / SP688 SAM - Toy Cars
一道很妙的贪心题 题面 我们考虑当我们插入时会面临的两种情况 当地上的玩具,不满 \(k\) 个时,那我们直接放就可以了. 当满了 \(k\) 个的时候,我们就要从地上拿出一个来给当前的腾位置. 这就 ...
- 洛谷P3431 [POI2005]AUT-The Bus
P3431 [POI2005]AUT-The Bus 题目描述 The streets of Byte City form a regular, chessboardlike network - th ...
- 洛谷 P3420 [POI2005]SKA-Piggy Banks
P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...
- 洛谷 CF1153B Serval and Toy Bricks
目录 题目 思路 \(Code\) 题目 CF1153B Serval and Toy Bricks 思路 自己也很懵的一道题(不知道自己怎么就对了)...只要对于所给的俯视图上值为\(1\)的位置输 ...
- 洛谷P3195 玩具装箱TOY
题目大意: 有n个数,要将他们分成若干段,每一段的cost定义为: cost=r-l+ΣCk (k∈[r,l]) 该段的最终花费是:(cost-L)^2; 给出L,n,C(1~n),总共的最小花费. ...
- 洛谷 P3420 [POI2005]SKA-Piggy Banks 题解
蒟蒻的第二篇题解 嗯,直接进入正题 先告诉你们这是并查集,好吧,标签上面有,再来分析这为什么是并查集. 根据题意: 每一个存钱罐能够用相应的钥匙打开或者被砸开,Byteazar已经将钥匙放入到一些存钱 ...
- 【洛谷4070】 [SDOI2016]生成魔咒(SAM)
传送门 洛谷 Solution 考虑要求的是什么,前缀的本质不同的字符串个数? 如果只要求一个串那么显然答案是\(\sum_{i=1}^{tot}len[i]-len[fa[i]]\)(实际上这个并不 ...
- BZOJ1229 & 洛谷2917:[USACO2008 NOV]toy 玩具 & 洛谷4480:[BJWC2018]餐巾计划问题——题解
标题很长emmm…… [USACO2008 NOV]toy 玩具 https://www.luogu.org/problemnew/show/P2917 https://www.lydsy.com/J ...
- 【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)
自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下. 题目: 洛谷3181 分析: SAM(可能是)模板题(不会SAM的同学戳我:[知识总结]后缀自动机的构建). 对\(s1\)建出SAM,用\ ...
随机推荐
- appium ios端自动化测试配置
一.安装环境介绍macOS 10.12.4 Xcode 8.3.2 适用机型:iOS9 及以上机型 二.Appium源码安装Xcode升级8.2之后不再支持UIAutomation,转而使用XCUIT ...
- [洛谷P1892][codevs2597]团伙
题目大意:有n个强盗,他们有这样的关系:1.朋友的朋友是朋友:2.敌人的敌人是朋友. 两个人是朋友,则他们在一个团伙中,是敌人则在不同团伙中. 现在给出一些朋友或敌人的关系,问最多有多少团伙.输入保证 ...
- 线性规划(LP)资料下载
1.学习用PPT harvard gondzio IOE610 mit cxg286 含matlab程序 2.测试库 BPMPD netlib fsu 3.软件测试 BENCHMARKS FOR OP ...
- linux进程管理之进程创建(三)
在linux系统中,许多进程在诞生之初都与其父进程共同用一个存储空间.但是子进程又可以建立自己的存储空间,并与父进程“分道扬镳”,成为与父进程一样真正意义上的进程. linux系统运行的第一个进程是在 ...
- #error 、 #line 和 #pragma 的使用
1. #error 的用法 (1)#error 是一种预编译器指示字,用于生成一个编译错误消息 (2)用法:#error message //注意:message 不需要用双引号包围 (3)#erro ...
- System and method for assigning a message
A processor of a plurality of processors includes a processor core and a message manager. The messag ...
- wipefs进程是啥,占用了百分之90多的cpu
http://www.cnblogs.com/liuchuyu/p/7490338.html
- 3D拾取技术
在unity3d中用户通过触摸屏选中虚拟3D世界中的物体进行操控,就须要掌握3d 拾取技术. 3d拾取技术很的简单:由摄像机与屏幕上的触控点之间确定一条射线.由此射线射向3d世界, 最先和此射线相交的 ...
- hadoop-07-ntp服务检查
hadoop-07-ntp服务检查 cd /etc/more /etc/ntp.conf里面进行了server的配置 service ntpd status / stop/ start 安装ntpd ...
- iOS gzip解压
1. 导入libz库(如:libz 1.2.5.dylib) 2. 引入头文件 #import "zlib.h" 3. 实现解压(输入输出都为NSData对象) -(NSData ...