SOLDIERS

Time Limit: 1000ms
Memory Limit: 10000KB

This problem will be judged on PKU. Original ID: 1723
64-bit integer IO format: %lld      Java class name: Main

 
N soldiers of the land Gridland are randomly scattered around the country. 
A position in Gridland is given by a pair (x,y) of integer coordinates. Soldiers can move - in one move, one soldier can go one unit up, down, left or right (hence, he can change either his x or his y coordinate by 1 or -1).

The soldiers want to get into a horizontal line next to each other (so that their final positions are (x,y), (x+1,y), ..., (x+N-1,y), for some x and y). Integers x and y, as well as the final order of soldiers along the horizontal line is arbitrary.

The goal is to minimise the total number of moves of all the soldiers that takes them into such configuration.

Two or more soldiers must never occupy the same position at the same time.

 

Input

The first line of the input contains the integer N, 1 <= N <= 10000, the number of soldiers. 
The following N lines of the input contain initial positions of the soldiers : for each i, 1 <= i <= N, the (i+1)st line of the input file contains a pair of integers x[i] and y[i] separated by a single blank character, representing the coordinates of the ith soldier, -10000 <= x[i],y[i] <= 10000.

 

Output

The first and the only line of the output should contain the minimum total number of moves that takes the soldiers into a horizontal line next to each other.

 

Sample Input

5
1 2
2 2
1 3
3 -2
3 3

Sample Output

8

Source

 
解题:变幻莫测的中位数
 
 #include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = ;
int x[maxn],y[maxn],n;
int main() {
while(~scanf("%d",&n)) {
for(int i = ; i < n; ++i)
scanf("%d%d",x+i,y+i);
sort(x,x+n);
for(int i = ; i < n; ++i) x[i] -= i;
sort(x,x+n);
int ret = ;
for(int i = ; i < n; ++i)
ret += abs(x[i] - x[n>>]);
sort(y,y+n);
for(int i = ; i < n; ++i)
ret += abs(y[i] - y[n>>]);
printf("%d\n",ret); }
return ;
}

POJ 1723 SOLDIERS的更多相关文章

  1. POJ 1723 SOLDIERS (中位数)

    题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...

  2. OpenJudge/Poj 1723 SOLDIERS

    1.链接地址: http://bailian.openjudge.cn/practice/1723/ http://poj.org/problem?id=1723 2.题目: 总时间限制: 1000m ...

  3. poj 1723 SOLDIERS 带权中位数

    题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...

  4. poj 1723 Soldiers【中位数】By cellur925

    题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...

  5. poj 1723 中位数

    最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...

  6. 【POJ 1723】 SOLDIERS

    [题目链接] http://poj.org/problem?id=1723 [算法] 中位数 [代码] #include <algorithm> #include <bitset&g ...

  7. POJ 1723

    #include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; struct ...

  8. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  9. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

随机推荐

  1. 脱离node自己使用普通的requirejs管理js资源

    首先,工程目录: 现在主页面(web框架写法.html): <!DOCTYPE html> <html lang="en"> <head> &l ...

  2. HDU 1175 连连看【BFS】

    题意:给出起点和终点的棋子,不能经过别的棋子,而且转弯的次数不能超过2次,问能否消除 和逃离迷宫一样,每个节点记录下来它的当前的方向和转弯的次数,再搜 注意特判起点的棋子和终点的棋子为0或者不一样的情 ...

  3. 路飞学城Python-Day15

    模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还 ...

  4. tinymce原装插件源码分析(二)-link

    link 功能描述如下: 单纯放置光标: 1.如果光标放到了<a>上,读取a标签的内容,并弹框显示,确定的时候,更新当前a标签. 2.否则,就创建弹框,确定的时候,按照参数添加a标签. s ...

  5. CSS常用样式--background

    CSS background 属性 参考:W3school- CSS background 所有浏览器都支持 background 属性,其简写形式,在一个声明中设置所有的背景属性,各属性需按顺序,语 ...

  6. python 比较数字大小按从大到小输出

    主要用到的python 的知识点 1:   内置函数max 2:     列表的操作 3:   while 循环 4 :  错误处理 代码如下: #!/usr/bin/python #coding=u ...

  7. 题解 洛谷 P3381 【【模板】最小费用最大流】

    发了网络流,再来一发费用流 能做费用流的,网络流自然做得来,但在这还是不要脸的安利一下自己的博客(里面也有网络流的题解): 点我 扯远了... 费用流,就是在不炸水管的情况下求源点到汇点的最小费用. ...

  8. 如何查询mysql中是否表被锁

    可直接在mysql命令行执行:show engine innodb status\G;(只能通过cmd或者shell登录mysql) 查看造成死锁的sql语句,分析索引情况,然后优化sql然后show ...

  9. servlet3.0 @webfilter 过滤顺序

    Servlet3.0之前Filter过滤的顺序是由用户在web.xml中配置的顺序决定的,如下会先执行encodingFilter,再执行filter1. <filter> <dis ...

  10. POJ1502 MPI Maelstrom Dijkstra

    题意 给出图,从点1出发,求到最后一个点的时间. 思路 单源最短路,没什么好说的.注意读入的时候的技巧. 代码 #include <cstdio> #include <cstring ...