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

50 篇文章 0 订阅
5 篇文章 0 订阅

[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值