POJ 1723 SOLDIERS
SOLDIERS
This problem will be judged on PKU. Original ID: 1723
64-bit integer IO format: %lld Java class name: Main
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 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
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的更多相关文章
- POJ 1723 SOLDIERS (中位数)
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...
- OpenJudge/Poj 1723 SOLDIERS
1.链接地址: http://bailian.openjudge.cn/practice/1723/ http://poj.org/problem?id=1723 2.题目: 总时间限制: 1000m ...
- 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 中位数
最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...
- 【POJ 1723】 SOLDIERS
[题目链接] http://poj.org/problem?id=1723 [算法] 中位数 [代码] #include <algorithm> #include <bitset&g ...
- POJ 1723
#include <iostream> #include <algorithm> #define MAXN 10005 using namespace std; struct ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
随机推荐
- Codeforces 987B. High School: Become Human
解题思路: 1.题意:判断x^y和y^x谁大谁小. 2.由于x^y和y^x太大了,时间复杂度也不允许,所以做同等变换,比较e^(ylnx)和e^(xlny). 3.即为比较ylnx和xlny的大小. ...
- HTML基础——网站信息显示页面
1.语法和规范 HTML文件都是以.html或者.htm结尾的.建议使用.html结尾. HTML文件分为头部分(<head></head>)和体部分(<body> ...
- hiho 1613 - 墨水滴 - bfs+优先队列 *
题目链接 小Ho有一张白纸,上面有NxN个格子.小Ho可以选择一个格子(X, Y),在上面滴一滴墨水.如果这滴墨水的颜色深度是G,那么这个格子也会被染成深度为G的格子.同时周围的格子也会被这滴墨水浸染 ...
- 连类比事-category和extension
extension看起来很像一个匿名的category,但是extension和有名字的category几乎完全是两个东西. extension在编译期决议,它就是类的一部分,在编译期和头文件里的@i ...
- NOIp2018模拟赛四十四
加量不加价?! 昨晚看时间变成了3.5h以为终于变成了正常难度,结果还是国家集训队作业... A题看起来很神仙,B题看上去很神仙,C题一看就知道很神仙: 结果发现B是假题,放榜后发现A也是假题,C是Y ...
- javascript中实现继承的几种方式
javascript中实现继承的几种方式 1.借用构造函数实现继承 function Parent1(){ this.name = "parent1" } function Chi ...
- PHP中使用DOM读取解析XML属性值一例
先看XML文件结构,与常见的文件略有不同,数据并不是用闭合标签保存的,而是直接保存在属性值中. <?xml version="1.0" encoding="utf- ...
- redis 多实例监控
1.制作redis_low_discovery.sh脚本 mkdir -p /data/service/script/zabbix cd /data/service/script/zabbix cat ...
- php7 memcache和memcached.so扩展
php7安装memcache和memcached扩展 https://github.com/websupport-sk/pecl-memcache https://github.com/php-mem ...
- 网页里如何使用js屏蔽鼠标右击事件
图片.png 在后台管理系统里面,遇到了这样的一个问题,右击ztree菜单,弹出修改界面,但是,现在确实这样的,右击默认弹出功能提示的框框,看上去似乎很影响自己想要的功能,只能禁用了,那么,网页里如何 ...