原来usaco的金组题这么难。。。蒟蒻根本不会T T

我只会贪心部分,然后发现要平衡树,就不行了。。。(论线段树都写不出的蒟蒻)

然后发现iwtwiioi的blog,Orz!原来可以用STL来做的说。。。

STL大法好!

 

 1 /**************************************************************
 2     Problem: 1691
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:336 ms
 7     Memory:3428 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm>
12 #include <set>
13  
14 using namespace std;
15 typedef long long ll;
16 const int N = 100005;
17  
18 struct data{
19     int x, y;
20 }a[N], b[N];
21 inline bool operator < (const data &a, const data &b){
22     return a.y > b.y;
23 }
24 multiset <int> s;
25 multiset <int> :: iterator it;
26 int n, m, i, j = 1, x;
27 char ch;
28 ll ans;
29  
30 inline unsigned int read(){
31     x = 0;
32     ch = getchar();
33     while (ch < '0' || ch > '9')
34         ch = getchar();
35  
36     while (ch >= '0' && ch <= '9'){
37         x = x * 10 + ch - '0';
38         ch = getchar();
39     }
40     return x;
41 }
42  
43 int main(){
44     n = read(), m = read();
45     for (i = 1; i <= n; ++i)
46         a[i].x = read(), a[i].y = read();
47     for (i = 1; i <= m; ++i)
48         b[i].x = read(), b[i].y = read();
49     sort(a + 1, a + n + 1);
50     sort(b + 1, b + m + 1);
51     for (i = 1; i <= n; ++i){
52         while (a[i].y <= b[j].y && j <= m)
53             s.insert(b[j].x), ++j;
54         it = s.lower_bound(a[i].x);
55         if (it == s.end()){
56             printf("-1\n");
57             return 0;
58         }
59         ans += *it;
60         s.erase(it);
61     }
62     printf("%lld\n", ans);
63     return 0;
64 }
View Code

(p.s. 蒟蒻被rank1.的iwtwiioi大神稳稳地踩在了脚下)

posted on 2014-10-21 22:52  Xs酱~  阅读(560)  评论(0编辑  收藏  举报