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 ...
随机推荐
- Linux性能优化和监控系列(一)——top工具
解释服务器发生了什么——top工具 在检查服务器的详细工作性能状态前,系统管理员需要对当前服务器状态有总体的了解. top是检查服务器总体状态的强有力工具, 通过top可以获取CPU, Memory, ...
- POJ 3150 循环矩阵的应用
思路: 首先 先普及一个性质: 循环矩阵*循环矩阵=循环矩阵 由于此题是距离小于d的都加上一个数. 那么 构造矩阵的时候 我们发现 诶呦 这是个循环矩阵 看看数据范围 n^2log(k)可以过. 那就 ...
- node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)
最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. Mongoose是在node.js ...
- c# TextBox
1. text内容全选事件 textBox1.selectAll(); 2.失去与获取焦点事件 textox1.LostFocus += new EventHandler(txt_LostFocus) ...
- TFS源代码管理工具:
源代码管理: 先获取最新版本,再签入.如发现错误,可以点击--源代码管理--获取特定版本撤回修改 1.签入:(要备注,测试通过后签入) 敏捷开发:(小步快跑):小部分功能开发完成测试通过后就签入 全部 ...
- 移动端 | table 布局
<table border=” cellspacing="> <caption>表格标题</caption> <tr> <td alig ...
- 微信小程序引用阿里巴巴矢量图标iconfont
最近在写微信小程序,但是引用图片,导致项目文件太大,所以就想到引用阿里巴巴矢量图标的方法 第一步:下载阿里巴巴矢量图代码: 第二步:将下载下来的文件中iconfont.ttf转换即可.转换地址:htt ...
- Golden Gate 检查点
检查点是记录读写位置信息,在恢复时候要用到,保证事务的完整性. 两种存储方式: 存放在dirchk下 存放在指定的checkpoint table Replicat: nodbcheckpoint: ...
- BZOJ2440: [中山市选2011]完全平方数 容斥原理_莫比乌斯函数
emmm....... 数学题都不友好QAQ...... Code: #include <cstdio> #include <algorithm> #include <c ...
- 路飞学城Python-Day24(practise)
本章总结 练习题 什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件)