[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
相关文章推荐
- 【set】【Splay】【pb_ds】bzoj1208 [HNOI2004]宠物收养所
- BZOJ 1208: [HNOI2004]宠物收养所 set,或者splay
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
- BZOJ 1208 HNOI2004 宠物收养所 平衡树/set
- BZOJ 1208: [HNOI2004]宠物收养所 平衡树/set
- bzoj 1208: [HNOI2004]宠物收养所 set
- BZOJ-1208: [HNOI2004]宠物收养所 (splay 查询前驱后继 set也可)
- 【BZOJ 1208】【HNOI 2004】宠物收养所【treap】|【STL】
- 【bzoj1208】【HNOI2004】【宠物收养所】【平衡树】【set】
- BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用
- bzoj 1208: [HNOI2004]宠物收养所 stl
- 【BZOJ 1208】 [HNOI2004]宠物收养所 splay模板(指针)+set
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
- BZOJ 1208: [HNOI2004]宠物收养所
- BZOJ 1208 [HNOI2004]宠物收养所 treap
- 【bzoj1208】[HNOI2004]宠物收养所
- [BZOJ1208][HNOI2004]宠物收养所(平衡树splay)
- BZOJ_1208_[HNOI2004]宠物收养所_SPLAY
- 【BZOJ1208】【HNOI2004】宠物收养所
- Bzoj1208 [HNOI2004]宠物收养所