其他分享
首页 > 其他分享> > 外卖店优先级(蓝桥杯模拟题)

外卖店优先级(蓝桥杯模拟题)

作者:互联网

1241. 外卖店优先级

 

“饱了么”外卖系统中维护着 NN 家外卖店,编号 1∼N1∼N。

每家外卖店都有一个优先级,初始时 (00 时刻) 优先级都为 00。

每经过 11 个时间单位,如果外卖店没有订单,则优先级会减少 11,最低减到 00;而如果外卖店有订单,则优先级不减反加,每有一单优先级加 22。

如果某家外卖店某时刻优先级大于 55,则会被系统加入优先缓存中;如果优先级小于等于 33,则会被清除出优先缓存。

给定 TT 时刻以内的 MM 条订单信息,请你计算 TT 时刻时有多少外卖店在优先缓存中。

输入格式

第一行包含 33 个整数 N,M,TN,M,T。

以下 MM 行每行包含两个整数 tsts 和 idid,表示 tsts 时刻编号 idid 的外卖店收到一个订单。

输出格式

输出一个整数代表答案。

数据范围

1≤N,M,T≤1051≤N,M,T≤105,
1≤ts≤T1≤ts≤T,
1≤id≤N

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N=100010;
PII order[N];
int n,m,T;
int score[N],last[N];//score表示优先级,last[id]表示该店铺最后一个有订单的时刻
bool st[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;
        while(j<m&&order[j]==order[i]) j++;
        int t=order[i].x,id=order[i].y,cnt=j-i;//cnt表示同一家店铺同一时刻的订单数
        i=j;//处理该店铺下一时刻的订单数或下一个店铺同一时刻的订单数
        
        score[id]-=t-last[id]-1;//减去该时刻到上一个有订单的时刻之间减少的优先级
        if(score[id]<0) score[id]=0;
        if(score[id]<=3) st[id]=false;
        
        score[id]+=cnt*2;
        if(score[id]>5) st[id]=true;
        
        last[id]=t;//更新最后一个有订单的时刻
    }
    for(int i=1;i<=n;i++)//枚举所有店铺
        if(last[i]<T){//T时刻不是最后一个带店铺有订单的时刻
        score[i]-=T-last[i];//T时刻也没有订单
        if(score[i]<=3) st[i]=false;//最后判断一次该店铺是否在优先缓存中
        }
    int res=0;
    for(int i=1;i<=n;i++)
     if(st[i]==true) res++;

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

 


标签:时刻,优先级,int,模拟题,蓝桥,外卖,include,id
来源: https://www.cnblogs.com/z218/p/15922600.html