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

思路:

队列维护, map记录, 模拟即可.

代码:

#include <bits/stdc++.h>
using namespace std;

map<int, bool> Mp;
deque<int> Que;
int n, k;

int main()
{
    int id;
    scanf("%d%d", &n, &k);
    for (int i = 1;i <= n;i++)
    {
        scanf("%d", &id);
        if (!Mp[id])
        {
            Que.push_front(id);
            Mp[id] = true;
        }
        if (Que.size() > k)
        {
            int tmp = Que.back();
            Que.pop_back();
            Mp[tmp] = false;
        }
    }
    printf("%d\n", (int)Que.size());
    while (!Que.empty())
    {
        printf("%d ", Que.front());
        Que.pop_front();
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/11618493.html

时间: 2024-08-29 17:21:54

Codeforces Round #590 (Div. 3) B2. Social Network (hard version)的相关文章

Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)

This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In addition, k≤1000,n≤50k≤1000,n≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previou

Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造

链接:https://www.luogu.com.cn/problem/CF1243B2 题意:给你长度为n的两个字符串s和t,你可以最多进行2*n次操作,每次操作选择i和j,然后交换s[i]和t[j],问你能否使得两个字符串相同 构造方法:假如(0~i)部分s和t已经相等,在i位置时首先在(i+1~t.size()-1)里找有没有和t[i]相同的字符,如果找到,则交换s[i]和t[j],如果找不到,则在s中找,找到之后,先将s[j]与t[t.size()-1],交换,再将s[i]与t[t.si

Codeforces Round #590 (Div. 3) Editorial

Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同的价格,找出一个最小的可能值price,使得price * n >= sum,sum指的是原来商品价格的总和. 知识点:模拟 思路:求出sum/n向上取整即可,有两种方法.一是使用ceil()函数,但注意ceil()返回的是向上取整后的浮点数,所以要进行强制类型转换:二是直接向下取整,然后用if语句

Codeforces Round #590 (Div. 3)(e、f待补

https://codeforces.com/contest/1234/problem/A A. Equalize Prices Again 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int main(){ 5 int n,a; 6 int t; 7 cin>>t; 8 ll sum = 0,ans; 9 while(t--){ 10 cin>>n;sum = 0

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

Codeforces Round #595 (Div. 3)B2 简单的dfs

原题 https://codeforces.com/contest/1249/problem/B2 这道题一开始给的数组相当于地图的路标,我们只需对每个没走过的点进行dfs即可 #include <bits/stdc++.h> using namespace std;const int maxn=2e5+20;int a[maxn],b[maxn],c[maxn];int dfs(int pos,int step){//传递坐标与步数 if(b[pos]==1){//再次遇到b[pos],返回

Codeforces Round #590 (Div. 3)

D. Distinct Characters Queries Description You are given a string ss consisting of lowercase Latin letters and qq queries for this string. Recall that the substring s[l;r]s[l;r] of the string ss is the string slsl+1…srslsl+1…sr. For example, the subs

Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowercase Latin letters and q queries for this string. Recall that the substring s[l;r] of the string s is the string slsl+1-sr. For example, the substrings

Codeforces Round #590 (Div. 3) C. Pipes

链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists of two rows, each row consists of n pipes. The top left pipe has the coordinates (1,1) and the bottom right - (2,n). There are six types of pipes: two