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\n",sum);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/alan-W/p/5936692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值