day7-Example 1 |
Difficulty level: B; run time limit: 1000ms; operating space limit: 256000KB; code length limit: 2000000B |
Question Description |
In a computer, the CPU can only exchange data directly with cache caches. When the required memory unit is not in the cache, the data needs to be transferred from the main storage into the cache. At this point, if the cache capacity is full, you must first remove one from it. For example, the current cache capacity is 3, and there are already primary cells numbered 10 and 20. At this point, the CPU accesses the primary deposit with the number 10, and the cache hits. Then, the CPU accesses the primary cell with the number 21, so simply move the primary deposit into the cache, causing a deletion (Cache Miss). Then, the CPU accesses the primary cell with the number 31, you must swap out a chunk from the cache in order to move the main deposit number 31 into the cache, assuming we move out the primary deposit Number 10. Then, once again, the CPU accesses the primary deposit number of 10, causing a loss. We have seen that if you delete other cells at the time of the last deletion, you can avoid the absence of this visit. In modern computers, the LRU (least recently used) algorithm is often used for cache scheduling-but, as can be seen from the previous example, this is not an optimal algorithm. For a fixed-capacity empty cache and a number of contiguous main memory access requests, Cong wants to know how to swap out the correct primary storage element each time the cache is missing in order to achieve a minimum number of cache misses. |
Input |
The first line of the input file contains two integers N and M (1<=m<=n<=100,000), each representing the number of main memory accesses and the cache capacity. The second line contains n spaces separated by a positive integer, and the number of each main memory block (no more than 1,000,000,000) is given in order of the access request. |
Output |
The output line is the minimum number of cache misses. |
Input example |
6 2 1 2 3 1 2 3 |
Output example |
4 |
Problems: There is a "very obvious" conclusion: Nest Meng to delete the next occurrence of the collection of time as far as possible after the element is very good = =, so get a heap maintenance on the line ... There is also a tip: the first situation in the discussion is as long as it is added, because next is obviously incremental and will not have access to the original point and will not have to be deleted.
(Too lazy to write with set treap= =)
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <stack>6#include <queue>7#include <cstring>8 #definePAU Putchar (")9 #defineENT Putchar (' \ n ')Ten #defineCH for (int d=0;d<2;d++) if (Ch[d]) One using namespacestd; A Const intmaxn=100000+Ten, maxnode=200000+Ten, inf=-1u>>1; - structdata{intNxt,p;} X[MAXN]; - BOOL operator< (ConstData&a,ConstDATA&B) {returna.nxt<b.nxt;} thepriority_queue<data>q;intNEXT[MAXN],A[MAXN]; -InlineintRead () { - intx=0, sig=1;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; + for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; - returnsig?x:-x; + } AInlinevoidWriteintx) { at if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; - for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } - structnode{ -node*ch[2];intr,siz,v; in voidInit () {R=rand (); siz=1; ch[0]=ch[1]=NULL;} - voidUpdate () {siz=1; Ch{siz+=ch[d]->siz;}return;} to}treap[maxnode],*nodecnt=treap,*root;queue<node*>R; +Node*neawnode () {node*k;if(R.empty ()) k=nodecnt++;ElseK=r.front (), R.pop (); K->init ();returnK;} - voidDel (node*&x) {r.push (x); x=null;return;} the voidRotate (node*&x,intd) { *node*k=x->ch[d^1];x->ch[d^1]=k->ch[d];k->ch[d]=x;x->update (); K->update (); x=k;return; $ }Panax Notoginseng voidInsert (Node*&x,intv) { - if(!x) X=nodecnt++,x->init (), x->v=v; the Else{intD=v>x->v;insert (x->ch[d],v); + if(x->r<x->ch[d]->r) Rotate (x,d^1);ElseX->update (); A}return; the } + voidRemove (Node*&x,intv) { - if(x->v==v) { $ if(x->ch[0]&&x->ch[1]){ $ intd=x->ch[0]->r>x->ch[1]->R; -Rotate (x,d); Remove (x->ch[d],v); -}Else{node*k;x=x->ch[0]?x->ch[0]:x->ch[1];d El (k);} the}ElseRemove (x->ch[v>x->v],v); - if(x) x->update ();return;Wuyi } the voidPrint (node*x) { - if(!x)return;p Rint (x->ch[0]);p rintf ("%d", x->v);p rint (x->ch[1]);return; Wu } - BOOLNum (node*x,intv) { About if(!x)return false; $ if(X->V==V)return true; - if(V>X->V)returnNum (x->ch[1],v); - Else returnNum (x->ch[0],v); - } A intN,M,B[MAXN]; + intMain () { the intTP; -N=read (); m=read (); $ for(intI=1; i<=n;i++) {x[i].nxt=read (); x[i].p=i;} theSort (x+1, x+n+1);intCur=0; the for(intI=1; i<=n;i++){ the if((i==1)|| (x[i].nxt!=x[i-1].NXT)) cur++; a[x[i].p]=cur; the } - for(inti=n;i;i--){ in if(b[a[i]]==0) next[i]=n+1; the Elsenext[i]=B[a[i]]; theb[a[i]]=i; About}intans=0; the for(intI=1; i<=n;i++){ the if(root&&num (root,a[i])) Q.push (data) {Next[i],a[i]}); the Else if(!root| | Root->siz<m) {Q.push (data) {Next[i],a[i]}); Insert (Root,a[i]); ans++;} + Else{Remove (Root, (Q.top ()). P); Q.pop (); Q.push (data) {Next[i],a[i]}); Insert (Root,a[i]); ans++;} - }write (ans); the return 0;Bayi}
Coj 2108 day7-Example 1