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. 记一次mysql性能优化过程

    摘要: 所谓mysql的优化,三分是配置的优化,七分是sql语句的优化,通过一些案例分析,希望给大家在工作中带来一些思路 由于配置是运行过那么长时间,很稳定,基本上不考虑,所以本次主要是sql的优化, ...

  2. socket网络编程登录实现及多客户端和服务端的数据交互

    一.TCP/IP 客户端 package com.demo.entity; import java.io.Serializable; public class UserInfo implements ...

  3. vue路由跳转传参

    this.$router.push({ path: '/message/result_notice', query: { id: id } }) // let type = this.$route.n ...

  4. 服务器搭建域控与SQL Server的AlwaysOn环境过程(三)配置故障转移

    0 引言 主要讲述如何搭建故障转移集群,因为AlwaysOn是基于Windows的故障转移集群的. 在讲解步骤之前需要了解一下故障转移集群仲裁配置 下面图片来自<Windows Server20 ...

  5. WordPress 增加 keywords 和 description

    WordPress 增加 keywords 和 description . <?php $keywords = '798资源网'; $description = '798资源网'; //文章页 ...

  6. [arc082e]ConvexScore

    题意: 给出直角坐标系中的$N$个点$(X_i,Y_i)$,定义由其中部分点构成的点集为“凸点集”当且仅当这些点恰好能构成一个凸多边形(内部没有其他点). 如图,点集$\{A,C,E\}$和$\{B, ...

  7. PageUtil

    package cn.com.qmhd.oto.common; import java.io.Serializable; import java.util.List; import org.sprin ...

  8. django xadmin插件 的基本用法 1

    1  安装或导入 xadmin 1 pip 安装 2 源码导入 在新建项目中新建extra_apps文件夹并将下载后的源码解压放入 (推荐,方便后续我们可以在源码中自定义一些插件的使用) 注: 具体可 ...

  9. Zookeeper入门-Linux环境下异常ConnectionLossException解决

    实际项目开发中,用的是Linux环境.  中午突然断电,死活连不上Zookeeper,最终发现是需要关闭防火墙.    看日志,报错如下:  Exception in thread "mai ...

  10. C语言程序设计:现代方法阅读笔记

    第二十六章 atexit函数允许用户“注册”在程序终止时要调用的函数:atexit(func); 在程序终止后,func函数会被自动调用 clock()函数可以计算程序运行时间 time函数返回当前的 ...