Idea:
Assuming that the time when a store receives the order is t1, t2, t3, t4 (assuming it has been sorted in non-decreasing order), ask the priority of the store at t4 (according to the problem t4>=t1, t2, t3)
solution :
There are 2 parts to be calculated in total:
·Part 1: 1 ~ t1-1, t1+1 ~ t2-1, t2+1 ~ t3-1, t3+1 ~ t4-1, t4+1 ~ T, these Part of it is the moment when there is no order. These moments need to be subtracted
. The second part: t1, t2, t3, t4 These moments are the moments when the order is received. These moments are added by 2 respectively. It
should be noted that, If there are multiple orders at the same time, if t2 == t3 in the above non-decreasing sequence, the idea is as follows :
·Part 1: 1 ~ t1-1, t1+1 ~ t2-1, t3+1 ~ t4-1, t4+1 ~ T, these parts are the moments when there is no order, and these moments need to be subtracted
. The second part: t1, t2, t4 These moments are the moments when the order is received. These moments are respectively Add the number of orders at that moment cnt*2 (cnt[t1] = 1, cnt[t2] = 2, cnt[t4] = 1)
code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n, m, T;
int score[N], last[N];//score[]代表第i个店的优先级,last表示第i个店上一个有订单的时刻
bool st[N];//第i个店是否在优先队列里
PII order[N];
int main()
{
scanf("%d%d%d", &n, &m, &T);
for (int i = 0; i < m; i ++ ) scanf("%d%d", &order[i].x, &order[i].y);
//排序
sort(order, order + m);
for (int i = 0; i < m;)
{
int j = i;
//循环是为了找到相同的订单,cnt表示相同订单的数量
while (j < m && order[j] == order[i]) j ++ ;
int t = order[i].x, id = order[i].y, cnt = j - i;
i = j;
score[id] -= t - last[id] - 1;
if (score[id] < 0) score[id] = 0;
if (score[id] <= 3) st[id] = false; // 以上处理的是t时刻之前的信息
score[id] += cnt * 2;
if (score[id] > 5) st[id] = true;
last[id] = t;
}
//最后一个订单t时刻到结尾T那段距离,需要手动计算
for (int i = 1; i <= n; i ++ )
if (last[i] < T)
{
score[i] -= T - last[i];
if (score[i] <= 3) st[i] = false;
}
int res = 0;
//计算最终答案
for (int i = 1; i <= n; i ++ ) res += st[i];
printf("%d\n", res);
return 0;
}