优先队列(牛客挑战赛50C)

牛客网挑战赛50C:https://ac.nowcoder.com/acm/problem/50439;

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100020;
 4 #define LL long long
 5 priority_queue<LL, vector<LL>, greater<LL> > q;
 6 struct ty
 7 {
 8     LL v;
 9     int s;
10     bool operator < (const ty &a) const{
11         return s > a.s;
12     }
13 }a[maxn];
14 int main()
15 {
16     int n;
17     scanf("%d", &n);
18     for(int i = 1; i <= n; i++)
19     {
20         scanf("%lld%d",&a[i].v, &a[i].s);
21     }
22     sort(a+1, a+n+1);
23  
24     LL tmp = 0, ans =0;
25   
26  
27     for(int i = 1; i <= n; i++)
28     {
29         tmp += a[i].v;
30         q.push(a[i].v);
31  
32         while(q.size() > a[i].s)
33         {
34             tmp -= q.top();
35             q.pop();
36         }
37         ans = max(ans, tmp);
38     }
39     printf("%lld", ans);
40     return 0;
41 }

这题好像还可以用权值线段树,可以试一试。

 

posted @ 2020-03-27 10:30  tzy666  阅读(115)  评论(0)    收藏  举报