外卖店优先级
题意

思路
n家外卖店,有一个优先级判断,给出一堆订单,每个订单有时间t和外卖店编号。
按照pair<时间,编号> 排序,暴力枚举期间可以有优化:出现同一个时间、同一家店铺有好几个订单。这部分用双指针找出。
同时这家店铺这个时间点之前没有订单,先处理掉这些没有订单的天数。
之后处理这连续的订单。
枚举完订单之后,之后的时间有可能无订单,处理一下。
以上过程,为了处理是隔几天有的订单,开一个数组记录上次有订单的时间。
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+100;
typedef pair<int,int>pll;
pll a[N];
bool st[N];
int val[N],post[N];
int n,m,t;
int main()
{
cin>>n>>m>>t;
for(int i=0;i<m;i++)
{
int ts,id;
cin>>ts>>id;
a[i]={ts,id};
}
sort(a,a+m);
for(int i=0;i<m;)
{
int j=i;
while(j<m && a[i] ==a[j]) j++;
int cnt=j-i,tt=a[i].first,id=a[i].second;
val[id]-=(tt-post[id]-1);
if(val[id]<0) val[id]=0;
if(val[id]<=3) st[id]=false;
val[id]+=cnt*2;
if(val[id]>5) st[id]=true;
post[id]=tt;
i=j;
}
// for (int i = 0; i < m;)
// {
// int j = i;
// while (j < m && a[j] == a[i]) j ++ ;
// int tt = a[i].x, id = a[i].y, cnt = j - i;
// i = j;
// val[id] -= tt - post[id] - 1;
// if (val[id] < 0) val[id] = 0;
// if (val[id] <= 3) st[id] = false; // 以上处理的是t时刻之前的信息
// val[id] += cnt * 2;
// if (val[id] > 5) st[id] = true;
// post[id] = tt;
// }
for (int i = 1; i <= n; i ++ )
if (post[i] < t)
{
val[i] -= t - post[i];
if(val[i]<0) val[i]=0;
if (val[i] <= 3) st[i] = false;
}
int res=0;
for(int i=1;i<=n;i++)
if(st[i])
res++;
cout<<res<<endl;
return 0;
}
posted on
浙公网安备 33010602011771号