洛谷题目链接:[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: (n) , (k) , (p) ( (1le kle nle 100 000) , (1le ple 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 1 to (n) ).
输出格式:
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
简述一下题意: 有n个玩具,要玩p次,每次要玩就要从架子上取,如果地上已经有这个玩具了,就不需要取.每次去架子上取玩具时可以更换一个玩具.现在要求出最少取玩具的次数.
首先来想一下如何才能尽量少的取玩具. 显然,如果一个玩具已经在地上了,就可以不用去取. 但是地上能放的玩具的数量有限,所以我们可以记录每种玩具下一次要被玩的时间next[i],并在需要更换玩具的时候换走当前队列中next[i]最大的那一个玩具.
就拿样例举个栗子吧:
1 2 3 1 3 1 2
- 首先肯定是要将1,2都放到地板上.
- 然后要玩3,但是此时地板上并没有3,必须要拿地板上的一个玩具去换3.
这个时候我们就要考虑是换1还是换2了. - 显然,如果换走了1,下次又会要玩1,这样很显然是不划算的.所以此时我们取走2,将3拿过来.
- 接下来几次要玩1,3都可以直接在地上拿.直到最后一次再把2换回来.
所以我们可以得到这样一个贪心策略:要换玩具的时候,换next[i]最远的那个玩具.
然后就是模拟一下入队出队情况,用优先队列维护一下next.
#include<bits/stdc++.h>
using namespace std;
const int N=500000+5;
const int M=100000+5;
int n, k, p;//inqueue times
int a[N], next[N];//next same toy's position
bool vis[M];//the toy is in the queue or not
int ans = 0;
struct cmp{
bool operator()(const int&x,const int &y){
return next[x] < next[y];
}
};
queue <int> q[M];
priority_queue <int,vector<int>,cmp> h;
int gi(){
int ans = 0, f = 1; char i = getchar();
while(i<'0'||i>'9'){if(i=='-')f=-1;i=getchar();}
while(i>='0'&&i<='9'){ans=ans*10+i-'0';i=getchar();}
return ans * f;
}
int main(){
//freopen("data.in","r",stdin);
n = gi(); k = gi(); p = gi();
for(int i=1;i<=p;i++) a[i] = gi(), q[a[i]].push(i);
for(int i=1;i<=p;i++){
q[a[i]].pop();
if(q[a[i]].empty()) next[i] = p+1;
else next[i] = q[a[i]].front();
}
for(int i=1;i<=p;i++){
if(!vis[a[i]]){
if(h.size() == k){
vis[a[h.top()]] = 0;
h.pop();
}
h.push(i); vis[a[i]] = 1; ans++;
}
else h.push(i), k++;
}
printf("%d
",ans);
return 0;
}