bzoj1528 [Poi2005]sam-toy Cars

Source: Internet
Author: User

Descriptionjasio is a three-year-old boy, he likes to play toys, he has n different toys, they are placed on a very high shelf so jasio can't get them. In order to have enough space in his room, there will be no more than K toys on the floor at any moment. Jasio playing with toys on the floor. Jasio's mother stayed with his son in the room. When Jasio wants to play with the other toys on the floor, he will take it himself, if he wants to play the toy on the shelf, his mother will help him to take it, when she took the toy, by the way will also put a toy on the floor on the shelf to allow enough space on the floor. His mother knows her own child so he can anticipate what toys Jasio want to play. So she wanted to try to make herself to the shelves to take the toys as little as possible, how to arrange the order of toys put? Input first row three integers: N, K, p (1 <= k <= n <= 100.000, 1 <= p <= 500.000), respectively, the total number of toys, the maximum number of toys on the floor and jasio the number of sequences he wants to play, The next P line each line describes a toy number indicating the toy Jasio wants to play. Output a number indicates how many toys Jasio's mother should take at least. Sample Input3 2 7
1
2
3
1
3
1
2
Sample Output4find out where each toy will reappear for the first time after Nex[i], and then delete the smallest I nex[i at the time the K is present .so just use a heap of maintenance.Rose Posture ... There is a kind of STL called matching heap
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <set> #include <map> #include <ctime> #include <ext/ Pb_ds/priority_queue.hpp> #define LL Long long#define INF 0x7ffffff#define pa pair<int,int> #define Pi 3.1415926535897932384626433832795028841971using namespace std;using namespace __gnu_pbds;inline ll read () {ll x=0,f=1;    Char Ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,k,p,now=1,ans;int a[500010];int lst[100010];int nex[500010];bool mrk[100010];__gnu_pbds::p riority_queue <pa    >q;int Main () {n=read (); K=read ();p =read ();    for (int i=1;i<=p;i++) a[i]=read ();    for (int i=1;i<=n;i++) lst[i]=p+1;        for (int i=p;i>=1;i--) {nex[i]=lst[a[i]];    Lst[a[i]]=i;    } ans=k; while (k&&now<=p) {Q.pusH (Make_pair (Nex[now],a[now]));        if (!mrk[a[now]]) k--;        Mrk[a[now]]=1;    now++;            } for (int i=now;i<=p;i++) {if (Mrk[a[i]) {Q.push (Make_pair (Nex[i],a[i]));        Continue        } while (!mrk[q.top (). Second]) Q.pop ();        int Fst=q.top (). First,cnd=q.top (). Second;        Q.pop ();        mrk[cnd]=0;        Q.push (Make_pair (nex[i],a[i));        Mrk[a[i]]=1;    ans++;    } printf ("%d\n", ans); return 0;}

  

bzoj1528 [Poi2005]sam-toy Cars

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.