[BZOJ1941][Sdoi2010]Hide and Seek

试题描述

小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏---捉迷藏。 但是,他们觉得,玩普通的捉迷藏没什么意思,还是不够寂寞,于是,他们决定玩寂寞无比的螃蟹版捉迷藏,顾名思义,就是说他们在玩游戏的时候只能沿水平或垂直方向走。一番寂寞的剪刀石头布后,他们决定iPig去捉giPi。由于他们都很熟悉PKU的地形了,所以giPi只会躲在PKU内n个隐秘地点,显然iPig也只会在那n个地点内找giPi。游戏一开始,他们选定一个地点,iPig保持不动,然后giPi用30秒的时间逃离现场(显然,giPi不会呆在原地)。然后iPig会随机地去找giPi,直到找到为止。由于iPig很懒,所以他到总是走最短的路径,而且,他选择起始点不是随便选的,他想找一个地点,使得该地点到最远的地点和最近的地点的距离差最小。iPig现在想知道这个距离差最小是多少。 由于iPig现在手上没有电脑,所以不能编程解决这个如此简单的问题,所以他马上打了个电话,要求你帮他解决这个问题。iPig告诉了你PKU的n个隐秘地点的坐标,请你编程求出iPig的问题。

输入

第一行输入一个整数N 第2~N+1行,每行两个整数X,Y,表示第i个地点的坐标

输出

第一行输入一个整数N 第2~N+1行,每行两个整数X,Y,表示第i个地点的坐标

输入示例


输出示例


数据规模及约定

对于30%的数据,N<=1000 对于100%的数据,N<=500000,0<=X,Y<=10^8 保证数据没有重点保证N>=2

题解

kd树模板题,对于每一个点算一下除它之外的最远和最近距离。

