The main topic: There are N toys, are placed on the shelf, the floor can put K, to play P-times toys, if not on the floor will go to the shelves, the floor is full to put back, the minimum number of operations
Greedy thought: Every time you put back the toys, choose the next time you play the most behind the toys put back
can use heaps to simulate this greedy process.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 500500using namespace Std;typedef pair<int,int> abcd;int n,k,p,cnt,ans;int a[m],last[100100],next[m];bool on_ Floor[100100];namespace PRIORITY_QUEUE{ABCD heap[m];int top;void Insert (abcd x) {Heap[++top]=x;int t=top;while (t>1 {if (heap[t]>heap[t>>1]) swap (heap[t],heap[t>>1]), T>>=1;elsebreak;}} void Pop () {heap[1]=heap[top--];int t=2;while (t<=top) {if (t<top&&heap[t+1]>heap[t]) ++t;if (Heap[t] >HEAP[T>>1]) swap (heap[t],heap[t>>1]), T<<=1;elsebreak;}} int main () {int i;cin>>n>>k>>p;for (i=1;i<=p;i++) {scanf ("%d", &a[i]), if (Last[a[i]]) next[ Last[a[i]]]=i;last[a[i]]=i;} for (i=1;i<=n;i++) if (Last[i]) next[last[i]]=0x3f3f3f3f;for (i=1;i<=p;i++) {if (On_floor[a[i]]) {Priority_Queue :: Insert (ABCD (next[i],a[i])); continue;} if (cnt==k) {on_floor[priority_queue::heap[1].second]=0; Priority_queue::P op ();--cnt;} Priority_queuE::insert (ABCD (next[i],a[i])); on_floor[a[i]]=1;++ans;++cnt;} Cout<<ans<<endl;return 0;}
Bzoj 1528 POI2005 Sam-toy Cars heap + greedy