POJ1723.SOLDIERS 排序+思维
来源:互联网 发布:hadoop git 源码下载 编辑:程序博客网 时间:2024/05/21 09:59
1.题目描述:
SOLDIERS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8490 Accepted: 2993
Description
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.
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.
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
51 22 21 33 -23 3
Sample Output
8
Source
CEOI 1998
2.题意概述:
有N个士兵,每个士兵站的位置用一个坐标(x,y)表示,现在要将N个士兵站在同一个水平线,即所有士兵的y坐标相同并且x坐标相邻,每个士兵每次可以移动一个位置。求出最少的移动步数。
3.思路分析:
对x方向和y方向分开讨论。
1 y方向:
要使士兵最后位于同一水平线,则最终所有士兵的y坐标相同。
将所有坐标的y值从小到大排序,对于首尾两个y值,移动到它们之间的任何y值所需要的步数是相同的,所以 最终的y值取中位数。
y方向的步数y_steps=|y[0]-y_mid|+|y[1]-y_mid|+...+|y[n-1]-y_mid|,y_mid=y[n/2]。
2 x方向:
x方向稍微复杂点,先对所有x坐标从小到大排序,由于要移动步数最少,所以最终的x坐标相对位置与排序后的x坐标相对位置相同。
4.AC代码:
#include <iostream>#include <cstdlib>#define maxn 10004using namespace std;int a[maxn], b[maxn];int cmp(const void *a, const void *b){return *(int*)a - *(int*)b;}int main(){int test, mid_a, mid_b, ans = 0;cin >> test;for (int i = 0; i < test; i++)cin >> a[i] >> b[i];qsort(a, test, sizeof(a[0]), cmp);qsort(b, test, sizeof(b[0]), cmp);for (int j = 0; j < test; j++)a[j] -= j;qsort(a, test, sizeof(a[0]), cmp);mid_a = a[test / 2];mid_b = b[test / 2];for (int k = 0; k < test; k++)ans += abs(a[k] - mid_a) + abs(b[k] - mid_b);cout << ans << endl; return 0;}
0 0
- POJ1723.SOLDIERS 排序+思维
- POJ1723 SOLDIERS【中位数+排序】
- POJ1723----SOLDIERS
- poj1723(排序)
- POJ1723,SOLDIERS,纯数学,猜想+证明
- poj1723 排序+中位数
- poj1723
- poj1723
- poj1723
- poj1723
- poj1723
- soldiers
- POJ - 1723 Soldiers 士兵站队 排序+中位数
- Soldiers(0696)
- poj1723(士兵移动)
- poj1723 中位数的应用
- 排序与思维
- PKU 1723 SOLDIERS
- Windows使用PuTTY连接Ubuntu
- 42、Linux初步(CentOS 7下安装JDK)
- Repeating Decimals
- LeetCode 530. Minimum Absolute Difference in BST 解题报告
- 抽象工厂模式
- POJ1723.SOLDIERS 排序+思维
- 关于虚拟机中几种网络模式小结
- sqlite数据库拷贝覆盖时,数据库大小恢复到原来大小,内容却是新数据库不分内容
- NZT在线源安装教程
- 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
- 【bzoj3295】动态逆序对 CDQ分治
- Jenkins+Dokcer,run一个docker demo
- Java虚拟机:类加载器与双亲委派模型
- Apache http 服务器如何启用和设置 https 连接以提供加密的 http 通讯教程