• Codeforces 704A Thor 队列模拟


    题目大意:托尔有一部手机可执行三种操作

    1.x APP产生一个新消息

    2.读取x App已产生的所有消息

    3.读取前t个产生的消息

    问每次操作后未读取的消息的数量

    题目思路:

    队列模拟,坑点在于竟然卡内存……详细看代码。

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stdio.h>
    #include<stdlib.h>
    #include<queue>
    #include<math.h>
    #include<map>
    #define INF 0x3f3f3f3f
    #define MAX 300050
    #define Temp 1000000000
    
    using namespace std;
    
    queue<int>Q[MAX];//记录时间戳
    queue<pair<int,int> >time;//记录时间戳和APP的名称
    bool vis[MAX];//标记是否未读取
    
    int main()
    {
        int ant,sum,n,q,x,op;
        ant=1;
        sum=0;
        memset(vis,false,sizeof(vis));
        scanf("%d%d",&n,&q);
        while(q--)
        {
            scanf("%d%d",&op,&x);
            if(op==1)
            {
                Q[x].push(ant);//向队列中添加
                time.push(make_pair(ant,x));
                ant++;
                sum++;//计数加1
            }
    
            else if(op==2)
            {
                while(!Q[x].empty())
                {
                    vis[Q[x].front()]=true;//标记为已读
                    Q[x].pop();//弹出
                    sum--;//计数减1
                }
            }
    
            else if(op==3)
            {
                while(!time.empty() && time.front().first<=x)
                {
                    if(!vis[time.front().first])//如果当前时间戳读入的信息为读取则弹出
                    {
                        Q[time.front().second].pop();
                        sum--;
                    }
                    time.pop();
                }
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    QButton
    注入
    SpringBoot热重启配置
    centos7 安装 tomcat
    centos 安装jdk
    spring boot (6) AOP的使用
    spring boot (5) 自定义配置
    spring boot (4) 使用log4 打印日志
    SpringBoot (3)设置支持跨域请求
    spring boot (2) 配置swagger2核心配置 docket
  • 原文地址:https://www.cnblogs.com/alan-W/p/5936692.html
Copyright © 2020-2023  润新知