洛谷 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,用\ ...
随机推荐
- git pull 失败 ,提示:fatal: refusing to merge unrelated histories
首次 git pull 时失败,并提示:fatal: refusing to merge unrelated histories 在使用git pull 命令时,添加一个可选项 git pull or ...
- [国家集训队]最长双回文串 (PAM)回文自动机
Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #include <cstr ...
- appium ios端自动化测试配置
一.安装环境介绍macOS 10.12.4 Xcode 8.3.2 适用机型:iOS9 及以上机型 二.Appium源码安装Xcode升级8.2之后不再支持UIAutomation,转而使用XCUIT ...
- python3 之 Ellipsis
在翻django 代码的时候无意中看到的, 主要还是在注解时候使用 官方参考:https://docs.python.org/3/library/constants.html#Ellipsis 注意: ...
- NOIp模拟赛三十一
持续降智 分数:100+0+0=100 C题subtask是假的,根本没有部分分中的情况...还我20分QAQ A:[BZOJ4444]国旗计划 B:[agc006f]blackout C:[arc0 ...
- 捕捉soap的xml形式
下面是我以前对Php的soap接口进行抓包分析出的结果,这个分析在当服务端或者客户端的Php没有安装soap模块时,可以使用构建xml的方式实现相同的功能 服务端: $data = $HTTP_RAW ...
- JZOJ5787轨道(容斥+DP)
JZOJ5787轨道 Description 2018年1月31日,152年一遇的超级大月全食在中国高空出现(没看到的朋友真是可惜),小B看到月食,便对月球的轨道产生了兴趣.他上网查重力加速度的公式, ...
- 批量删除harbor中的镜像
一 说明 这个是我第一篇博客,所以我想放上原创的东西,尽管我一直都很担心自己写得太low,但是总要学会尝试,学会改变自己,相信自己.在写这个脚本时,由于我接触LInux不是很多,能力有限,仅仅是为了让 ...
- hadoop 2.6.0 LightWeightGSet源码分析
LightWeightGSet的作用用一个数组来存储元素,而且用链表来解决冲突.不能rehash.所以内部数组永远不用改变大小.此类不支持空元素. 此类也不是线程安全的.有两个类型參数.第一个用于查找 ...
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...