题意描述半天描述不好,直接粘贴了
Now your team is participating a programming contest whose rules are slightly different from ICPC. This contest consists of N problems, and you must solved them in order: Before you solve the (i+1)th problem, you must solve the ith problem at first. And solving the ith problem requires a specified code template Ti.
You
are allowed to hold M code templates only. At the beginning of the
contest, your are holding templates numbered 1, 2, ..., M. During the
contest, if the problem you are trying to solve requires code template Ti, and Ti is happened at your hand (i.e, one of the M code templates you are holding is Ti), you can solve it immediately. On the other hand, if you are not holding Ti,
you must call your friends who are outside the arena for help (yes, it
is permitted, not cheating). They can give you the code template you
need. Because you are only allowed to hold M code templates, after
solving current problem, you must choose to drop the code you get from
your friends just now, or to keep it and drop one of the M templates at
your hand previously.
1 /* 2 HDU 4398 3 G++ 156ms 2868K 4 贪心,维护一个M个元素的集合,根据当前位置的元素的 5 下一个位置选择,删除下一个位置最远的元素 6 7 */ 8 9 #include<stdio.h> 10 #include<iostream> 11 #include<string.h> 12 #include<algorithm> 13 #include<queue> 14 #include<map> 15 #include<set> 16 using namespace std; 17 const int MAXN=100010; 18 19 int Ti[MAXN]; 20 int next[MAXN]; 21 map<int,int>mp; 22 23 struct Node 24 { 25 int next_id; 26 int ti; 27 }; 28 struct classcomp 29 { 30 bool operator()(const Node &a,const Node &b)const 31 { 32 return a.next_id<b.next_id;//从小到大排序 33 } 34 };//这个逗号别忘记 35 multiset<Node,classcomp>T_info; 36 multiset<Node>::iterator it_n; 37 set<int>Te; 38 set<int>::iterator it; 39 40 int main() 41 { 42 // freopen("in.txt","r",stdin); 43 // freopen("out.txt","w",stdout); 44 int n,m; 45 while(scanf("%d%d",&n,&m)==2) 46 { 47 for(int i=1;i<=n;i++) 48 scanf("%d",&Ti[i]); 49 mp.clear();//清空map 50 for(int i=n;i>=1;i--)//从后往前扫描 51 { 52 if(mp[Ti[i]])//出现过 53 next[i]=mp[Ti[i]]; 54 else next[i]=n+1; 55 mp[Ti[i]]=i; 56 } 57 Te.clear(); 58 T_info.clear(); 59 for(int i=1;i<=m;i++)//先把前面带的m个模板入set 60 { 61 if(!mp[i])mp[i]=n+1; 62 Node temp; 63 temp.next_id=mp[i]; 64 temp.ti=i; 65 T_info.insert(temp); 66 Te.insert(i); 67 } 68 int ans=0; 69 for(int i=1;i<=n;i++) 70 { 71 it=Te.find(Ti[i]); 72 if(it!=Te.end()) 73 { 74 Node temp; 75 temp.next_id=i; 76 temp.ti=Ti[i]; 77 T_info.erase(temp); 78 temp.next_id=next[i];//更新 79 T_info.insert(temp); 80 } 81 else 82 { 83 ans++; 84 it_n=T_info.end(); 85 it_n--; 86 if(next[i]<(*it_n).next_id) 87 { 88 Te.erase((*it_n).ti); 89 T_info.erase(it_n); 90 Te.insert(Ti[i]); 91 Node temp; 92 temp.next_id=next[i]; 93 temp.ti=Ti[i]; 94 T_info.insert(temp); 95 } 96 } 97 } 98 printf("%d ",ans); 99 100 } 101 return 0; 102 }