POJ 3269 中位数
题意:
思路:
这道题坑也不少。。 你准备好脑洞了么?
首先
要认真审题 题目中有说:“没有两头牛的吃草位置是相邻的”
这句话让我们省了很多的事儿 (Discuss里有的大神就入了这个坑了)
然后呢
自然想到了中位数 (不要问我怎么想到的)
但是如果n为偶数怎么办呢 就取两个中间位置的数那段区间呗~
本以为随便搞搞
| n的取值 | 干啥 |
|---|---|
| n为奇数 | 找到中间点 |
| n为偶数 | 找到矩形区间 |
就像酱紫,就完了呢….
然而 我想简单了….
还有几步没有想到
1. 中间的那个点被牛占了怎么办
2. 矩形区间里面有牛怎么办
对于第一个问题
遍历它旁边的所有点(上下左右) 找到合适的点(可能是好几个)并统计
再重复一遍这句话“没有两头牛的吃草位置是相邻的”
对于第二个问题 判个重就OK了……
最后放一个毕克大爷的数据
(我跟他的结果一样 嘿嘿)
(不一样不就惨了嘛……)
//By SiriusRen
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
int n,xx[]={1,-1,0,0},yy[]={0,0,1,-1};
struct Point{int x,y;}point[10005];
bool cmp1(Point a,Point b){return a.x<b.x;}
bool cmp2(Point a,Point b){return a.y<b.y;}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&point[i].x,&point[i].y);
if(n&1){
int ans=0,l,r;
sort(point+1,point+1+n,cmp1);
int ansx=point[n/2+1].x;
sort(point+1,point+1+n,cmp2);
int ansy=point[n/2+1].y;
for(l=n/2+1;l>=1;l--)
if(point[l].y!=point[l-1].y)break;
for(r=n/2+1;r<=n;r++)
if(point[r].y!=point[r+1].y)break;
for(int i=l;i<=r;i++){
if(i==r) {
for(int i=1;i<=n;i++){
ans+=fabs(point[i].x-ansx);
ans+=fabs(point[i].y-ansy);
}
printf("%d 1\n",ans);
return 0;
}
if(ansx==point[i].x)goto end;
}
end:int answer=0x3ffffff,temp;
for(int i=0;i<=3;i++){
ans=0;
int tempx=ansx+xx[i];
int tempy=ansy+yy[i];
for(int i=1;i<=n;i++){
ans+=fabs(point[i].x-tempx);
ans+=fabs(point[i].y-tempy);
}
if(answer>ans)answer=ans,temp=1;
else if(answer==ans)temp++;
}
printf("%d %d\n",answer,temp);
}
else{
sort(point+1,point+1+n,cmp1);
int ansx=point[n/2].x,ans=0,ansx2=point[n/2+1].x,recs=0;
sort(point+1,point+1+n,cmp2);
int ansy=point[n/2].y,ansy2=point[n/2+1].y;
for(int i=1;i<=n;i++)
ans+=fabs(point[i].x-ansx),ans+=fabs(point[i].y-ansy);
for(int i=1;i<=n;i++)
if(point[i].x>=ansx&&point[i].x<=ansx2&&point[i].y>=ansy&&point[i].y<=ansy2)
recs++;
printf("%d %d\n",ans,(ansx2-ansx+1)*(ansy2-ansy+1)-recs);
}
}
POJ 3269 中位数的更多相关文章
- poj 1723 中位数
最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...
- poj 2579 中位数问题 查找第K大的值
题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太 ...
- poj 3269 Building A New Barn
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- POJ 2388 Who's in the Middle(水~奇数个数排序求中位数)
题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #in ...
- POJ 1723 SOLDIERS (中位数)
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...
- poj 1723 SOLDIERS 带权中位数
题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...
- POJ 3784 Running Median(动态维护中位数)
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
- POJ 3784 Running Median (动态中位数)
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...
- POJ 3784 Running Median【维护动态中位数】
Description For this problem, you will write a program that reads in a sequence of 32-bit signed int ...
随机推荐
- [POJ 1316] 树上的询问
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1316 [算法] 点分治 由于边权较大,笔者在计算时使用了STL-set 注意当询问为 ...
- SPOJ COT2 Count on a tree II (树上莫队)
题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...
- HttpClient连接超时及读取超时
HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接时间 所谓连接的时候 是HttpClient发送请求 ...
- Windows平台下如何使用node.js显示系统盘符
本文地址: http://www.cnblogs.com/blackmanba/articles/windows-nodejs-show-system-letter.html或者http://fork ...
- 微信小程序引用阿里巴巴矢量图标iconfont
最近在写微信小程序,但是引用图片,导致项目文件太大,所以就想到引用阿里巴巴矢量图标的方法 第一步:下载阿里巴巴矢量图代码: 第二步:将下载下来的文件中iconfont.ttf转换即可.转换地址:htt ...
- Java对象、Json、Xml转换工具Jackson使用
在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...
- Linux apache tomcat
[root@node1 ~]# mv jdk-7u79-linux-x64.tar.gz /usr/local/[root@node1 ~]# cd /usr/local/[root@node1 lo ...
- 修改properties文件后系统运行异常
今天修改了项目的properties配置文件以后,运行会报异常,即使将内容改回,异常仍然存在.中间还会出现项目报错等问题,现将解决方法整理出来. 1.修改properties的打开方式,将打开方式从p ...
- DDD中Dto领域驱动设计概述,摘自《NET企业级应用架构设计》
- 如何打开DOS控制台及常见DOS命令作用
如何打开DOS控制台? * A:xp下如何打开DOS控制台? * a:开始--程序--附件--命令提示符 * b:开始--运行--cmd--回车 * c:win+r--cmd- ...