poj 1723 中位数
最近在看一些中位数的东西,然后顺便也看了些题目。poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列。
到y轴的距离好办,按y轴坐标排序,求中位数,然后求所有到中位数的距离和。
但是在x上怎么样才能最短呢?百思不得其解啊,最后看了这篇之后,豁然开朗。
x轴方向,先把x[]排好序,要想移动的距离最短,那么这时的相对位置肯定不变。那么假设a是这个队列的最左边的x坐标,那么它们的关系就有就有
x[0] -> a
x[1] -> a + 1
x[2] -> a + 2
........
x[i] -> a + i
即
x[0] -> a
x[1] - 1 -> a
x[2] - 2 -> a
.......
x[i] - i -> a
也就是要把这些点移动到固定的一个点,那么我们只要求出x[i]-i的中位数,就可以求出x轴移动的最短距离了。
那么我们就可以这样来做:对x,y排序, 然后再对x进行x[i] = x[i] - i,再排序,去除两个中位数,分别求距离的绝对值即可。
代码:
//poj 1732
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int n;
int *x, *y; int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
} void input()
{
int i = ;
/*
FILE *fp; fp = fopen("in.txt","r");
if (fp == NULL)
{
printf("FOPEN ERROR\n");
return;
}
*/ scanf("%d",&n);
//fscanf(fp,"%d",&n);
x = (int *)malloc(sizeof(int)*n);
y = (int *)malloc(sizeof(int)*n);
while (i < n)
{
scanf("%d%d",x+i,y+i);
//fscanf(fp,"%d%d",x+i,y+i);
i++;
}
} void free_buf()
{
free(x);
free(y);
} int main()
{
int sum = , i;
int mid_x, mid_y; input();
qsort(y,n,sizeof(int),cmp);
qsort(x,n,sizeof(int),cmp);
for (i = ; i < n; i++)
{
*(x+i) = *(x+i) - i;
}
qsort(x,n,sizeof(int),cmp); mid_y = *(y + n/);
mid_x = *(x + n/);
for (i = ; i < n; i++)
{
sum += abs(*(y+i) - mid_y);
sum += abs(*(x+i) - mid_x);
} printf("%d\n",sum);
free_buf(); return ; } 2013/7/25 23:41
poj 1723 中位数的更多相关文章
- POJ 1723 SOLDIERS (中位数)
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...
- poj 1723 SOLDIERS 带权中位数
题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...
- poj 1723 Soldiers【中位数】By cellur925
题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...
- 【POJ 1723】 SOLDIERS
[题目链接] http://poj.org/problem?id=1723 [算法] 中位数 [代码] #include <algorithm> #include <bitset&g ...
- OpenJudge/Poj 1723 SOLDIERS
1.链接地址: http://bailian.openjudge.cn/practice/1723/ http://poj.org/problem?id=1723 2.题目: 总时间限制: 1000m ...
- POJ 1723 SOLDIERS
SOLDIERS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 1 ...
- poj 2579 中位数问题 查找第K大的值
题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太 ...
- POJ 3269 中位数
题意: 思路: 这道题坑也不少.. 你准备好脑洞了么? 首先 要认真审题 题目中有说:"没有两头牛的吃草位置是相邻的" 这句话让我们省了很多的事儿 (Discuss里有的大神就入了 ...
- POJ 1723
#include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; struct ...
随机推荐
- shell 计算时间差
#!/bin/bash #date_5='awk 'BEGIN{print strftime("%H:%M",(systime()-300))}'' #ps -ef | grep ...
- cocos2d-x 模态对话框的实现
心情不好,恩.不扯淡了.直接讲. ================================== 在泰然看了一篇实现模态对话框的文章,写的还不错,然后在其基础上加了我简单加了一层灰色透明背景,这 ...
- [MeetCoder] Crossing Bridge
Crossing Bridge Description N people wish to cross a bridge at night. It’s so dark around there that ...
- WCF学习分享2
整个solution结构例如以下: 以下介绍每一个project: 1. Service.Interface 定义契约 ICalculator.cs watermark/2/text/aHR0cDov ...
- 深入源码分析Java线程池的实现原理
程序的运行,其本质上,是对系统资源(CPU.内存.磁盘.网络等等)的使用.如何高效的使用这些资源是我们编程优化演进的一个方向.今天说的线程池就是一种对CPU利用的优化手段. 通过学习线程池原理,明白所 ...
- 每日英语:How to Solve India's Huge Food Wastage
India is one of the world’s largest producers of fruits and vegetables, but a third of its produce ...
- grpc-golang实现账号and密码认证
// I would recommend to use interceptors: // client grpc.Dial(target, grpc.WithPerRPCCredentials(&am ...
- css基础 -文本溢出 text-overflow:ellipsis;
.className{overflow:hidden;white-space:nowrap;text-overflow:ellipsis;} 外部结构如下是就失效了:(移动端) <a class ...
- Windows下使用MINGW编译ffplay
之前考虑到需要快速配置编译ffplay,使用了比较暴力的方法,具体可以参考编译ffplay.exe简化版. 这里介绍下相对规范的做法. 前提:已经安装了Windows下GCC开发环境--MINGW+m ...
- 1. 感知机原理(Perceptron)
1. 感知机原理(Perceptron) 2. 感知机(Perceptron)基本形式和对偶形式实现 3. 支持向量机(SVM)拉格朗日对偶性(KKT) 4. 支持向量机(SVM)原理 5. 支持向量 ...