POJ1723 SOLDIERS【中位数+排序】

ACM ICPC 2018 World Finals

Language:
SOLDIERS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10226 Accepted: 3442

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.

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



问题链接POJ1723 SOLDIERS

问题简述

  有N个士兵,每个士兵开始站的坐标是(x,y),现在使得将N个士兵站在同一个水平线(即所有士兵的y坐标相同)并且x坐标相邻,每个士兵每次可以移动一个位置(分别在x和y方向移动)。求出最少的移动步数。

问题分析

  这是一个最优化问题,可以使用中位数计算来解决。

  x方向和y方向可以分别来考虑。

  y方向就是一个简单的中位数计算。

  x方向是坐标相邻,可以先将x位置排序,然后往中间靠,也是一个中位数计算问题。x方向分别移动到a+i的位置,那么士兵就相邻了,即x[i]=a+i,那么x[i]-i=a。用中位数来算的话,首先将x方向进行排序,然后让x[i]=x[i]-i,再次排序后求中位数即可。

程序说明:(略)

题记(略)


AC的C++语言程序如下:

/* POJ1723 SOLDIERS */

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10000;
int x[N], y[N];

int main()
{
    int n;

    while(cin >> n) {
        for(int i=0; i<n; i++)
            cin >> x[i] >> y[i];

        sort(x, x + n);
        for(int i=0; i<n; i++)
            x[i] -= i;
        sort(x, x + n);
        sort(y, y + n);

        int ans = 0, x_median, y_median;
        if(n % 2 == 1) {
            x_median = x[n / 2];
            y_median = y[n / 2];
        } else {
            x_median = (x[n / 2 - 1] + x[n / 2]) / 2;
            y_median = (y[n / 2 - 1] + y[n / 2]) / 2;
        }
        for(int i=0; i<n; i++)
            ans += abs(x_median - x[i]) + abs(y_median - y[i]);

        cout << ans << endl;
    }

    return 0;
}




  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值