AcWing 1241. 外卖店优先级 解题思路及代码

先贴个题目:

以及原题链接:
1241. 外卖店优先级 - AcWing题库icon-default.png?t=N7T8https://www.acwing.com/problem/content/1243/

然后讲讲思路, 这题原来我想用一个二维数组,一个表示id,一个表示时间,然后读入数据最后遍历处理,但1e5*1e5的数组会爆内存,所以考虑优化,我们发现, 如果直接把订单记录下来,然后按时间排序,就可以节省很大一部分空间,因为店铺优先级可以变成一维数组,然后怎么处理中间没有订单的时候呢?我们考虑用一个一维数组存最后一次有订单的时间,然后在下一次有订单的时候一起处理,由此,思路完备,代码如下:

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

const int N = 1e5 + 10;

struct dingdan
{
    int ts;
    int id;
};

bool operator<(dingdan &a, dingdan &b)
{
    return a.ts < b.ts;
}

dingdan a[N];
int  score[N],last[N];
bool st[N] = {false};

int main()
{
    int n, m, t;
    cin >> n >> m >> t;
    for (int i = 1; i < m + 1; ++i)
    {
        scanf("%d%d", &a[i].ts, &a[i].id);
    }
    sort(a + 1, a + m + 1);
    for (int i = 1; i < m + 1; ++i)
    {
        //处理此次订单之前的无订单状态
        int x;
        if(a[i].ts!=last[a[i].id])//防止同一时间同家订单的情况导致电表倒转
             x= a[i].ts - last[a[i].id] - 1;
        else
             x = 0;
        score[a[i].id] -= x;
        if (score[a[i].id] < 0)
            score[a[i].id] = 0;
        if (score[a[i].id] < 4 && st[a[i].id] == true)
            st[a[i].id] = false;
        //处理此次订单
        score[a[i].id] += 2;
        last[a[i].id] = a[i].ts;
        if (score[a[i].id] > 5)
            st[a[i].id] = true;
    }
    for (int i = 1; i < n + 1; ++i)//处理最后一个时刻没有订单的情况
    {
        if (last[i] < t)
        {
            int x = t - last[i];
            score[i] -= x;
            if (score[i] < 0)
                score[i] = 0;
            if (score[i] < 4 && st[i] == true)
                st[i] = false;
        }
    }
    int ans = 0;
    for (int i = 1; i < n + 1; ++i)
        if (st[i])
        {
            ans++;
        }

    cout << ans;

    return 0;
}

确实算是模拟题里面的比较恶心的题了。

by————2024.3.1刷题记录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值