Meteor Flow(贪心+优先队列)

Meteor Flow(贪心+优先队列)

AC_Code

 1 ///既然只要发射一次,就可以打掉,那么就要打掉那个耗费经历最多的,以保留更多的精力 (所以用优先队列,先弹出耗费经历最多的)
 2 ///其次,只要有能力打就先不发射(所以先入栈)
 3 
 4 #include <iostream>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <map>
 8 #include <queue>
 9 #include <ctime>
10 #include <vector>
11 #include <algorithm>
12 #include <functional>
13 #include <cstring>
14 using namespace std;
15 typedef long long ll;
16 typedef unsigned long long ull;
17 const int maxn=222222;
18 
19 int n, t[maxn],d[maxn];
20 priority_queue<int>que;
21 int main()
22 {
23     while( ~scanf("%d",&n) )
24     {
25         while(!que.empty() ) que.pop();
26         for(int i=1;i<=n;i++) scanf("%d%d",&t[i],&d[i]);
27         ll sum=0;
28         int ans=0;
29         for(int i=1;i<=n;i++)
30         {
31             que.push(d[i]);
32             sum+=d[i];
33 
34             while( i<n && t[i+1]==t[i] )
35             {
36                 i++;
37                 que.push(d[i]);
38                 sum+=d[i];
39             }
40 
41             while( sum>t[i]&&!que.empty() )
42             {
43                 int now=que.top();
44                 que.pop();
45                 sum-=now;
46                 ans++;
47             }
48         }
49         printf("%d
",ans);
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/wsy107316/p/11739453.html