Sam-toy Cars

Source: Internet
Author: User

Title Description

Jasio 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 output Format input format:

The first line is three integers: n,k,p (1≤k≤n≤100,000,1≤p≤500000), which represents the total number of toys, the maximum number of toys on the floor, and the number of Jasiojasio he wants to play with, and then the PP line describes a toy number for each line Jasio Want to play with the toys.

Output format:

A number indicates how many times a Jasiojasio's mother should take a toy.

Input and Output Sample input example # #:
3 2 71231312
Sample # # of output:
4
Ideas Luogu have been unable to AC children see here: Do not submit SP688, submit P3419 [Poi2005]sam-toy cars do not submit SP688, submit P3419 [Poi2005]sam-toy cars do not submit SP688, Submit P3419 [Poi2005]sam-toy Cars

As for why, I do not know ... Anyway I card 3 times submitted on Luogu (manual funny)

This problem uses greedy + heap optimization.

Greed for:

Each time you enter a number, a number in the set of K will pop up to allow that number to be added.

Of course, if the number is already in the collection, you can switch directly to the next number.

The first k numbers can also be entered directly into the set.

Is that the problem? Which of the numbers in the collection can be optimal?

Of course the next time the distance is the farthest from the number.

Heap Optimization:

priority_queue<int> q;

is a big pile.

Then the problem needs to be modified in the heap.

priority_queue<pair<int,int> >q;

The next occurrence of the previous storage node of the pair, and the second store the value of the node.

This will automatically sort out the number that is farthest away.

So each operation only needs to take q.top () out and then remove the Head element--q.pop ().

Finally, on the code:

#include <algorithm>#include<bitset>#include<complex>#include<deque>#include<exception>#include<fstream>#include<functional>#include<iomanip>#include<ios>#include<iosfwd>#include<iostream>#include<istream>#include<iterator>#include<limits>#include<list>#include<locale>#include<map>#include<memory>#include<New>#include<numeric>#include<ostream>#include<queue>#include<Set>#include<sstream>#include<stack>#include<stdexcept>#include<streambuf>#include<string>#include<typeinfo>#include<utility>#include<valarray>#include<vector>#include<cstring>#include<cmath>#definell Long Long#defineFhakioi intusing namespaceStd//end of header fileinline ll read () {LL ret=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-f;ch=GetChar ();}  while(ch>='0'&&ch<='9') ret=ret*Ten+ch-'0', ch=GetChar (); returnret*F;}//end of Reading excellentInlinevoidWrite (ll ZX) {if(zx<0) {Zx=-zx;putchar ('-');} if(zx<Ten) Putchar (zx+'0'); Else{Write (ZX/Ten); Putchar (ZX%Ten+'0'); }}//end of the optimization of transmissionll n,k,p,a[500010];ll use[100010];ll f[500010];ll las[500010],ans;ll nxt[500010];p riority_queue<pair<int,int> > Q;//HeapFhakioi Main () {n=read (); K=read ();p =read ();  for(LL i=1; i<=p;i++) A[i]=read (), las[a[i]]=p+1;//Read in     for(LL i=p;i>=1; i--) {Nxt[i]=Las[a[i]]; Las[a[i]]=i; }//the distance between pre-processing and the next occurrence     for(intI=1; i<=p;i++){        if(use[a[i]]!=0) {Q.push (Make_pair (nxt[i],a[i));//It's already in the collection.}Else if(k!=0){//number of First kk--; Ans++; Use[a[i]]=1;        Q.push (Make_pair (nxt[i],a[i)); }Else{//in the last case, you need to pop a number             while(!use[q.top (). Second]) Q.pop ();//not currently in the collection            intX=q.top (). Second;//take the farthest distance.Q.pop (); USE[X]=0; Ans++;//ans plus oneuse[a[i]]=1;        Q.push (Make_pair (nxt[i],a[i)); }} write (ans);p Utchar ('\ n');/Outputreturn 0;}

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.