Bzoj1528[poi2005]sam-toy Cars
BZOJ1826[JSOI2010] Cache Swapping
Test instructions
Jasio have n different toys, they are placed on a very high shelf, there will be no more than K toys on the floor. When Jasio wants to play with the other toys on the floor, he will take it himself, if he wants to play the toy on the shelf, his mother will help him to take it, when she took the toy, by the way will also put a toy on the floor on the shelf to allow enough space on the floor. His mother knew the child's P request by deciding which of the minimum number of times a toy would be put on his mother's shelf for each change. k≤100000,p≤500000. The types of toys in bzoj1826 need to be discretized.
Exercises
First, find the location of the next request for the same kind of toy for each request. After maintaining a priority queue for the current floor of the toys on the next request location, each time you change a toy, the next request to the maximum location of the popup.
Code (1528):
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5#include <Set>6 #defineInc (I,J,K) for (int i=j;i<=k;i++)7 #defineMAXN 5000108 #defineINF 0X3FFFFFFF9 using namespacestd;Ten OneInlineintRead () { A CharCh=getchar ();intf=1, x=0; - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); the returnf*x; - } - structnd{ - intV,id;BOOL operator< (ConstNd&a)Const{returnv<A.V;} + }; -Priority_queue<nd>Q; + intN,K,P,A[MAXN],NEXT[MAXN],TOT,ANS,LAST[MAXN];BOOLZSYE[MAXN]; A intMain () { atN=read (); K=read (); P=read (); Inc (I,1, p) a[i]=read (); -Inc (I,1, N) last[i]=p+1; for(inti=p;i>=1; i--) next[i]=last[a[i]],last[a[i]]=i; -Inc (I,1, p) { - if(!Zsye[a[i]]) { - if(tot<k) { -zsye[a[i]]=1; ans++; tot++; in if(Next[i]) Q.push ((nd) {next[i],a[i]});ElseQ.push (nd) {p+1, A[i]}); -}Else{ tozsye[a[i]]=1; ans++; nd x=q.top (); Q.pop (); zsye[x.id]=0; + if(Next[i]) Q.push ((nd) {next[i],a[i]});ElseQ.push (nd) {p+1, A[i]}); - } the}Else{ * Q.push (nd) {next[i],a[i]}); $ } Panax Notoginseng } -printf"%d", ans);return 0; the}
20161109
Bzoj1528[poi2005]sam-toy cars*&&bzoj1826[jsoi2010] Cache interchange