0
点赞
收藏
分享

微信扫一扫

外卖的订单--蓝桥杯第十届省赛c++A/C组

Java架构领域 2022-03-19 阅读 48

n家店,m个订单,问T时刻有多少外卖店在缓存中

m个订单中每个给出ts时刻id店收到一个订单

思路:

将所有订单按照pair<int,int> order[N]存储N个订单

first为时间,second为店的编号

按照先时间,后店编号的顺序sort一下,这样同时间的订单就放到了一起

然后按照顺序处理m条订单

如果订单相同,一次处理多条相同的订单

last[id]数组存储第id家店上次收到订单的时间

score[id]数组存储第id家店铺当前的优先级

bool st[id]表示第id家店铺还在不在缓存中

具体过程:

遍历m个订单

当前到了第i个订单,order[i],且有cnt个相同的订单(用j从第i个开始往后遍历,直到order

[j]!=order[i]退出,即可得到有多少相同的)

(1)

当前时间为t=order[i].x,这个订单是给id=order[i].y的店铺的

上一次收到订单的时间是last[id] 因为每过1h就会减少1个优先度

两者过了t-last[id]-1的时间间隔,所以score[id]先减去t-last[id]-1

(2)

如果当前减完之后score[id]<=3,那么该店当前不在缓存中 st[id]==false

如果减完之后score[id]<0了,由于题目规定优先级不能小于0,score[id]==0

减完再加上当前订单会增加的优先级数,有一个订单就+2,score[id]+=2* cnt

如果加完>5说明进入了缓存,st==true

到下一个该店铺的订单时,本订单相当于上一个订单,更新last[id]的时间==t

(3)

每个店铺最后一个订单可能<当前要询问的时间T

所以最后再处理一下每个店铺的优先级,score[id]-=T-last[i];

这里不减1,因为T时刻也没订单。T时刻有订单会在上面情况中被处理掉

最后遍历一下所有的店是不是在缓存中即可,判断st==true?

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n,m,T;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1e5+10;

PII order[N];
int last[N];//第i家店铺上次订单的时间
int score[N];//第i家店铺当前的优先级
bool st[N];//判断第i家店铺现在在不在优先缓存中

int main()
{
    cin>>n>>m>>T;
    for(int i=0;i<m;i++)
    {
        cin>>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]<=3) st[id]=false;
        
        if(score[id]<0) score[id]=0;
        
        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)
        {
            score[i]-=T-last[i];
            if(score[i]<=3) st[i]=false;
        }
    }
    
    int res=0;
    for(int i=1;i<=n;i++)
    {
        if(st[i]) res++;
    }
    cout<<res<<endl;
    return 0;
}

举报

相关推荐

0 条评论