CodeForces - 705C (16/600)

博客讲述了关于CodeForces中的一道题目,题目要求模拟手机应用通知的读取情况,包括新通知生成、特定应用的通知全部读取以及读取前t个通知的场景。博主分享了对题意的理解和解决过程。

Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can’t count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can’t).

q events are about to happen (in chronological order). They are of three types:

Application x generates a notification (this new notification is unread).
Thor reads all notifications generated so far by application x (he may re-read some notifications).
Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It’s guaranteed that there were at least t events of the first type before this event. Please note that he doesn’t read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.

Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.

The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).

Output
Print the number of unread notifications after each event.

Example
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:

Application 3 generates a notification (there is 1 unread notification).
Application 1 generates a notification (there are 2 unread notifications).
Application 2 generates a notification (there are 3 unread notifications).
Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:

Application 2 generates a notification (there is 1 unread notification).
Application 4 generates a notification (there are 2 unread notifications).
Application 2 generates a notification (there are 3 unread notifications).
Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
Application 3 generates a notification (there is 1 unread notification).
Application 3 generates a notification (there are 2 unread notifications).

就是模拟了一下。。
题意有点傻逼
他过去的也算

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using namespace std;
int n, m, q, w, bj[300001], bz[300001];
set<int>tt[300001];
int main()
{
    cin >> n >> m;
    int tou = 1, wei = 0;
    int qb = 0;
    for (int a = 1; a <= m; a++)
    {
        scanf("%d%d", &q, &w);
        if (q == 1)
        {
            bj[++wei] = w;
            tt[w].insert(wei);
            qb++;
        }
        else if (q == 2)
        {
            set<int, int>::iterator it;
            for (it = tt[w].begin(); it != tt[w].end(); it++)
            {
                int sd = *it;
                if (bz[sd])continue;
                bz[sd] = 1;
                qb--;
            }
            tt[w].clear();
        }
        else
        {
            for (; tou<= w; )
            {
                if (tou > wei)break;
                if (bz[tou])
                {
                    tou++;
                    continue;
                }
                qb--;
                bz[tou] = 1;
                tou++;
            }
        }
        printf("%d\n", qb);
    }
}
### 问题分析 Codeforces Contest 1215 Problem D,题名为 "Ticket Exchange System",主要涉及图论和贪心策略的应用。题目要求在特定条件下,寻找最优路径,使得某种目标函数最大化或最小化。 ### 解题思路 1. **问题建模** 本题通常可以建模为一个图上的最短路径问题,但目标函数可能不是传统的路径长度之和,而是某种组合函数。例如,题目可能要求在路径中选择某些节点,使得它们的某些属性满足特定条件,例如最大化最小值、最小化最大值等。 2. **使用 Dijkstra 算法变体** 由于目标函数可能涉及最大化某个属性(例如路径上最小边权的最大值),Dijkstra 算法可以被修改以适应这种需求。传统的 Dijkstra 用于求解最短路径,但在这种情况下,可以维护一个优先队列,每次扩展当前节点时,更新目标函数的值[^1]。 3. **状态表示与更新** 状态通常表示为 `(current_node, current_value)`,其中 `current_value` 是到达 `current_node` 时的目标函数值。如果通过某条边能够得到更优的 `current_value`,则更新对应的状态。这类似于松弛操作,但目标函数的更新方式可能不同[^2]。 4. **初始化与终止条件** 初始化时,起点的状态被设置为一个初始值(例如 0 或无穷大),其余节点的状态设置为无效或不可达。当终点的状态被取出时,可以提前终止算法,以减少不必要的计算[^3]。 5. **数据结构优化** 使用优先队列(如 `priority_queue`)来维护待处理的状态,确保每次取出的是当前最优的状态。此外,可以使用哈希表或数组来记录每个节点当前的最优值,避免重复处理[^4]。 6. **特殊情况处理** 在某些情况下,可能存在多个相同的边或自环边,需要确保这些情况不会导致死循环或错误的状态更新。可以通过判断新状态是否优于已有状态来决定是否将其加入队列[^5]。 ### 示例代码 以下是一个基于 Dijkstra 变体的伪代码实现: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; vector<pair<int, int>> adj[MAXN]; // 邻接表 int dist[MAXN]; // 存储每个节点的最优值 void dijkstra(int start, int n) { priority_queue<pair<int, int>> pq; fill(dist, dist + n + 1, -1); // 初始化为 -1 表示未访问 dist[start] = 0; pq.push({0, start}); while (!pq.empty()) { int u = pq.top().second; int cost = pq.top().first; pq.pop(); if (cost < dist[u]) continue; // 已找到更优解,跳过 for (auto &edge : adj[u]) { int v = edge.first; int w = edge.second; int new_cost = min(cost, w); // 以最小值为目标函数 if (new_cost > dist[v]) { dist[v] = new_cost; pq.push({new_cost, v}); } } } } ``` ### 相关问题 1. 如何在 Codeforces Contest 1215 Problem D 中建模图结构? 2. Dijkstra 算法的哪些变体适用于最大化路径上的最小值? 3. 在 Codeforces Contest 1215 Problem D 中,如何处理多个相同边的情况? 4. 为什么在某些情况下需要提前终止 Dijkstra 算法? 5. 如何验证 Codeforces Contest 1215 Problem D 的解是否正确?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值