zoukankan      html  css  js  c++  java
  • SOLDIERS(poj1723 排序/中位数/货舱选址问题扩展)

    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
    分析:最终要求所有人处于同一Y坐标,不同X坐标。
    所以:
    一、按照货舱选址的解法,|Y1-Y|+|Y2-Y|+……+|Yn-Y| Y取中位数即得最小结果;
    二、处理横坐标。按照X从小到大排序,点在序列中的相对位置与最终在坐标系中的相对位置相同。
      设X为最终状态下最左边的点。则 |X1-X|+|X2-X-1|+……+|Xn-X-n+1|
      先将所有Xi减去i,即转化为货舱选址中的情况。|X1-X|+|X2-X|+……+|Xn-X|


    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <cstdio>
    
    using namespace std;
    
    const int maxn = 1e4 + 100;
    const int mod = 998244353;
    const int inf = 0x3f3f3f3f;
    
    
    struct node {
        int x, y;
    } e[maxn];
    
    
    bool cmp1(node a, node b) {
        return a.y < b.y;
    }
    
    bool cmp2(node a, node b) {
        return a.x < b.x;
    }
    
    int main() {
        freopen("input.txt", "r", stdin);
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> e[i].x >> e[i].y;
        sort(e, e + n, cmp1);
        int ans = 0;
        for (int i = 0; i < n / 2; ++i) {
            ans = ans + e[n - i - 1].y - e[i].y;
        }
        sort(e, e + n, cmp2);
        for (int i = 0; i < n; i++) {
            e[i].x -= i;
        }
        sort(e, e + n, cmp2);
        for (int i = 0; i < n / 2; ++i) {
            ans = ans + e[n - i - 1].x - e[i].x;
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    表观遗传学|
    Mutation|DNM|
    Right journal|First-class paper|Mediocre paper|figure legend |Discussion|Introduction
    PCoA|NMDS|STRESS|RDA |RA|Unimodal|CCA|Generalized Joint Attribute Modeling
    缺失值|回归分析|协变关系|
    基金写作流程|留学回国人员启动资金
    nexage video asset tag
    获取的时候报cannot find package "golang.org /x/net/context",编译也报错误
    google play apk 下载
    vim golang 插件
  • 原文地址:https://www.cnblogs.com/albert-biu/p/9382181.html
Copyright © 2011-2022 走看看