poj1456 Supermarket

书上用的方法是正着按照天数推,如果任务大于小根堆顶就替换,天数多于任务就加。

而我依稀记得以前洛谷上有一题也是这个,用时光倒流来求解,天数倒推,加任务,取大根堆顶即可。

我的代码实现:

 1 #include <cstdio>
 2 #include <queue>
 3 #include <algorithm>
 4 const int N = 100010;
 5 /// poj 1456
 6 struct A {
 7     int val, time;
 8     bool operator < (const A &x) const {
 9         return time < x.time;
10     }
11 }a[N];
12 
13 std::priority_queue<int> Q;
14 
15 int main() {
16     int n;
17     while(scanf("%d", &n) != EOF) {
18         while(!Q.empty()) Q.pop();
19         for(int i = 1; i <= n; i++) {
20             scanf("%d%d", &a[i].val, &a[i].time);
21         }
22         std::sort(a + 1, a + n + 1);
23         int ans = 0;
24         for(int t = a[n].time, j = n; t >= 1; t--) {
25             while(a[j].time >= t) {
26                 Q.push(a[j--].val);
27             }
28             if(!Q.empty()) {
29                 ans += Q.top();
30                 Q.pop();
31             }
32         }
33         printf("%d\n", ans);
34     }
35     return 0;
36 }
AC代码

 

posted @ 2018-05-23 18:16  garage  阅读(90)  评论(0编辑  收藏  举报