poj1723 中位数的应用
来源:互联网 发布:淘宝服装图标 编辑:程序博客网 时间:2024/06/14 05:54
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).
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.
(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.
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
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
题意:已知有n个士兵,以及他们的坐标(x,y),最后他们必须移动到同一条水平线上。
(目标设为(k,y),(k+1,y)...(k+n-1,y))
每个士兵只能向上下左右移动,求最少的总移动步数。
(目标设为(k,y),(k+1,y)...(k+n-1,y))
每个士兵只能向上下左右移动,求最少的总移动步数。
思路:分别对x,y分析。
y坐标:要想使移动步数最少,即|y-y0|+|y-y1|+...+|y-y[n-1]|的和最小,只需y取中位数即可,得到结果为
最后一项减第一项,然后分别后移前移,进行计算。
x坐标:设x坐标最后为k,k+1,k+2,...k+n-1. 要想使移动步数最少,则|x0-k|+|x1-1-k|+|x2-2-k|+...+|x[n-1]-(n-1)-k|
的和最小,即k取(x0,x1-1x2-2,x3-3,...x[n-1]-(n-1)的中位数即可),计算同y。
#include <cstdio>#include <algorithm>#define N 10005using namespace std;int x[N], y[N];int main(){long long int ans;int n;while (scanf("%d", &n) != EOF){ans = 0;for (int i = 0; i < n; i++)scanf("%d%d", &x[i], &y[i]);sort(x, x + n);sort(y, y + n);for (int i = 0; i < n; i++)x[i] -= i;sort(x, x + n);for (int i = 0, j = n - 1; i < j; i++, j--)ans += y[j] - y[i] + x[j] - x[i];printf("%lld\n", ans);}return 0;}
0 0
- poj1723 中位数的应用
- poj1723 排序+中位数
- POJ1723 SOLDIERS【中位数+排序】
- 面试题之中位数的应用POJ1723
- poj1723
- poj1723
- poj1723
- poj1723
- poj1723
- 中位数的应用
- 上中位数的应用
- POJ 2313 中位数的应用
- 【中位数的应用】邮局设置
- 中位数应用
- 中位数的应用—士兵站队问题
- 中位数的中位数
- poj1723(排序)
- POJ1723----SOLDIERS
- Qt Creator 最实用的快捷操作
- 链表基础1
- NETCONF模块设计介绍
- css rem 的理解
- Android中自定义属性的使用
- poj1723 中位数的应用
- 【典型例题】遍历文件夹下面的文件;(递归算法)
- A+B (III)
- C语言栈帧破坏
- pugixml 丢失空格的解决方法
- 网络流Dinic
- 整合 spring 4(包括mvc、context、orm) + mybatis 3 示例
- 欢迎使用CSDN-markdown编辑器
- 2.1 VB语言基础——关键字和标识符