n<=10000个点(xi,yi),找到一个不同于给出的所有点的点,使得该点到所有点的曼哈顿距离最小并找出这样的点的个数。

第一眼看上去这不是中位数嘛,奇数一个点偶数一片,然后找一下这篇区域有几个不符合的点即可。不过要找出“不同于给出的点”的点,那万一中位数那个点被占了,就找它四周四个点即可。

错误!明知道会有中位数那一个点被占了,那怎么就不考虑四周都被占了的情况?

不过可以肯定的是,离中位数越近的点算出来的距离是越小的,尽管与中位数点距离相同的点答案可能不同。因此判断下中位数区域是否大于n,如果是那直接记答案(因为该区域肯定有答案),否则bfs。

为什么判重要用map呢其实用set就好了。。。。

 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
//#include<iostream>
using namespace std; int n;
#define maxn 40011
int x[maxn],y[maxn],sx[maxn],sy[maxn];
struct Point
{
int x,y;
bool operator < (const Point &b) const
{return x<b.x || (x==b.x && y<b.y);}
}p[maxn];
map <Point,bool> mp,vis;
int ans,cnt;
struct qnode{int d,x,y;}q[maxn];int head,tail;
const int dx[]={,,,,-},dy[]={,,-,,},inf=0x3f3f3f3f;
int dis(int xx,int yy)
{
int L=,R=n+;
while (L<R)
{
const int mid=(L+R+)>>;
if (x[mid]<xx) L=mid;
else R=mid-;
}
int idx=L;
L=;R=n+;
while (L<R)
{
const int mid=(L+R+)>>;
if (y[mid]<yy) L=mid;
else R=mid-;
}
int idy=L;
return (*idx-n)*xx-*sx[idx]+sx[n]+(*idy-n)*yy-*sy[idy]+sy[n];
}
bool bfs(int d)
{
bool flag=;
while (head!=tail && q[head].d==d)
{
const int nx=q[head].x,ny=q[head++].y;
if (head==maxn) head=;
if (!mp.count((Point){nx,ny}))
{
flag=;int tmp=dis(nx,ny);
if (ans>tmp)
{
ans=tmp;
cnt=;
}
else if (ans==tmp) cnt++;
}
else
{
for (int i=;i<=;i++)
{
const int px=nx+dx[i],py=ny+dy[i];
if (vis.count((Point){px,py})) continue;
vis[(Point){px,py}]=;
qnode &now=q[tail];
now.x=px;now.y=py;now.d=d+;tail++;
}
}
}
return flag;
}
int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
p[i].x=x[i];p[i].y=y[i];
mp[(Point){x[i],y[i]}]=;
}
sort(x+,x++n);sort(y+,y++n);
sx[]=;for (int i=;i<=n;i++) sx[i]=sx[i-]+x[i];
sy[]=;for (int i=;i<=n;i++) sy[i]=sy[i-]+y[i];
int x1=x[(n+)/],x2=x[n/+],y1=y[(n+)/],y2=y[n/+];
if (1ll*(x2-x1+)*(y2-y1+)>n)
{
ans=sx[n]-*sx[(n+)/]+sy[n]-*sy[(n+)/];cnt=1ll*(x2-x1+)*(y2-y1+);
for (int i=;i<=n;i++)
if (p[i].x>=x1 && p[i].x<=x2 && p[i].y>=y1 && p[i].y<=y2) cnt--;
printf("%d %d\n",ans,cnt);
}
else
{
head=tail=;ans=inf;
for (int i=x1;i<=x2;i++)
for (int j=y1;j<=y2;j++)
{
qnode &now=q[tail];
now.x=i;now.y=j;now.d=;tail++;
vis[(Point){i,j}]=;
}
x[]=y[]=-inf;x[n+]=y[n+]=inf;
int play=;
while (!bfs(play++));
printf("%d %d\n",ans,cnt);
}
return ;
}

BZOJ1696: [Usaco2007 Feb]Building A New Barn新牛舍的更多相关文章

  1. Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学

    1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 394  Solve ...

  2. 【BZOJ】1696: [Usaco2007 Feb]Building A New Barn新牛舍(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1696 原题要求min(sum{|x-xi|+|y-yi|}),且一定要看题:“没有两头牛的吃草位置是 ...

  3. bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 ——中位数排序

    Description 经过多年的积蓄,农夫JOHN决定造一个新的牛舍.他知道所有N(2 <= N <= 10,000)头牛的吃草位置,所以他想把牛舍造在最方便的地方. 每一头牛吃草的位置 ...

  4. BZOJ 1696 [Usaco2007 Feb]Building A New Barn新牛舍 数学

    题意:链接 方法:数学+模拟 解析: 首先这类问题不是第一次见了,所以直接知道拿x的中位数.y的中位数. 这题就是讨论情况很的烦. 题中有个限制,给出待求和的点不能选取. 所以假设奇数个点,求出x中位 ...

  5. [USACO07FEB]新牛棚Building A New Barn

    洛谷题目链接:[USACO07FEB]新牛棚Building A New Barn 题目描述 After scrimping and saving for years, Farmer John has ...

  6. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  7. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  8. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  9. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

随机推荐

  1. smile domain name www.bn-nd.com for sell. Please contact boyanzheng at foxmail.com 微笑的域名。请联系邮箱。

  2. 数据库管理系统X

    大部分DBMS提供数据定义语言DDL(Data Definition Language)和数据操作语言DML(Data Manipulation Language),供用户定义数据库的模式结构与权限约 ...

  3. CentOS 7上修改主机名

                                       如何在CentOS 7上修改主机名                                           在Cent ...

  4. Format a Hard Drive in Csharp

    Article Author(s): Audric Thevenet All Rights Reserved. Here's how to format hard drives, floppies, ...

  5. Java中System.setProperty()用法

    /*  * 设置指定键对值的系统属性  * setProperty (String prop, String value);  *  * 参数:  * prop - 系统属性的名称.  * value ...

  6. 1.ssm web项目中的遇到的坑--自定义JQuery插件(slide menu)

    自定义的JQuery插件写的回调函数不执行: 写好了回调函数,将函数打印出来是原形,就是不执行 function () { console.log("---onClickItem---&qu ...

  7. 最小生成树 || HDU 1301 Jungle Roads

    裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...

  8. delphi并行压缩

    real case test MM parallel 4x scalable (i7 6700)(on the newer processors will be linear) I did a sma ...

  9. modify django app models.py adn settings.py

    from django.db import models from django.contrib import admin # from personal import models class Us ...

  10. Maven实战读书笔记(四):Maven生命周期与插件

    Maven的生命周期是对所有构建过程的抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等几乎所有构建步骤. Maven的生命周期是抽象的,其实际行为是由插件来完成 ...