[藍橋杯][2019年第十屆真題]外賣店優先級


題目描述

“飽了么”外賣系統中維護着 N 家外賣店,編號 1 ∼ N。每家外賣店都有 一個優先級,初始時 (0 時刻) 優先級都為 0。

每經過 1 個時間單位,如果外賣店沒有訂單,則優先級會減少 1,最低減 到 0;而如果外賣店有訂單,則優先級不減反加,每有一單優先級加 2。

如果某家外賣店某時刻優先級大於 5,則會被系統加入優先緩存中;如果 優先級小於等於 3,則會被清除出優先緩存。

給定 T 時刻以內的 M 條訂單信息,請你計算 T 時刻時有多少外賣店在優 先緩存中。

輸入

第一行包含 3 個整數 N、M 和 T。
以下 M 行每行包含兩個整數 ts 和 id,表示 ts 時刻編號 id 的外賣店收到

一個訂單。

輸出

輸出一個整數代表答案。

樣例輸入

2 6 6
1 1
5 2
3 1
6 2
2 1
6 2

樣例輸出

1

提示

對於 80% 的評測用例,1 ≤ N, M, T ≤ 10000。 對於所有評測用例,1 ≤ N,M,T ≤ 100000,1 ≤ ts ≤ T,1 ≤ id ≤ N。


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

#define MAXN 100001
/*
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
*/

bool cmp(pair<int, int>a, pair<int, int> b) {
    if (a.first == b.first) return a.second < b.second;
    return a.first < b.first;
}

int main()
{
    int n,m,t;
    cin >> n >> m >> t;
    int temp[MAXN], score[MAXN];
    bool cache[MAXN];
    vector < pair<int, int>> F;
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a>>b;
        pair<int, int> p(a, b);
        F.push_back(p);
    }
    sort(F.begin(), F.end(), cmp);
    memset(temp, 0, sizeof(temp));
    memset(score, 0, sizeof(score));
    memset(cache, false, sizeof(cache));
    for (int i = 0; i < m; i++) {
        int ti = F[i].first, si = F[i].second;
        if (ti != temp[si]) score[si] -= ti - temp[si] - 1;
        score[si] = score[si] > 0 ? score[si] : 0;
        if (score[si] <= 3) cache[si] = false;
        score[si] += 2;
        if (score[si] > 5) cache[si] = true;
        temp[si] = ti;
    }
    for (int i = 1; i < n+1; i++) {
        if (t > temp[i]) {
            score[i] -= t - temp[i];
            if (score[i] <= 3) cache[i] = false;
        }
    }
    int res=0;
    for (int i = 1; i < n+1; i++) {
        if(cache[i]==true)res++;
    }
    cout << res;
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM