BZOJ 1208 宠物收养所 set+二分

题目链接:

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

题目大意:

最近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。 1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。 2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

思路:

直接用set即可。

注意特判迭代器,否则会RE

 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
11 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
12 using namespace std;
13 inline int read()
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 }