[bzoj1826] [JSOI2010]缓存交换

  虽然不知道为什么。。但显然,每次扔掉离下次查询最远的内存单元就行了233

  用堆来维护贪心。。。(优先队列大法好

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<queue>
 8 #define d double
 9 using namespace std;
10 const int maxn=100233;
11 const int inf=1002333333;
12 struct zs{
13     int v,pos;
14 }a[maxn];
15 struct zzs{
16     int next,v;
17 };
18 priority_queue<zzs>q;
19 int b[maxn],next[maxn];
20 int i,j,k,n,m,ans;
21 bool inq[maxn];
22 
23 int ra;char rx;
24 inline int read(){
25     rx=getchar(),ra=0;
26     while(rx<'0'||rx>'9')rx=getchar();
27     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra;
28 }
29 bool cmp(zs a,zs b){return a.v==b.v?a.pos<b.pos:a.v<b.v;}
30 
31 bool operator <(zzs a,zzs b){
32     return a.next<b.next;
33 }
34 int main(){
35     n=read(),m=read();
36     for(i=1;i<=n;i++)a[i].v=read(),a[i].pos=i;
37     sort(a+1,a+1+n,cmp);int last=1,cnt=0;
38     for(i=1;i<=n;i++)if(a[i].v!=a[i+1].v||i==n){
39         cnt++;
40         for(j=last;j<i;j++)
41             next[a[j].pos]=a[j+1].pos;
42         next[a[i].pos]=inf;
43         for(j=last;j<=i;j++)b[a[j].pos]=cnt;
44         last=i+1;
45     }
46 //    for(i=1;i<=n;i++)printf("  %d",b[i]);puts("");
47     int sz=0;zzs tmp;
48     for(i=1;i<=n;i++){
49         if(sz==m&&!inq[b[i]]){
50             while(!q.empty()&&!inq[q.top().v])q.pop();
51             if(q.empty())continue;
52             tmp=q.top(),q.pop();
53             sz--;//printf("%d out:%d
",i,tmp.v);
54             inq[tmp.v]=0;
55         }
56         if(!inq[b[i]])sz++,inq[b[i]]=1,ans++;
57         q.push((zzs){next[i],b[i]});
58     }
59     printf("%d
",ans);
60     return 0;
61 }
View Code
原文地址:https://www.cnblogs.com/czllgzmzl/p/5301522.html