既然要“除它之外”,那么设计一个删除操作将删除的点打一个标记,询问完后再删除标记。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
#define LL long long const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
LL read() {
LL x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 500010
#define oo 1047483647
int n, ToT, lc[maxn], rc[maxn], nx[maxn], ny[maxn], root;
bool Cur;
struct Node {
int x[2], mx[2], mn[2];
bool del;
bool operator < (const Node& t) const { return x[Cur] == t.x[Cur] ? x[Cur^1] < t.x[Cur^1] : x[Cur] < t.x[Cur]; }
bool operator == (const Node& t) const { return x[0] == t.x[0] && x[1] == t.x[1]; }
int operator * (const Node& t) const { return abs(x[0] - t.x[0]) + abs(x[1] - t.x[1]); }
} ns[maxn]; void maintain(int o) {
int l = lc[o], r = rc[o];
for(int i = 0; i < 2; i++) {
ns[o].mx[i] = max(max(ns[l].mx[i], ns[r].mx[i]), ns[o].del ? -oo : ns[o].x[i]);
ns[o].mn[i] = min(min(ns[l].mn[i], ns[r].mn[i]), ns[o].del ? oo : ns[o].x[i]);
}
return ;
}
void build(int& o, int L, int R, bool cur) {
if(L > R){ o = 0; return ; }
int M = L + R >> 1; o = M;
Cur = cur; nth_element(ns + L, ns + M, ns + R + 1);
build(lc[o], L, M - 1, cur ^ 1); build(rc[o], M + 1, R, cur ^ 1);
maintain(o);
return ;
}
Node x;
void remove(int o, bool cur) {
Cur = cur;
if(!o) return ;
if(x == ns[o]){ ns[o].del = 1; return maintain(o); }
remove(x < ns[o] ? lc[o] : rc[o], cur ^ 1);
return maintain(o);
}
void insert(int o, bool cur) {
Cur = cur;
if(!o) return ;
// if(x == ns[o]) printf("%d: %d %d\n", o, ns[o].x[0], ns[o].x[1]);
if(x == ns[o]){ ns[o].del = 0; return maintain(o); }
insert(x < ns[o] ? lc[o] : rc[o], cur ^ 1);
return maintain(o);
}
int calcmx(int o) { return max(abs(ns[o].mx[0] - x.x[0]), abs(ns[o].mn[0] - x.x[0])) + max(abs(ns[o].mx[1] - x.x[1]), abs(ns[o].mn[1] - x.x[1])); }
int querymx(int o) {
if(!o) return -oo;
int ans = -oo;
if(!ns[o].del) ans = max(ans, ns[o] * x);
int dl = calcmx(lc[o]), dr = calcmx(rc[o]);
if(dl > dr) {
if(dl > ans) ans = max(ans, querymx(lc[o]));
if(dr > ans) ans = max(ans, querymx(rc[o]));
}
else {
if(dr > ans) ans = max(ans, querymx(rc[o]));
if(dl > ans) ans = max(ans, querymx(lc[o]));
}
return ans;
}
int calcmn(int o) {
int ans = 0;
for(int i = 0; i < 2; i++) {
if(x.x[i] > ns[o].mx[i]) ans += x.x[i] - ns[o].mx[i];
if(x.x[i] < ns[o].mn[i]) ans += ns[o].mn[i] - x.x[i];
}
return ans;
}
int querymn(int o) {
if(!o) return oo;
int ans = oo;
if(!ns[o].del) ans = min(ans, ns[o] * x);
int dl = calcmn(lc[o]), dr = calcmn(rc[o]);
if(dl < dr) {
if(dl < ans) ans = min(ans, querymn(lc[o]));
if(dr < ans) ans = min(ans, querymn(rc[o]));
}
else {
if(dr < ans) ans = min(ans, querymn(rc[o]));
if(dl < ans) ans = min(ans, querymn(lc[o]));
}
return ans;
} int main() {
ns[0].mx[0] = ns[0].mx[1] = -oo;
ns[0].mn[0] = ns[0].mn[1] = oo;
n = read();
for(int i = 1; i <= n; i++) {
nx[i] = ns[++ToT].x[0] = read(); ny[i] = ns[ToT].x[1] = read();
ns[ToT].del = 0;
} build(root, 1, n, 0);
int ans = oo;
for(int i = 1; i <= n; i++) {
x.x[0] = nx[i]; x.x[1] = ny[i];
remove(root, 0);
ans = min(ans, querymx(root) - querymn(root));
// printf("%d %d\n", querymx(root), querymn(root));
insert(root, 0);
} printf("%d\n", ans); return 0;
}
/*
6
0 0
1 1
0 9
9 3
4 2
9 8
*/

[BZOJ1941][Sdoi2010]Hide and Seek的更多相关文章

  1. 【kd-tree】bzoj1941 [Sdoi2010]Hide and Seek

    枚举每个点,计算离他最近的和最远的点. #include<cstdio> #include<cmath> #include<algorithm> using nam ...

  2. BZOJ1941:[SDOI2010]Hide and Seek(K-D Tree)

    Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏- ...

  3. 【BZOJ1941】[Sdoi2010]Hide and Seek KDtree

    [BZOJ1941][Sdoi2010]Hide and Seek Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了 ...

  4. 【BZOJ-1941】Hide and Seek KD-Tree

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 830  Solved: 455[Submi ...

  5. bzoj:1941: [Sdoi2010]Hide and Seek

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 531  Solved: 295[Submi ...

  6. 【BZOJ1941】Hide and Seek(KD-Tree)

    [BZOJ1941]Hide and Seek(KD-Tree) 题面 BZOJ 洛谷 题解 \(KD-Tree\)对于每个点搜一下最近点和最远点就好了 #include<iostream> ...

  7. [bzoj1941][Sdoi2010]Hide and Seek_KD-Tree

    Hide and Seek bzoj-1941 Sdoi-2010 题目大意:给出平面上n个点,选出一个点,使得距离这个点的最远点曼哈顿距离减去距离这个点的最近非己点的曼哈顿距离最小.输出最小曼哈顿距 ...

  8. 【bzoj1941】 Sdoi2010—Hide and Seek

    http://www.lydsy.com/JudgeOnline/problem.php?id=1941 (题目链接) 题意 给出n个二维平面上的点,求一点使到最远点的距离-最近点的距离最小. Sol ...

  9. 【bzoj1941】[Sdoi2010]Hide and Seek KD-tree

    题目描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏---捉迷藏. ...

随机推荐

  1. 回归到最初的编程——Linux下的C编程

    最近感觉有些浮躁,一方面感觉最近写公司的PHP代码倍感无聊,没有什么成就感!另一方面面对我的mac电脑中安装了诸多开发语言,倍感浮躁与困惑!同时想到这么多年来,却一直在使用PHP进行程序开发,总觉得有 ...

  2. [AHOI2013]打地鼠(网络流)

     [问题描述]      游戏里一共会冒出来N个地鼠,这些地鼠冒出来的位置都分布在一条直线上.第i个地鼠会在Ti时刻在Xi位置冒出来,打到第i个地鼠的得分是Pi.     当游戏开始时(也就是0时刻) ...

  3. AngularJS开发指南13:AngularJS的过滤器详解

    AngularJS过滤器是用来格式化输出数据的.除了格式化数据,过滤器还能修改DOM.这使得过滤器通常用来做些如“适时的给输出加入CSS样式”等工作. 比如,你可能有些数据在输出之前需要根据进行本地化 ...

  4. 在CentOS7上安装RabbitMQ

    安装过程参考官网: Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) 首先需要安装erlang,参考:http://fedo ...

  5. strncmp很好的函数

    strcmp比较的是所有的长度,而strncmp可以比较前几个长度 strncmp(s1,s2,n);这样就比较了s1,s2,前n个长度的大小.

  6. 【poj1090】 Chain

    http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...

  7. Linux中的TUN/TAP设备

    今天才发现这家伙...怎么讲...深以为耻.晚上的任务是加深对它的了解,就这么定了. 1. General questions.1.1 What is the TUN ?  The TUN is Vi ...

  8. 使用JMeter创建数据库(Mysql)测试

    我的环境:MySQL:mysql-essential-5.1.51-win32 jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945 ...

  9. 学习使用Robot Framework自动化测试框架-web元素定位

    转:http://blog.csdn.net/u012145166/article/details/50342569 1.name和id 其中使用到了name和id定位.但有时候由于开发人员的疏忽或者 ...

  10. appium按钮定位,去掉弹出框

    #coding=utf-8 这个一定要加上,不然脚本中注释中都不能有中文 ''' Created on 2015年7月2日 @author: liujuan ''' import sys reload ...