题目链接: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.

cf 手机短信问题的更多相关文章

  1. Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结

    Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结 1. 验证码的前世今生11.1. 第一代验证码 图片验证码11.2. 第二代验证码  用户操作 ,比如 ...

  2. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 忘记密码功能改进、手机短信、电子邮件

    由于我们的系统接近有100000个用户账户,经常会有忘记密码的时候,用户多了,很小的一个功能,每天都会有很多人在用,每个功能都非常友善,会提高提系统的效率,提高用户体验. 一天最多能返回3次手机短信, ...

  3. C#.NET 大型信息化系统集成快速开发平台 - 手机短信开发接口 4.0

    可以批量发信息给手机,相同的信息发给多个手机号码的效果图 已发送手机短信列表 可以批量发手机的功能,可以把先有的待发信息列表,直接通过批量发送功能发出 这个是设置发送模板公式的功能展示,可以设置发送的 ...

  4. SNF开发平台WinForm之十二-发送手机短信功能调用-金笛-SNF快速开发平台3.3-Spring.Net.Framework

    1.调用前组装参数 2.调用发送信息服务脚本   .调用前组装参数: BaseSendTaskEntity entity = new BaseSendTaskEntity(); entity.Mess ...

  5. 利用java实现的一个发送手机短信的小例子

    今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看,写的不太相信,闲的没事,把他整理下来,以后可能用得着 JAVA发送手机短信,流传有几种方法:(1)使用webservi ...

  6. js手机短信按钮倒计时

    /*   120秒手机短信按钮倒计时   */    exports.sendmessage = function (name) {        var second = 120; $(name). ...

  7. C#简单实现发送手机短信

    偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2) ...

  8. Ios拦截手机短信程序

    引用 1.手机要越狱,没有越狱的话,下面的可以不用看了!  2.IOS 要5.0以上,4.xx的同上  首先,声明下!由于公司移动开发的项目中,需要根据手机的内容进行逻辑处理,也就是要实现手机短信拦截 ...

  9. Android 读取手机短信

    获取android手机短信需要在AndroidManifest.xml加权限: <uses-permission android:name="android.permission.RE ...

随机推荐

  1. java工作流系统jflow表单引擎字段扩展组件介绍

    关键词:工作流快速开发平台  工作流流设计  业务流程管理   asp.net 开源工作流  bpm工作流系统  java工作流主流框架  自定义工作流引擎 表单设计器  流程设计器 装饰类图片 用于 ...

  2. CentOS、Ubuntu的安装

    Linux使用最广泛的2个发行版:CentOS.Ubuntu. CentOS安全性高,常用作企业的服务器,Ubuntu常用作个人桌面. 常见的虚拟机有2个: VM VirtualBox,这个是Orac ...

  3. PMP--1.7 项目治理

    治理凌驾于管理之上 组织治理用于影响项目治理. 组织治理需要组织根据组织文化.项目类型和组织需求裁剪治理框架,适用于当前组织. 其实组织治理的内容,在项目管理初期不需要详细了解,组织治理的内容都是高层 ...

  4. npm常用命令和脚手架使用

    最近前端同学使用最多的莫过于vue,angualr,react等热门前端框架了.那么就避免不掉的使用npm命令,本人就经常因为这三个脚手架的使用而不得不百度相应的npm命令,不胜其烦,,,因此就整理一 ...

  5. docker入门整理(1)--安装

    1.安装批量命令: CentOS7操作系统下. 包含卸载旧版本.安装依赖包.添加最新Yum源.安装docker-ce最新稳定版本.启动docker等: sudo yum remove docker \ ...

  6. iMacros 入门教程-基础函数介绍(2)

    imacros 的 pos 参数是什么意思 position的缩写,如果有 2 个以上的元素共用完全相同的属性(比方说同一个小区的同一栋楼),这个 POS 的参数可以借由不同位置来帮助明确定位(也就是 ...

  7. DolphinScheduler源码分析

    DolphinScheduler源码分析 本博客是基于1.2.0版本进行分析,与最新版本的实现有一些出入,还请读者辩证的看待本源码分析.具体细节可能描述的不是很准确,仅供参考 源码版本 1.2.0 技 ...

  8. spss——定义变量

    在变量视图 1,名称:必须以文字.字母.@ 这三类命名, 不能以数字.特殊字符.spss保留字等命名 2,类型:数字.逗号.点.字符串等,(字符数) 3,宽度 4,小数位数 5,标签:对名称进一步解释 ...

  9. CSP-J2019被虐记

    -------------------------------------- (我菜到连背包都不会) ------------------------------------- 回首一想,今年的pj还 ...

  10. 剑指offer-面试题33-二叉搜索树的后序遍历序列-二叉树遍历

    /* 题目: 给定一个序列,判断它是否为某个二叉搜索树的后序遍历. */ /* 思路: 二叉搜索树:左子树<根节点<右子树. 序列的最右端为根节点,小于根节点的左半部分为左子树,大于根节点 ...