# BZOJ 1208 宠物收养所 set+二分

https://www.lydsy.com/JudgeOnline/problem.php?id=1208

 1 #include<bits/stdc++.h>
2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数，会超时
4 #define Min(a, b) ((a) < (b) ? (a) : (b))
5 #define Mem(a) memset(a, 0, sizeof(a))
6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
8 #define lson ((o)<<1)
9 #define rson ((o)<<1|1)
10 #define Accepted 0
12 using namespace std;
14 {
15     int x=0,f=1;char ch=getchar();
16     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
17     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
18     return x*f;
19 }
20 typedef long long ll;
21 const int maxn = 200000 + 10;
22 const int MOD = 1000000007;//const引用更快，宏定义也更快
23 const int INF = 1e9 + 7;
24 const double eps = 1e-10;
25 const double pi = acos(-1);
26
27 set<int>s, s1;
28 set<int>::iterator l, r, cnt;
29
30 int main()
31 {
32     int n, m, T;
33     scanf("%d", &n);
34     int x, flag;
35     int ans = 0;
36     for(int i = 1; i <= n; i++)
37     {
38         scanf("%d%d", &flag, &x);
39         if(flag)//来了领养者
40         {
41             if(s.empty())//没有宠物
42                 s1.insert(x);
43             else
44             {
45                 l = --s.lower_bound(x);
46                 r = s.lower_bound(x);
47                 if(r != s.begin() && r != s.end())
48                 {
49                     if(x - *l <= *r - x)cnt = l;
50                     else cnt = r;
51                 }
52                 else if(r == s.begin())cnt = r;
53                 else cnt = l;
54                 ans += abs(*cnt - x);
55                 ans %= 1000000;
56                 s.erase(cnt);
57             }
58         }
59         else//来了宠物
60         {
61             if(s1.empty())//没有领养者
62                 s.insert(x);
63             else
64             {
65                 l = --s1.lower_bound(x);
66                 r = s1.lower_bound(x);
67                 if(r != s1.begin() && r != s1.end())
68                 {
69                     if(x - *l <= *r - x)cnt = l;
70                     else cnt = r;
71                 }
72                 else if(r == s1.begin())cnt = r;
73                 else cnt = l;
74                 ans += abs(*cnt - x);
75                 ans %= 1000000;
76                 s1.erase(cnt);
77             }
78         }
79     }
80     cout<<ans<<endl;
81     return Accepted;
82 }

posted @ 2018-10-04 23:13  _努力努力再努力x  阅读(239)  评论(0编辑  收藏  举报