Greedy heap achieved - focus on thinking it

Jasio is a three year old little boy, playing with his favorite toy, he has n different toy, they are placed on a high shelf so Jasio not get them. To give him enough room space on the floor any time there will not be more than a toy k. Jasio playing with toys on the floor. Jasio 'of her mother to accompany his son in the room. when other toys on the floor Jasio want to play, he will himself get, if he want to play toys on the shelf, his mother would help him pick up when she took the toy, the way will be a toy on the floor and put the shelves so that there is enough space on the floor. his mother knew his children so he can anticipate what Jasio want to play toys, so she wanted to try to make themselves as much as possible to take fewer toys go on the shelf, put the toy should be how to arrange the order of it?

In the first line of the standard input there are three integers: nnn,kkk,ppp (1≤k≤n≤100 0001\le k\le n\le 100\ 0001kn100 000, 1≤p≤500 0001\le p\le 500\ 0001p500 000), separated by single spaces. These denote respectively: the total number of cars, the number of cars that can remain on the floor at once and the length of the sequence of cars which Johnny will want to play with. Each of the following ppp lines contains one integer. These integers are the numbers of cars Johnny will want to play with (the cars are numbered from 111 to nnn).

 

In the first and only line of the standard output one integer should be written - the minimal number of times his mother has to pick a car from the shelf.

 

Input # 1

2. 7. 3 
1 
2 
. 3 
1 
. 3 
1 
2 

Output # 1
4

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
const int maxn=6e5+10;
struct node
{
    int num,nxt;
    bool operator <(const node &x ) const
    {
        return nxt<x.nxt;//下一次出现的时间 
    }
};
priority_queue<node> q;
int t,n,k,p,a[maxn],head[maxn],nxt[maxn],chosen[maxn],ans;

int main()
{
    //scanf("%d",&t);
    //while(t--)
    //{
        ans=0;
        memset(head,0,sizeof(head));
        memset(nxt,0,sizeof(nxt));
        memset(chosen,0,sizeof(chosen));
        while(q.size()) q.pop();
        scanf("%d%d%d",&n,&k,&p);
        for(int i=1;i<=p;i++)
        {
            Scanf ( " % D " , A + I); 
        } 
        for ( int I = P; I> = . 1 ; i-- ) 
        { 
            IF (head [A [I]]) NXT [I] = head [A [I] ]; // position of the recording current position next occurrence of the 
            head [a [I]] = I; 
        } 
        for ( int I = . 1 ; I <= P; I ++) IF (! NXT [I]) NXT [I] + P = . 1 ; // not a position next assignment 
        for ( int I = . 1 ; I <= P; I ++ ) 
        { 
            IF (Chosen [a [I]]) // selected through 
            { 
                q.push ((Node) {A [I], NXT [I]}); // the next occurrence of lost in 
                Continue ; 
            } 
            IF (K) // there are places 
            { 
                q.push ((Node ) {a [I], NXT [I]}); 
                Chosen [a [I]] = . 1 ; 
                K -; ANS ++ ; 
            } 
            the else // location is full, to find the latest occurrence of a next take up, 
            {
                 the while (! Chosen [. q.top () NUM] && q.size ()) q.pop (); // find the first Sorted 
                Chosen [q.top () NUM.] = 0 ;
             //     COUT < <q.top () num << endl. ;
                if(q.size())q.pop();
                q.push((node){a[i],nxt[i]});
                chosen[a[i]]=1;
                ans++;
            }
        }
        printf("%d\n",ans);
    //}
    return 0;
}

To the number of less well, so play as long as possible, we want to know which one to get up, can not take the win frequently, so we have to compare the position of the next occurrence of the later emergence of the next, we can take this up, this is the thinking of this question of greed.

Guess you like

Origin www.cnblogs.com/WHFF521/p/11241443.html