P8685 [蓝桥杯 2019 省 A] 外卖店优先级

这道题虽然难度很低,但是细节不少

1.要先处理减,再处理加。因为是先经历了空档期的优先级衰减,然后才有订单带来的优先级提升;先减后加的时候有0这个下限兜底,如果先加后减可能会导致答案偏小。

2.减之后和加之后都要check一下,如果in_cache为true,减完之后小于等于三,但是加了以后又上升到了4,这时候应该就不算优先店铺了,但是如果少了减之后的那次check,就会导致这个本该被去掉的店仍然是优先店铺。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
using namespace std;

const int N = 1e5 + 5;

inline int read()
{
    int x = 0; char ch=getchar();
    while(ch<'0'||ch>'9')
        ch = getchar();
    while(ch>='0'&&ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x;
}

int n, m, t, cnt;
vector<int> _orders[N];

void check(bool &f, int l)
{
    if(!f && l > 5)
        f = true;
    if(f && l <= 3)
        f = false;
}

void calculate_priority(vector<int> &tar, int id)
{
    int last_time = 0, _level = 0;
    bool in_cache = false;
    for(int _time:tar)
    {
        _level = max(0, _level - max(0, _time - last_time - 1));
        check(in_cache, _level);
        _level += 2;
        check(in_cache, _level);
        last_time = _time;
    }
    _level = max(0, _level - (t - last_time));
    check(in_cache, _level);
    if(in_cache)
        cnt++;
}

int main()
{
    n=read();m=read();t=read();
    For(i,1,m)
    {
        int ts = read(), id = read();
        _orders[id].push_back(ts);
    }
    For(i,1,n)
    {
        if(!_orders[i].empty())
        {
            sort(_orders[i].begin(), _orders[i].end());
            calculate_priority(_orders[i], i);
        }
    }
    printf("%d\n", cnt);
    return 0;
}

 要严谨。

posted @ 2024-03-19 15:18  Gold_stein  阅读(5)  评论(0编辑  收藏  举报