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 (1≤idi≤1091≤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 (1≤n,k≤2⋅105)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 (1≤idi≤1091≤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 (1≤m≤min(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.

这道题可以说我并不清楚我当时是否能写出来(因为我一开始并没有觉得写hard也算一题)不过后来直接看了题目的题解,就总结一下吧。

首先这道题是这样子的:如果某联系人已经在队列中,就啥也不做;如果不在,并且加入之前数量就等于k,最后一个联系人出队,再加进去新的联系人,如果小于k直接入队。

easy version真的简单啊,vector模拟:

#include <stdio.h>
#include <vector>
#include <set>
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++)//查找是否存在
		{
			if(*it == t)
				break;
		}
		if(it == v.end())//不存在(存在就什么都不做)
		{
			if(v.size() >= k)//size大于等于k
			{
				v.erase(v.begin());//删掉最前面的(我反向模拟题目里面的规则,及我的是先进排在前面,当然出去也是从最前开始出)
				v.push_back(t);//压入末尾
			}
			else
				v.push_back(t);//size小于k
		}
	}
	printf("%d\n", v.size());
	for(int i = v.size() - 1; i >= 0; i--)
	{
		printf("%d", v[i]);
		if(i)
			printf(" ");
	}
	return 0;
}

  当时对付hard version的2*105就肯定要超时了,所以采用STL一起上,queue模拟,set查找,vector最后存储,注意queue和set里面的元素一样,然后按顺序赋给vector。

#include <stdio.h>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
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没找到
		{
			if(s.size() >= k)//数量超了
			{
				int b = q.front();//记录要出去的元素
				q.pop();//队列除去该元素
				q.push(t);//队列进入新元素
				s.insert(t);//set进入新元素
				s.erase(b);//set删除要出去的元素
			}
			else
			{
				q.push(t);
				s.insert(t);
			}
		}
	}
	printf("%d\n", s.size());
	vector<int> v;
	while(!q.empty())
	{
		v.push_back(q.front());
		q.pop();
	}
	reverse(v.begin(), v.end());//因为queue的顺序和题目要求刚好相反,于是反向vector,因为queue不能反向
	for(int i = 0; i < v.size(); i++)
	{
		printf("%d", v[i]);
		if(i != v.size() - 1)
			printf(" ");
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jacobfun/p/11617158.html

时间: 2024-10-29 18:59:28

Codeforces#590(1234)——B2Social Network (hard version)的相关文章

Codeforces 1335E2 - Three Blocks Palindrome (hard version)

题面 题意/解题思路 直接延用 Easy 版本的想法即可 详解见上一篇博客Codeforces 1335E1 - Three Blocks Palindrome (easy version) 完整程序 (93ms/2000ms) #include<bits/stdc++.h> using namespace std; int ar[200050]; vector<int> v[210]; void solve() { int n,ans=0; cin>>n; for(i

Codeforces Round #590 (Div. 3) B2. Social Network (hard version)

链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard versions are constraints on n and k. You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most

Xamarin for Visual Studio 3.11.590 稳定版 破解补丁 Version 3

前提概要 全新安装请参考 安装 Xamarin for Visual Studio. Release Log 3.11.590 此版本是紧急修复(HotFix)版,重点改善了 build-tool 及 platform-tool 23rc 的适配状况 原版下载链接:VisualStudio_3.11.590.msi,度盘备份 Release Notes:简略.详细 破解说明 1.提升本地权限到最高,程序大小不限: release bundle, VS设计器等受限功能都可以正常使用. 2.Vers

Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between easy and hard versions is a number of elements in the array. You are given an array \(a\) consisting of \(n\) integers. The value of the \(i\)-th element

Codeforces 690 C3. Brain Network (hard) LCA

C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie consum

Educational Codeforces Round 15_C. Cellular Network

C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line

codeforces 702C C. Cellular Network(水题)

题目链接: C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same

SSD Network Architecture--keras version

这里的网络架构和论文中插图中的网络架构是相一致的.对了,忘了说了,这里使用的keras版本是1.2.2,等源码读完之后,我自己改一个2.0.6版本上传到github上面.可别直接粘贴复制,里面有些中文的解释,不一定可行的.#defint input shapeinput_shape = (300,300,3)#defint the number of classes num_classes = 21 #Here the network is wrapped in to a dictory bec

Codeforces 1172C2 Nauuo and Pictures (hard version) dp

Nauuo and Pictures (hard version 首先考虑简单版本的, 一个一个dp求出来, 分成三坨, 一坨当前要求照片, 一坨除了当前的喜欢的照片, 一坨除了当前的讨厌的照片. 单次dp   50 ^ 4 感觉hard的也挺简单的.. 我们先算出最后喜欢的照片的总w, 和讨厌的照片的总w, 然后每个的贡献就是在原先的w中所占的比例. #include<bits/stdc++.h> #define LL long long #define LD long double #de