洛谷 P3419 [POI2005]SAM-Toy Cars

时间:2022-09-02 13:12:24

题目描述

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.

输入输出样例

输入样例#1:
3 2 7
1
2
3
1
3
1
2
输出样例#1:
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的更多相关文章

  1. P3419 &lbrack;POI2005&rsqb;SAM-Toy Cars &sol; SP688 SAM - Toy Cars

    一道很妙的贪心题 题面 我们考虑当我们插入时会面临的两种情况 当地上的玩具,不满 \(k\) 个时,那我们直接放就可以了. 当满了 \(k\) 个的时候,我们就要从地上拿出一个来给当前的腾位置. 这就 ...

  2. 洛谷P3431 &lbrack;POI2005&rsqb;AUT-The Bus

    P3431 [POI2005]AUT-The Bus 题目描述 The streets of Byte City form a regular, chessboardlike network - th ...

  3. 洛谷 P3420 &lbrack;POI2005&rsqb;SKA-Piggy Banks

    P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...

  4. 洛谷 CF1153B Serval and Toy Bricks

    目录 题目 思路 \(Code\) 题目 CF1153B Serval and Toy Bricks 思路 自己也很懵的一道题(不知道自己怎么就对了)...只要对于所给的俯视图上值为\(1\)的位置输 ...

  5. 洛谷P3195 玩具装箱TOY

    题目大意: 有n个数,要将他们分成若干段,每一段的cost定义为: cost=r-l+ΣCk (k∈[r,l]) 该段的最终花费是:(cost-L)^2; 给出L,n,C(1~n),总共的最小花费. ...

  6. 洛谷 P3420 &lbrack;POI2005&rsqb;SKA-Piggy Banks 题解

    蒟蒻的第二篇题解 嗯,直接进入正题 先告诉你们这是并查集,好吧,标签上面有,再来分析这为什么是并查集. 根据题意: 每一个存钱罐能够用相应的钥匙打开或者被砸开,Byteazar已经将钥匙放入到一些存钱 ...

  7. 【洛谷4070】 &lbrack;SDOI2016&rsqb;生成魔咒(SAM)

    传送门 洛谷 Solution 考虑要求的是什么,前缀的本质不同的字符串个数? 如果只要求一个串那么显然答案是\(\sum_{i=1}^{tot}len[i]-len[fa[i]]\)(实际上这个并不 ...

  8. BZOJ1229 &amp&semi; 洛谷2917:&lbrack;USACO2008 NOV&rsqb;toy 玩具 &amp&semi; 洛谷4480:&lbrack;BJWC2018&rsqb;餐巾计划问题——题解

    标题很长emmm…… [USACO2008 NOV]toy 玩具 https://www.luogu.org/problemnew/show/P2917 https://www.lydsy.com/J ...

  9. 【BZOJ4566&lowbar;洛谷3181】&lbrack;HAOI2016&rsqb;找相同字符(SAM)

    自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下. 题目: 洛谷3181 分析: SAM(可能是)模板题(不会SAM的同学戳我:[知识总结]后缀自动机的构建). 对\(s1\)建出SAM,用\ ...

随机推荐

  1. 对IOC和DI以及AOP的理解

    为了理解Spring的IoC与DI从网上查了很多资料,作为初学者,下面的描述应该是最详细,最易理解的方式了. 首先想说说IoC(Inversion of Control,控制倒转).这是spring的 ...

  2. oc-数据模型的建立

    @{@"name":@"David Beckham",@"age":@38,@"gender":@"男&quo ...

  3. 萌货猫头鹰登录界面动画iOS实现分析

    动画效果仿自国外网站readme.io的登录界面,超萌可爱的猫头鹰,具体效果如下图所示. 动画实现核心: 动画核心的是用到了iOS中UIView的transform属性,然后根据尺寸坐标对四张图片进行 ...

  4. python 基础篇(二)数据类型概述

    正式进入python的学习. 数据类型可以分为身份,类型,数据项三项联合组成. 身份: id() 类型:type() 数据类型:int,boolean,tuple,string,dict,list 1 ...

  5. fscanf()功能具体解释

    一旦文件被解析常规时间或使用正则表达式.或者是敲自己太傻代码来解析一个普通文件. 今天突然发现c该图书馆有一个现成的文件可以解析常规功能,这是fscanf()功能.哎 曾经自己做了这么多无用功.在这里 ...

  6. Python 实现整数线性规划:分枝定界法(Branch and Bound)

    今天做作业,要实现整数线性规划的分枝定界法算法.找了一些网上的博客,发现都很屎,感觉自己写的这个比较清楚.规范,所以在此记录.如有错误,请指正. from scipy.optimize import ...

  7. RNG牛掰!

    2018-05-21 RNG牛掰!Uzi圆梦! 不说了,先去哭了! 2018-07-08 洲际赛后更新,RNG依然牛逼! 2018-08-30 亚运后后更新,UZI加油! 2018-10-22 继续加 ...

  8. Github超棒资源汇总

    Awesome List 中文资源大全 经典编程书籍大全 免费的编程中文书籍索引 awesome-awesomeness-zh_CN https://github.com/jnv/lists awes ...

  9. 解决eclipse部署maven时,src&sol;main&sol;resources里面配置文件加载不到webapp下classes路径下的问题

    解决eclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题. 有时候是src/main/resources下面的,有时候是sr ...

  10. 你有哪些想要分享的 PyCharm 使用技巧?

    作者:Light Lin链接:https://www.zhihu.com/question/37787004/answer/75269463来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...