Codeforces#590(1234)——B2Social Network (hard version)

B2. Social Network (hard version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The only difference between easy and hard versions are constraints on nn and kk.

You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most kk most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 00).

Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.

You (suddenly!) have the ability to see the future. You know that during the day you will receive nn messages, the ii-th message will be received from the friend with ID idiidi (1idi1091≤idi≤109).

If you receive a message from idiidi in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.

Otherwise (i.e. if there is no conversation with idiidi on the screen):

  • Firstly, if the number of conversations displayed on the screen is kk, the last conversation (which has the position kk) is removed from the screen.
  • Now the number of conversations on the screen is guaranteed to be less than kk and the conversation with the friend idiidi is not displayed on the screen.
  • The conversation with the friend idiidi appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.

Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all nn messages.

Input

The first line of the input contains two integers nn and kk (1n,k2105)1≤n,k≤2⋅105) — the number of messages and the number of conversations your smartphone can show.

The second line of the input contains nn integers id1,id2,,idnid1,id2,…,idn (1idi1091≤idi≤109), where idiidi is the ID of the friend which sends you the ii-th message.

Output

In the first line of the output print one integer mm (1mmin(n,k)1≤m≤min(n,k)) — the number of conversations shown after receiving all nn messages.

In the second line print mm integers ids1,ids2,,idsmids1,ids2,…,idsm, where idsiidsi should be equal to the ID of the friend corresponding to the conversation displayed on the position ii after receiving all nn messages.

Examples

input

 
7 2
1 2 3 2 1 3 2

output

 
2
2 1 

input

 
10 4
2 3 3 1 1 2 1 2 3 3

output

 
3
1 3 2 

Note

In the first example the list of conversations will change in the following way (in order from the first to last message):

  • [][];
  • [1][1];
  • [2,1][2,1];
  • [3,2][3,2];
  • [3,2][3,2];
  • [1,3][1,3];
  • [1,3][1,3];
  • [2,1][2,1].

In the second example the list of conversations will change in the following way:

  • [][];
  • [2][2];
  • [3,2][3,2];
  • [3,2][3,2];
  • [1,3,2][1,3,2];
  • and then the list will not change till the end. 

 This question can be said that I do not know if I was able to write it (because I did not start writing that can be considered a hard problem) but then looked directly subject solution to a problem, then summarize it.

First, this question is like this: If a contact already in the queue, they do what is not; if not, and before joining the number is equal to k, the last contact the team, together into a new contact, If k is less than directly into the team.

easy version is really simple ah, vector simulation:

#include <stdio.h> 
#include <Vector> 
#include <SET> 
the using namespace STD; 
Vector <int> V; 
int main (void) 
{ 
	int n-, K, T; 
	Scanf ( "% D% D", & n- , & K); 
	for (int I = 0; I <n-; I ++) 
	{ 
		Scanf ( "% D", & T); 
		Vector <int> :: Iterator IT = v.begin (); 
		for (IT; IT =! v.end (); it ++) // find out if any 
		{ 
			iF (t == * IT) 
				BREAK; 
		} 
		iF (IT == v.end ()) // absent (do nothing) 
		{ 
			iF (v.size ()> = k) // size greater than or equal K 
			{ 
				v.erase (v.begin ()); // delete the foremost (I backward simulation rules inside the subject, and I is an advanced top surface, of course, also starts out from the front most) 
				v.push_back (T);// end pressed 
			} 
			the else 
				v.push_back (T); // size less than k
		}
	}
	printf("%d\n", v.size());
	for(int i = v.size() - 1; i >= 0; i--)
	{
		printf("%d", v[i]);
		if(i)
			printf(" ");
	}
	return 0;
}
		

  At that time against the hard version of 2 × 10 5 would certainly be out of time, so the use of STL on together, queue simulation, set look, vector finally stored, attention queue and set elements inside the same, then the order is assigned to vector.

#include <stdio.h> 
#include <Queue> 
#include <SET> 
#include <Vector> 
#include <algorithm> 
the using namespace STD; 
Queue <int> Q; 
SET <int> S; 
int main (void) 
{ 
	int n-, K, T; 
	Scanf ( "% D% D", & n-, & K); 
	for (int I = 0; I <n-; I ++) 
	{ 
		Scanf ( "% D", & T); 
		IF (s.find ( t) == s.end ()) // set not found 
		{ 
			IF (s.size ()> = K) over the number of // 
			{ 
				int q.front B = (); // record to go out of the element 
				q .pop (); // removes the queue element 
				q.push (t); // enters a new queue element 
				s.insert (t); // set the new element 
				s.erase (b);// set out to remove an element 
			} 
			the else 
			{ 
				q.push (T);
				s.insert (T); 
			} 
		} 
	} 
	the printf ( "% D \ n-", s.size ()); 
	Vector <int> V; 
	(! q.empty ()) the while 
	{ 
		v.push_back (q.front ( )); 
		q.pop (); 
	}	 
	reverse (v.begin (), v.end ()); // queue as the subject sequence requirements and the opposite, reverse Vector Thus, because the queue can not reverse 
	for (int 0 = I; I <v.size (); I ++) 
	{ 
		the printf ( "% D", V [I]); 
		! IF (I = v.size () -. 1) 
			the printf ( ""); 
	} 
	return 0 ; 
}
		

  

Guess you like

Origin www.cnblogs.com/jacobfun/p/11617158.html