Bzoj 1528 POI2005 Sam-toy Cars heap + greedy

Source: Internet
Author: User

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] &GT;HEAP[T&GT;&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.