Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<set>
- #include<algorithm>
- #include<map>
- using namespace std;
- int n,m,v[100100],sol;
- map <int,int> first; //first[x]=urmatoarea pozitie pe care apare x in lista
- map <int,bool> hand; //hand[x]=true daca il am pe x in grupul curent
- int next[100100]; //next[i]=pozitia urmatoare pe care se afla elementul v[i]
- struct Element{
- int y;
- Element(int yy)
- {
- y=yy;
- }
- bool operator <(const Element &A) const
- {
- return first[y]>first[A.y];
- }
- };
- set <Element> H;
- int main()
- {
- int i,x;
- set <Element>::iterator it;
- while(cin>>n)
- {
- cin>>m;
- sol=0;
- for(i=1;i<=n;i++)
- cin>>v[i];
- // construiesc vectorul next pentru v
- // si vectorul first pentru grupul 1,2,...,m
- for(i=1;i<=m;i++)
- first[i]=n+1;
- for(i=n;i>0;i--)
- {
- if(first[v[i]])
- next[i]=first[v[i]];
- else
- next[i]=n+1;
- first[v[i]]=i;
- }
- // initializez grupul 1,2,...,m
- for(i=1;i<=m;i++)
- {
- hand[i]=true;
- H.insert(Element(i));
- }
- // rezolvarea in sine
- for(i=1;i<=n;i++)
- {
- if(!hand[v[i]]) // nu am elementul in grup
- {
- sol++; // chem un prieten
- first[v[i]]=next[i];
- hand[v[i]]=true;
- H.insert(Element(v[i])); // inserez elementul primit
- it=H.begin();
- x=(*it).y;
- hand[x]=false;
- H.erase(it); // sterg elementul care apare cel mai departe
- }
- else // am deja elementul in grup
- {
- H.erase(H.find(Element(v[i])));
- first[v[i]]=next[i];
- H.insert(Element(v[i]));
- }
- }
- cout<<sol<<"\n";
- first.clear();
- hand.clear();
- H.clear();
- for(i=1;i<=n;i++)
- next[i]=0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement