zoukankan      html  css  js  c++  java
  • POJ 1723 SOLDIERS

    SOLDIERS

    Time Limit: 1000ms
    Memory Limit: 10000KB
    This problem will be judged on PKU. Original ID: 1723
    64-bit integer IO format: %lld      Java class name: Main
     
    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

     
    解题:变幻莫测的中位数
     
     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 const int maxn = 10010;
     5 int x[maxn],y[maxn],n;
     6 int main() {
     7     while(~scanf("%d",&n)) {
     8         for(int i = 0; i < n; ++i)
     9             scanf("%d%d",x+i,y+i);
    10         sort(x,x+n);
    11         for(int i = 0; i < n; ++i) x[i] -= i;
    12         sort(x,x+n);
    13         int ret = 0;
    14         for(int i = 0; i < n; ++i)
    15             ret += abs(x[i] - x[n>>1]);
    16         sort(y,y+n);
    17         for(int i = 0; i < n; ++i)
    18             ret += abs(y[i] - y[n>>1]);
    19         printf("%d
    ",ret);
    20 
    21     }
    22     return 0;
    23 }
    View Code
  • 相关阅读:
    HBase 操作
    HBase Java API 例子
    微信浏览器拖动出现黑色/白色背景、网址问题解决方案
    layui弹出层置顶弹出
    使用layui时,ajax执行后,重新渲染页面的方法
    宝塔更新
    js 播放音频文件 兼容火狐 谷歌浏览器
    SAP断点
    error_log 用法
    SE开头的事务代码
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4627861.html
Copyright © 2011-2022 走看看