Social Network (easy version)

CodeForces 1234B1

这是一道我曾经训练时做过的一道题,为了应对上面的要求指标我不得不把它掏了出来(doge),这道题还是有点代表性的所以写个记录

一、题目内容

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

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 kk , 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.

输入格式
The first line of the input contains two integers n and k (1≤n,k≤200) — 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≤10^9), where idi is the ID of the friend which sends you the i -th message.

输出格式
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, ……, idsn, 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.

题意翻译
你知道了将来N次给你发送的消息,而聊天软件一次性只能显示与K(1≤K≤200)个人的聊天记录。

当收到一条消息时,如果与这个人的聊天记录显示在聊天软件中,就什么都不做(注意,不需要把当前聊天记录置顶);否则,如果当前已经显示了K个聊天记录,则删除最后一个聊天记录;添加与这个人的聊天记录在列表顶端,同时其他聊天记录下移一个位置。

询问在这N条消息发送之后,你的聊天软件最终会显示与哪几个人的聊天记录。

输入格式
第一行两个正整数N,K(1≤N,K≤200),表示N条发来的消息和一次性显示的聊天记录个数。

接下来一行N个正整数id[i] (1≤id[i]≤200),为第i条消息的发送者。

输出格式
第一行,输出最后屏幕上显示的聊天记录个数X。

接下来一行X个正整数,按顺序输出最后屏幕上显示的聊天记录的对象。

说明/提示: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.

输入输出样例

输入#1

7 2
1 2 3 2 1 3 2

输入#2

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

输出#1

2
2 1

输出#2

3
1 3 2

二、个人思路

跟QQ,微信差不多,屏幕只能容纳k个消息(如果你不往下拉),不同的是这道题规定新消息的会话窗口已经出现在屏幕里,不会置顶,只有屏幕外的会话窗口发出新消息,才会置顶并且把最下面的消息顶出去。也没有人工置顶。
这里首先用一个映射b来维护当前记录里存在的记录,每读入一个数据先判断是否在记录里,如果不在则写入并进行判断,代码如下:

#include<stdio.h>
#include<map>
using namespace std;
map<int,bool>b;
const int N=200020;
int a[N];
int main()
{int n,k,m=0,s=0,t,i;scanf("%d%d",&n,&k);//n条发来的信息和当前最多k条显示聊天记录for(i=0;i<n;i++){scanf("%d",&t);//有t个人发来消息if(!b[t])//如果之前没有这个人的消息{b[t]=true;//新增记录,写入a[++m]=t;if(m-s>k)//消息数超过显示数b[a[++s]]=false;//把最下面那一条顶下去}}printf("%d\n",m-s);//屏幕最后显示的消息条数while(m>s)printf("%d ",a[m--]);//聊天对象return 0;
}

---------------------------------------分割线-----------------------------------------

上面的代码是我还在是小蒟蒻的时候写的代码,后来成了大蒟蒻(悲)我发现用队列也可以做,这道题很符合队列先进先出的特点,若当前队列长度为 k,则队列第 k 个位置的元素出队,如果队列长度小于 k 并且 idi 没在队列中出现过,则 idi 将进入队列头部,其他的元素向后移一位,最后输出队列,下面贴个代码:

