Codeforces 1234B2

B2. Social Network (hard version)

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 recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).

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 n messages, the i-th message will be received from the friend with ID idi (1≤idi≤109).

If you receive a message from idi 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 idi on the screen):

Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend idi is not displayed on the screen.
The conversation with the friend idi 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 n messages.

Input

The first line of the input contains two integers n and k (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 n integers id1,id2,…,idn (1≤idi≤109), where idi is the ID of the friend which sends you the i-th message.

Output

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

In the second line print m integers ids1,ids2,…,idsm, where idsi should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n 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];
[2,1];
[3,2];
[3,2];
[1,3];
[1,3];
[2,1].
In the second example the list of conversations will change in the following way:

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

Solution

一共有n条信息,屏幕上只能显示k个好友的信息,如果该好友的信息已经在屏幕上则不发生任何变化,否则将最早的一条好友信息顶掉,新的好友信息进入屏幕。听大佬们都是set和map,我这种菜鸡还是选用了离散化+vis标记的方式。但是不知道为什么用结构体全局离散化的话会TLE,反而在模拟的过程中进行离散寻找能过???

AC_Code

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 3e5+5;

int a[maxn];
vector<int> v,q;
bool vis[maxn];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,k;
	cin >> n >> k;
	for(int i=0; i<n; i++) {
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()), v.end());
	for(int i=0; i<n; i++) {
		if(!vis[lower_bound(v.begin(),v.end(),a[i])-v.begin()]) {
			if(q.size() < k) {
				q.push_back(a[i]);
				vis[lower_bound(v.begin(),v.end(),a[i])-v.begin()] = true;
			} else {
				vis[lower_bound(v.begin(),v.end(),q[0])-v.begin()] = false;
				q.erase(q.begin());
				q.push_back(a[i]);
				vis[lower_bound(v.begin(),v.end(),a[i])-v.begin()] = true;
			}
		}
	}
	cout << q.size() << endl;
	for(int i=q.size(); i; i--) {
		cout << q[i-1] << " ";
	}
}

TLE_Code

#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 3e5+5;

struct node {
	int num,lisan;
} a[maxn];
vector<int> v;
vector<node> q;
bool vis[maxn];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,k;
	cin >> n >> k;
	for(int i=0; i<n; i++) {
		cin >> a[i].num;
		v.push_back(a[i].num);
	}
	sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()), v.end());
	for(int i=0; i<n; i++) {
		a[i].lisan = lower_bound(v.begin(),v.end(),a[i].num)-v.begin();
		if(!vis[a[i].lisan]) {
			if(q.size() < k) {
				q.push_back(a[i]);
				vis[a[i].lisan] = true;
			} else {
				vis[q[0].lisan] = false;
				q.erase(q.begin());
				q.push_back(a[i]);
				vis[a[i].lisan] = true;
			}
		}
	}
	cout << q.size() << endl;
	for(int i=q.size(); i; i--) {
		cout << q[i-1].num << " ";
	}
}

Source

Codeforces 1234B2 Social Network (hard version)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值