• 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
    ", (int)Que.size());
        while (!Que.empty())
        {
            printf("%d ", Que.front());
            Que.pop_front();
        }
    
        return 0;
    }
    
  • 相关阅读:
    一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
    智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
    一种单片机支持WiFi的应用——SimpleWiFi在单片机中的应用
    TI推出SimpleLink低能耗蓝牙CC2541
    SimpleWiFi模块评估板
    Android架构设计和软硬整合完整训练
    CentOS上解压ZIP乱码的解决办法
    更具体的描述JNI
    数据市中心全省中国mysql脚本
    几种方法枚举子集
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11618493.html
Copyright © 2020-2023  润新知