#include<iostream>
#include<queue>
#include<map>
using namespace std;
queue<int>q;//使用STL模板
map<int,bool>b;
int a[200020];
int main()
{int n,k,x;cin>>n>>k;for(int i=1;i<=n;i++){cin>>x;if(!b[x])//入队{b[x]=1;q.push(x);}if(q.size()>k)//出队{b[q.front()]=0;q.pop();}}int t=q.size();cout<<t<<endl;for(int i=1;i<=t;i++){a[i]=q.front();q.pop();}for(int i=t;i>=1;i--)cout<<a[i]<<" ";cout<<endl;return 0;
}

以上代码均在洛谷平台检验过,均AC

总结

这道题如果你熟悉算法而且理解题意之后就比较好做了,可以用一个数组或者一个队列来维持当前记录,根据题意进行模拟

Social Network (easy version)相关推荐

  1. CodeForces - 1234B1 Social Network (easy version)

    CodeForces - 1234B1 Social Network (easy version) 题目: The only difference between easy and hard vers ...

  2. CodeForces - 1234B2 Social Network (hard version)

    CodeForces - 1234B2 Social Network (hard version) 题目: The only difference between easy and hard vers ...

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

    题目链接 The only difference between easy and hard versions are constraints on n and k. You are messagin ...

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

    原题链接:https://codeforces.com/contest/1234/problem/B2 你正在一个流行的社交网络中通过智能手机发送信息.你的智能手机最多可以显示k个最近与朋友的对话.最 ...

  5. Social Network(运用map容器)

    题目链接: Social Network (hard version) 题目: 你正在一个流行的社交网络中通过智能手机发送信息.你的智能手机最多可以显示k个最近与朋友的对话.最初,屏幕是空的(即显示的 ...

  6. Social network Social computing(社会网络和社会计算)

    社会计算,英文为Social Computing,中文也译为社会性计算.社交计算.社交性计算等. 什么是社会计算?目前对 此还没有一个明确和公认的定义.笼统而言,社会计算是一门现代计算技术与社会科学之 ...

  7. CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)

    题目链接:点击查看 题目大意:交互题猜密码,设原密码为 xxx,猜的密码为 yyy,如果没猜到,密码会自适应变成 zzz,满足 x⊕z=yx \oplus z=yx⊕z=y ,最多猜 nnn 次 题目 ...

  8. PTA 06-图2 Saving James Bond - Easy Version (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/672 5-10 Saving James Bond - Easy Version   ( ...

  9. Codeforces Round #579 (Div. 3) F1. Complete the Projects (easy version) 排序 + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 比较直观的想法就是对于bi≥0b_i\ge0bi​≥0的项目,我们将aia_iai​从小到大排序,让后依次加bib_ibi​,如果有取不到的,显然就无 ...

最新文章

  1. 你有哪些deep learning(rnn、cnn)调参的经验?
  2. WPF中获取鼠标相对于屏幕的位置
  3. 转:Swing中的线程探究
  4. php auth_http,php auth_http类库进行身份效验
  5. Android自定义控件之轮播图控件
  6. 北大博士干了半年外卖骑手,写出AI伦理论文登上顶刊,“系统知道一切”
  7. 105. 七夕祭【环形均分纸牌问题】
  8. windows配置samba客户端_如何搭建与Windows客户机结合使用的Samba文件服务器?
  9. Java IdentityHashMap putAll()方法与示例
  10. 剑指 Offer II 055. 二叉搜索树迭代器
  11. matlab命令及海洋作图
  12. 超级详细的pytest测试和allure测试报告
  13. 【TL431】TL431精密电压调节器简介
  14. 给敏捷软件开发的26条建议
  15. 猫哥教你写爬虫 019--debug-作业
  16. 阿里云服务器怎么更换ip?
  17. 读书笔记--项亮《推荐系统实践》第四章
  18. Python数据分析之商品数据分析
  19. java格式化XML文件
  20. 涛思数据加入龙蜥社区,携手共建时序数据库生态

热门文章

  1. 解放双手,自动刷抖音
  2. JavaScript 限制文本框不可输入英文单双引号
  3. Smartbi电子表格故事之用数据进行销售问题分析
  4. FFmpeg之视频解码
  5. Geoserver学习笔记-3、服务标准(WFS)
  6. 养老变身“坑老”? 找家靠谱养老机构为何这么难
  7. 2020年锅炉压力容器压力管道安全管理(全部)考试及锅炉压力容器压力管道安全管理(全部)考试内容
  8. “东华杯”2021年大学生网络安全邀请赛 暨第七届上海市大学生网络安全大赛线上赛MISC-Writeup
  9. oracle nlog,C#使用NLog记录日志
  10. Python字典常见操作方法 【增加、删除、修改】