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
  • 相关阅读:
    js 回车调用后台事件
    获取下拉框选中的值:
    MVC 3.0 在各个版本IIS中的部署
    创建Windows域
    SQL Server 事务、异常和游标
    IIS配置PHP环境(快速最新版)
    js操作select下拉框
    如何清除访问远程网络时保存的密码
    免费Web服务
    Firefox不支持event解决方法
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4627861.html
Copyright © 2011-2022 走看看