您的位置:首页 > 其它

[BZOJ1208][[HNOI2004]宠物收养所][STL set+二分]

2017-02-25 11:02 274 查看

[BZOJ1208][[HNOI2004]宠物收养所][STL set+二分]

题目:

题目链接在此:[HNOI2004]宠物收养所”

思路:

当年出这道题的时候貌似STL还没有解禁,但现在就可以直接用STL的set来做了。

对宠物的特点值和每个领养者的特点值都开一个set来维护,可以看出任意时刻,一定会有一个set为空,那么当加入一个人或者宠物时判断其所属的set是否为空,如果不为空就加入,否则在另一个set里二分查找。

(set也支持lower_bound和upper_bound操作!)

代码:

#include <bits/stdc++.h>
using namespace std;
set<int> a[2];
int n, sub;
inline int Abs(const int &a) {
return a < 0 ? -a : a;
}
inline char get(void) {
static char buf[1000000], *p1 = buf, *p2 = buf;
if (p1 == p2) {
p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
if (p1 == p2) return EOF;
}
return *p1++;
}
inline void read(int &x) {
x = 0; static char c; bool minus = false;
for (; !(c >= '0' && c <= '9'); c = get()) if (c == '-') minus = true;
for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get()); if (minus) x = -x;
}
inline int find(int x, int y) {
set<int>::iterator low, up;
low = a[x ^ 1].lower_bound(y);
up = low; if (up != a[x ^ 1].begin()) up--;
int itlow = *low, itup = *up;
if (Abs(itlow - y) < Abs(itup - y)) {a[x ^ 1].erase(low); return itlow; }
else {a[x ^ 1].erase(up); return itup; }

}
int main(void) {
//freopen("in.txt", "r", stdin);
read(n); int x, y;
while (n--) {
read(x), read(y);
if (a[x ^ 1].empty()) a[x].insert(y);
else sub = (sub +  Abs(y - find(x, y))) % 1000000;
}
printf("%d\n", sub);
return 0;
}


完。

By g1n0st
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stl