1241. 外卖店优先级

https://www.acwing.com/problem/content/1243/

稍复杂一些的模拟题,但是若是暴力做法
即将a[i][j]定义为i时刻id为j的优先级
即可双重循环枚举每个时刻,每个id所有组合
在此情况下再判断这样的a[i][j]是否满足进入优先缓存st[i]中,a[i][j]进行优先级更新
如此,可以跑完90%的数据

但是题目数据为1e5,即O(n^2)明显无法实现100%数据

如此可以从其他角度优化,Y总的思路是,将无订单的,即不需要更新的放到下一次有订单的时候
这样就不需要考虑没有订单的店了
并且sort一遍,由于是二元组,因此sort优先按照第一个关键字id排序,这样,id一样的便会在一起
由于排序在一起,id相同,时间相同(即同一时刻,同一店铺可能有多个单子),如此可以一次性处理多个订单

#include <cstdio>
#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];
bool st[N];

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;
        while (j < m && order[j] == order[i]) j++;//同一时刻,同一个店铺,有多个单子
        int t = order[i].x, id = order[i].y, cnt = j - i;//时刻,店铺id,相同单子数量
        i = j;//更新
        score[id] -= t - last[id] - 1;//更新减法优先级
        if (score[id] < 0) score[id] = 0;
        if (score[id] <= 3) st[id] = false; // 以上处理的是t时刻之前的信息
        //先处理完t之前的需要减去或者置零的优先级,不管是时间顺序上还是逻辑上都需要先处理这些,再处理加法优先级,加入队列
        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到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;
}

 

posted @ 2022-10-13 21:26  风乐  阅读(41)  评论(0)    收藏  举报