1241. 外卖店优先级 ###K //K

题目链接:https://www.acwing.com/problem/content/1243/

思路:考虑暴力的写法 每次枚举每一秒 然后来进行模拟,这样写是n^2的 可以发现很多时间段没有订单

这些都是-1的操作,所以考虑只枚举有订单的点,每次和上一次出现的订单点 相减, 这样就只需要枚举时间点

即可 时间复杂度o(n)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+10;
 4 const int mod=100019;
 5 #define ll long long
 6 #define ull unsigned long long
 7 #define pi pair<int,int>
 8 #define fi first
 9 #define sc second
10 #define pb push_back
11 vector<int>E[maxn];
12 int st[maxn];
13 int score[maxn];
14 
15 
16 
17 int main()
18 {
19     ios::sync_with_stdio(false);
20     cin.tie(0);
21     int n,m,t;
22     cin>>n>>m>>t;
23     while(m--)
24     {
25         int ts,id;
26         cin>>ts>>id;
27         E[id].pb(ts);
28     }
29     for(int i=1;i<=n;i++)
30     {
31         E[i].pb(0);
32         E[i].pb(t+1);
33         sort(E[i].begin(),E[i].end());
34     }
35 
36     for(int i=1;i<=n;i++)
37     {
38         int len=E[i].size();
39         if(len==2) continue;
40         for(int j=1;j<len;j++)
41         {
42             int x=E[i][j]-E[i][j-1]-1;
43             if(x>0)
44             {
45                 score[i]-=x;
46                 score[i]=max(0,score[i]);
47                 if(score[i]<=3) st[i]=0;
48             }
49             if(j!=len-1)
50             {
51                 score[i]+=2;
52                 if(score[i]>5) st[i]=1;
53             }
54         }
55     }
56 
57 
58     int ans=0;
59     for(int i=1;i<=n;i++) ans+=st[i];
60     cout<<ans<<'\n';
61 
62 }
View Code

 

posted @ 2021-04-10 13:56  canwinfor  阅读(56)  评论(0)    收藏  举报