题目链接:https://vjudge.net/contest/331120#problem/C

题目:你有一部手机,最多显示k个人发的信息,现在收到n条信息,有可能人是相同的人发的。最新的要顶置,当屏幕上显示的人数已经有k个时,

如果此时又新的人发信息,则把显示屏末端的会消失,最新的顶置。

输入:第一行:n,k     n表示短信的条数,k表示屏幕最多显示的人的个数

第二行:有n个数字,分别表示发短信的人,可以相同。

输出:屏幕上的人数 、  最终自顶向下显示在屏幕上的朋友

代码:

#include<iostream>
#include<set>
#include<deque>
using namespace std;
set<int>s;
deque<int>m;
int main() {
 int n,k;
 cin>>n;
 cin>>k;
 for(int i=0; i<n; i++) {
  int j;
  cin>>j;
  if(!s.count(j)) {
   m.push_front(j);
   s.insert(j);
  }
  if(m.size()>k) {
   s.erase(m.back());
   m.pop_back();
  }
 }
 int sum;
 sum=m.size();
 cout<<sum<<endl;
 for(int i=0; i<sum; i++) {
  cout<<m[i]<<" ";
 }
 return 0;
}

输入n k
for(1~n)  循环读入n个朋友
对于每个朋友x
首先用set的count函数set.count(x)判断之前有没有出现过
如果出现过,直接continue跳过,不做任何处理
如果没有出现过,把这个朋友x放入队头deque.push_front(x)
把这个数放入set,set.insert(x) 表示朋友x出现过,然后,
判断deque的size是否大于k,deque.size()
如果大于k,在set中删除deque的队尾那个数,set.erase(deque.back())
然后把deque队尾删除掉,deque.pop_back();
 最后输出deque

原文

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.

最新文章

  1. 用Linux命令wget进行整站下载
  2. iOS 23 种设计模式
  3. sdut 2819 比赛排名(边表 拓扑排序)
  4. 翻译:ECMAScript 5.1简介
  5. 统计难题 HDOJ--2222
  6. TFS2010中文版安装
  7. jsp if else c标签 总结
  8. SilkTest天龙八部系列6-用open agent进行测试
  9. javascript alert,confirm,prompt弹框用法
  10. 瞎j8封装第二版之数据层的封装
  11. Intellij IDEA 生成返回值对象快捷键
  12. Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in another process or thread is using this directory.
  13. VueJs(10)---vue-router(动态路由,嵌套式路由,编程式路由)
  14. 使用xlrd模块操作Excel
  15. AnguarJS中链式的一种更合理写法
  16. Python 操作 mysql数据库的一个小小的基础案例,小白新手,以备后用~~
  17. C# 连接PDA扫码枪
  18. 为什么用freemarker视图?
  19. linux cfs调度器_模型实现
  20. go_组合接口

热门文章

  1. C语言基础五 数组
  2. go每日新闻--2020-02-19
  3. Ajax0002: 省市县三级联动案例
  4. 【React Native】在网页中打开Android应用程序
  5. .net平台 通过Windows installer 打包和部署实例操作
  6. .NET CORE(C#) WPF亚克力窗体
  7. 关于hp proliant sl210t服务器raid 1阵列配置(HP P420/Smart Array P420阵列卡配置)
  8. eclipse一直不停building workplace
  9. 修改 div 的滚动条的样式
  10. nginx: [warn] conflicting server name &quot;aaa.7yule.cn&quot; on 0.0.0.0:80, ignored