Codeforces Round #366 (Div. 2) C. Thor(模拟)

54 篇文章 0 订阅

题目链接

一开始理解错了第三个的类型的意思,
手机上有n个应用,q个事件发生。
类型1:应用发生了一个通知
类型2:使用者读完了到目前为止应用x的发出的所有通知
类型3:使用者按照时间先后,读取t个通知
输出每次事件发生后未读通知的数量

#include<bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd

#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)

const int maxn = 3e6+10;
const int inf  = 1 << 28;

map<int,set<int> > mp;
bool vis[maxn];
vector<pair<int,int> > v;
int main(){

    int n,q;
    scanf("%d%d",&n,&q);
    int tot = 0;
    int last = 0;
    int tim = 0;
    for(int i=1;i<=q;i++){
        int type,x;
        scanf("%d%d",&type,&x);
        if(type==1){
            tot++;tim++;
            mp[x].insert(tim);
            v.pb(make_pair(tim,x));
        }
        else if(type==2){
            set<int>::iterator it=mp[x].begin();
            while(it!=mp[x].end()){
                vis[*it]=1;
                tot--;
                it++;
            }
            mp[x].clear();
        }
        else {
            if(x>last) {
                for(int i=last;i<x;i++)if(!vis[v[i].first]){
                    vis[v[i].first]=true;
                    tot--;
                    set<int>::iterator it = mp[v[i].second].begin();
                    mp[v[i].second].erase(*it);
                }
                last = x;

            }
        }
        printf("%d\n",tot);
    }
    return 0;
}

使用队列模拟,更加清晰

#include<bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd

#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)

const int maxn = 3e5+10;
const int inf  = 1 << 28;

queue<int> app[maxn];
queue<pair<int,int> > all;
bool mark[maxn];

int main(){
    int n,q;
    scanf("%d%d",&n,&q);
    int tot=0,cur=0;
    for(int i=1;i<=q;i++){
        int type,x;
        scanf("%d%d",&type,&x);
        if(type==1){
            tot++;cur++;
            all.push(make_pair(tot,x));
            app[x].push(tot);
        }
        else if(type==2){
            while(!app[x].empty()){
                int t = app[x].front();app[x].pop();
                mark[t]=1;
                cur--;
            }
        }
        else {
            while(!all.empty()&&all.front().first<=x){
                int t = all.front().first;
                int y = all.front().second;
                all.pop();
                if(mark[t]==false){
                    mark[t]=true;
                    cur--;app[y].pop();
                }
            }
        }
        printf("%d\n",cur);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值