2019 10th Lanqiao Cup Provincial Competition C++ University Group A Question G Takeaway Store Priority [Simulation]

Portal: AcWing 1241. Takeaway priority / Lanqiao Cup 2019 Tenth Real Question

Ideas

Summarize orders at the same time, and then traverse each time.

Only consider the moment when there is an order, for moment iii , first summarize the number of orders in the same store, and then traverse all orders, each order contains store number information (assuming the store number isid idi d ), maintain storeid idi d The time when the order last appeared (assuming it islast [id] last[id]l a s t [ i d ] ), then the shopid idid之前有 i − l a s t [ i d ] − 1 i-last[id]-1 ilast[id]There is no order at the time of 1 , at this time, the priority needs to be subtracted from the time when there is no order. If the priority is <=3, the storeid idi d move out of the cache queue; then the priority plusnum numPriority brought by n u m orders2 ∗ num 2*num2n u m , if the priority is >5, enter the buffer queue.

Finally, you need to traverse all TTShops with no orders at time T, the time they last order appears toTTThe period of time T is the reduced value of its priority, and at the same time whether the update is removed from the cache queue.

T T T moments,mmm orders,nnn stores, the time complexity is aboutO (T + m + n) O(T+m+n)O(T+m+n ) because for less thanTTAt the moment of T , the priority of all stores is not updated, but only the priority of the stores with orders is updated, only in the lastTTIt is sufficient to update the priority of all stores once at T.

AC code

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,m,T,last[N],sum[N];
bool f[N]; // 是否在缓存队列中
vector<int>ans[N];
unordered_map<int,int>vis;
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin>>n>>m>>T;
    int t,id;
    for(int i=1;i<=m;i++)
    {
    
    
        cin>>t>>id;
        ans[t].push_back(id); // t时刻编号为id的店有订单
    }
    memset(last,-1,sizeof(last));
    for(int i=1;i<=T;i++) // 遍历时间
    {
    
    
        if(ans[i].size()) // i时刻有订单
        {
    
    
            int sz=ans[i].size();
            vis.clear();
            for(int j=0;j<sz;j++)
            {
    
    
                int id=ans[i][j];
                vis[id]++; // 汇总相同店的订单数
            }
            for(auto it:vis)
            {
    
    
                int id=it.first; // 店id
                int num=it.second; // 订单数num
                if(last[id]==-1) // 店id第1次出现
                {
    
    
                    sum[id]=num*2;
                    last[id]=i;
                    if(sum[id]>5)f[id]=1; // 放入缓存队列
                }
                else
                {
    
    
                    int d=i-last[id]-1;
                    sum[id]=max(0,sum[id]-d);
                    if(sum[id]<=3)f[id]=0; // 移出缓存队列
                    sum[id]+=num*2;
                    if(sum[id]>5)f[id]=1; // 放入缓存队列
                    last[id]=i;
                }
            }
        }
        if(i==T) // 处理T时刻无订单的店
        {
    
    
            for(int j=1;j<=n;j++) 
            {
    
    
                if(last[j]!=T) // 店j的最后一次订单出现在T时刻之前
                {
    
    
                    int d=T-last[j]; 
                    sum[j]=max(0,sum[j]-d);
                   if(sum[j]<=3)f[j]=0; // 移出缓存队列
                }
            }
        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(f[i])cnt++;
    printf("%d\n",cnt);
    return 0;
}
/*
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2
ans:1
2 6 8
1 1
5 2
3 1
6 2
2 1
6 2
ans:1
2 6 9
1 1
5 2
3 1
6 2
2 1
6 2
ans:0
1 3 5
1 1
2 1
3 1
ans:1
1 3 6
1 1
2 1
3 1
ans:0
*/

Guess you like

Origin blog.csdn.net/ljw_study_in_CSDN/article/details/109069626