妙笔难书一纸愁肠,苍白的誓言,终究抵不过岁月的遗忘。

题目描述

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.

输入

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.

输出

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.

样例输入

复制样例数据

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

样例输出

8

题意:给你n个士兵在棋盘里的坐标,要你将他们排成连续的一行(即与x轴平行),问你最少要将这些士兵移动多少步。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int N,x[10010],y[10010];
//struct node{
//    int x;
//    int y;
//}p[10010];
int main(){cin>>N;for(int i=1;i<=N;i++)cin>>x[i]>>y[i];sort(x+1,x+N+1);sort(y+1,y+N+1);for(int i=1;i<=N;i++)x[i]-=i;sort(x+1,x+N+1);int jg1=0,jg2=0,jg;for(int i=1;i<=N;i++){jg1+=abs(x[i]-x[(N+1)/2]);jg2+=abs(y[i]-y[(N+1)/2]);}jg=jg1+jg2;cout<<jg<<endl;return 0;
}

问题 c: SOLDIERS相关推荐

  1. G - SOLDIERS POJ - 1723 sort+数学

    G - SOLDIERS POJ - 1723 题解 好妙啊! 由于与路径没啥关系,走折线即可 先找出 y 的中位数,全部走到中位数的位置 对 x ,先排序,再构造一个 temp[i] = x[i] ...

  2. POJ 1723 Soldiers (中位数)

    $ POJ~1723~Soldiers $ (中位数) $ solution: $ 这道题说难也不算太难,但是当时自己想的很矛盾.所以还是列一篇题解. 这道题首先比较容易看出来的就是:行和列是两个分开 ...

  3. 编写Java程序,定义士兵类(Soldiers)并初始化5个士兵对象。

    返回本章节 返回作业目录 需求说明: 创建士兵类(Soldiers),定义有一个String类型参数name,代表士兵的姓名,两个int类型变量x和y,分别表示士兵所在的坐标位置,x代表横坐标,y代表 ...

  4. 【POJ 1723 --- SOLDIERS】

    [POJ 1723 --- SOLDIERS] Description N soldiers of the land Gridland are randomly scattered around th ...

  5. Soldiers(0696)

    Soldiers(0696) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1077 Accepted: 432 Accepted ...

  6. 【POJ 1723】SOLDIERS(排序、中位数)

    题面:SOLDIERS 题目大意 有 nnn 个士兵,并且知道每个士兵在二维坐标图上的位置. 士兵可以进行移动,但是每个士兵每次只能向上.向下.向左或向右移动一个单位,因此,他的 xxx 或 yyy ...

  7. 【POJ1723】SOLDIERS(中位数,货仓选址)

    problem 平面上有N(N<=10000)个点 求这些点变成一条水平线的最小移动步数 solution 对于y轴,易证得在中位数处取到最优,直接排序统计即可. 对于x轴,因为要保证相对顺序, ...

  8. poj1723 SOLDIERS

    看错题了无语...还以为只要在一排并且每个位置最多为1个士兵就可以... 原来是(x,y), (x+1,y), ..., (x+N-1,y), y取中点,x先减i,再去中点. 就是贪心- #inclu ...

  9. POJ 1723 SOLDIERS

    原题 Description: 平面直角坐标系中给定n个点, 每一次只能将一个点移动一个单位, 求移动的最小次数使得这n个点位于同一水平线上且全部相邻. Input: n:点的个数(1<= n ...

最新文章

  1. delphi xls 线程_锐龙9 5950X怒超近6GHz!关键是16核心32线程全开-锐龙,锐龙9 5950X,超频 ——快科技(驱动之家旗下媒体)-...
  2. boost::fusion::replace用法的测试程序
  3. 数据结构课程设计----基数排序
  4. 第十五章 Python和Web
  5. 第二天——hibernate讲完了
  6. python3爬取网易云歌单数据清洗_如何利用Python网络爬虫爬取网易云音乐歌词
  7. uva11549Calculator Conundrum
  8. 学习笔记(01):ThreeJS视频教程-了解着色器基础
  9. 人工智能--符号计算--笔记
  10. UOJ#198 [CTSC2016]时空旅行
  11. java autoconf_「Autoconf」- 安装 @20210202
  12. Java随笔——逻辑运算符和(|和||)之间的区别
  13. 设备预测性维护,为企业生产保驾护航!
  14. gdal命令实操专题——ogrinfo命令(转)
  15. 歌者绮贞 花的姿态演唱会...
  16. MediaPlayer 之音乐循环播放器
  17. 2022年PMP项目管理考试敏捷知识点(1)
  18. PCL 非线性最小二乘法拟合圆柱
  19. Rancher RKE K8s 集群 etcd 恢复
  20. 【有利可图网】PS教程:巧用PS简单快速设计制作一个发光文字效果

热门文章

  1. 集合 —— 集合与集合运算
  2. matlab的特殊字符
  3. manytomany
  4. XSS漏洞总结之小试牛刀
  5. B2B企业如何打造自己的私域流量池?
  6. Rockland丨Rockland蛋白质表达和纯化解决方案
  7. 【八卦趣事】富人对穷人的平均工资影响有多大
  8. Python描述 LeetCode 剑指 Offer 30. 包含min函数的栈
  9. 数据库 - 设计:逻辑设计
  10. 最爱你的男孩总把你挂在心上