POJ 1723 SOLDIERS (中位数)
题目大意:
平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数。
算法讨论:
表示自己太弱弱了,打算从今天开始提高一下智商。
我们考虑,既然是要成一条水平线,那么这条直线的y坐标肯定是所有y的中位数了。这是不用置疑的。所以我们只要求出中位数,然后对y坐标的距离差求下和就可以了。
对于x坐标,我们这样考虑,既然题目要求是最小步数,那么也就是说,这些博士兵在水平位置上的相对位置不变,换个意思说就是 原来三个士兵的x坐标是 -1 5 6,那么在他们移动之后,假设移动成一条直线之后,起点是原来-1的那个士兵,现在的坐标是9,那么他们之间的相对位置就是9 10 11...
那么,这样来说,我们就可以这样想:假设最后水平线的起点是a,根据上面的理论可以得到 x'[0] = a, x'[1] = a+1, x'[2] = a+2...
移项,可以得到x[0] - 0 = a, x[1] - 1 = a, x[2] - 2 = a.....
所以我们把每个x[i] 都减去i,然后再从小到大排序,这样就保证在相对位置不变的情况下成一条水平线。然后把距离差求和就可以了。
还值得一提的,下标从0开始的时候,mid = n / 2, 从1 开始的时候, mid = n / 2 + 1。。。。也是诡异。
Codes:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N = + ;
typedef long long ll; int n, mid;
int x[N], y[N];
ll step_x = , step_y = ; int main(){
while(~scanf("%d", &n)){
mid = n / + ;
step_x = step_y = ;
for(int i = ; i <= n; ++ i)
scanf("%d%d", &x[i], &y[i]); sort(y + , y + n + );
for(int i = ; i <= n; ++ i) step_y += abs(y[i] - y[mid]);
sort(x + , x + n + );
for(int i = ; i <= n; ++ i) x[i] -= i;
sort(x + , x + n + );
for(int i = ; i <= n; ++ i) step_x += abs(x[i] - x[mid]); printf("%lld\n", (ll) step_x + step_y);
} return ;
}
POJ 1723
POJ 1723 SOLDIERS (中位数)的更多相关文章
- 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)……的状态 ...
- 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 1723 中位数
最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...
- 【POJ 1723】 SOLDIERS
[题目链接] http://poj.org/problem?id=1723 [算法] 中位数 [代码] #include <algorithm> #include <bitset&g ...
- [POJ1723]SOLDIERS(中位数)
题意 给出n个点的坐标,它们只能往上.下.左.右一格一格地移动,求使其移动至水平线上的最小步数. 思路 转载 先易后难,对于纵向的问题,我们推个公式,,这个很容易看出是货仓选址问题,k取y[i]的中位 ...
- POJ 1723
#include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; struct ...
- POJ 2388 Who's in the Middle(水~奇数个数排序求中位数)
题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #in ...
随机推荐
- memcached介绍及基本使用
一:概念 memcached是LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件.现在已成为mixi,hatena,facebook ...
- 有关 sizeof和strlen的总结
先看看sizeof() 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出了其操作数的存储大小.操作数可以是 ...
- rsync实现文件备份同步(比如服务器镜像)
[rsync实现网站的备份,文件的同步,不同系统的文件的同步,如果是windows的话,需要windows版本cwrsync] 一.什么是rsync rsync,remote synchronize顾 ...
- 导入Excel表格到SharePoint站点
在Excel中创建表 选中所有要导入的行,点击菜单"插入"->"表格" 如果数据内容包含表头,选择"我的表格包含表头". 选择菜单 ...
- svn: “sqlite: attempt to write a readonly database”
原因很可能是在svn与本地同步的时候上锁了,可能没注意在svn执行与仓库同步的时候被中断,所以锁文件没有解锁,但是这样的错误,应该不是标题上所说的错误啊??搞不懂了,以前这样的错误,cleanup都有 ...
- find详解
一.简介 在目录结构中搜索文件,并执行指定的操作. 二.语法 find path -option [ -print ] [ -exec -ok command ] {} \;path: find命令所 ...
- 总结spring下配置dbcp,c3p0,proxool数据源链接池
转载自 http://hiok.blog.sohu.com/66253191.html applicationContext-datasource-jdbc.xml <?xml version= ...
- 可持久化Trie树
代码 ; struct PerTrie { ][ChSize]; ]; void init() { memset(next[],,])); inf[]=; id=; } int GetId(char ...
- 苹果拒绝App内部使用版本检测功能
10.6 - Apple and our customers place a high value on simple, refined, creative, well thought through ...
- FTP Client
1: /// <summary> 2: /// FTP 管理类 3: /// </summary> 4: public class FTPManage 5: { 6: priv ...