1.链接地址:

http://bailian.openjudge.cn/practice/1723/

http://poj.org/problem?id=1723

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
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.

输入
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 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.
样例输入
5
1 2
2 2
1 3
3 -2
3 3
样例输出
8
来源
CEOI 1998

3.思路:

4.代码:

 //1723_01.cpp
//2010-04-09 by wuzhihui #include<iostream>
#include<cstdio>
#include <cmath> using namespace std;
int x[],y[],c[]; //快速排序
int Partition(int a[],int low,int high)
{
int temp=a[low];
while(low<high)
{
while(low<high&&a[high]>=temp) high--;
a[low]=a[high];
while(low<high&&a[low]<=temp) low++;
a[high]=a[low];
}
a[low]=temp;
return low;
}
void QSort(int a[],int low ,int high)
{
int pivotloc;
if(low<high)
{
pivotloc=Partition(a,low,high);
QSort(a,low,pivotloc-);
QSort(a,pivotloc+,high);
}
}
void QuickSort(int a[],int size)
{
QSort(a,,size-);
} long long f(int x[],int y[],int size)
{
int i;
QuickSort(x,size);
for(i=;i<size;i++)
{
c[i]=x[i]-i;
}
QuickSort(y,size);
QuickSort(c,size);
long long count=;
for(i=;i<size;i++)
{
count+=abs(y[i]-y[size/])+abs(x[i]-(c[size/]+i));
}
return count;
}
int main()
{
//测试快排的正确性
/*int test[10]={9,8,7,6,5,4,3,2,1,0};
int testSize=10;
QuickSort(test,testSize);
for(int i=0;i<10;i++) {cout<<test[i]<<" ";}
cout<<endl; */ int size;
int i,j;
long long count;
while((cin>>size)&&size!=)
{
for(i=;i<size;i++)
{
cin>>x[i]>>y[i];
} //测试输入是否正确
/*for(i=0;i<size;i++)
{
cout<<x[i]<<" "<<y[i];
cout<<endl;
} */ count=f(x,y,size);
printf("%lld\n",count);
} //system("pause");
return ;
}

OpenJudge/Poj 1723 SOLDIERS的更多相关文章

  1. POJ 1723 SOLDIERS (中位数)

    题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...

  2. poj 1723 SOLDIERS 带权中位数

    题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...

  3. POJ 1723 SOLDIERS

    SOLDIERS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 1 ...

  4. poj 1723 Soldiers【中位数】By cellur925

    题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...

  5. poj 1723 中位数

    最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...

  6. 【POJ 1723】 SOLDIERS

    [题目链接] http://poj.org/problem?id=1723 [算法] 中位数 [代码] #include <algorithm> #include <bitset&g ...

  7. OpenJudge / Poj 2141 Message Decowding

    1.链接地址: http://poj.org/problem?id=2141 http://bailian.openjudge.cn/practice/2141/ 2.题目: Message Deco ...

  8. OpenJudge/Poj 2105 IP Address

    1.链接地址: http://poj.org/problem?id=2105 http://bailian.openjudge.cn/practice/2105 2.题目: IP Address Ti ...

  9. OpenJudge/Poj 2027 No Brainer

    1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...

随机推荐

  1. Objective-C中的Block(闭包) (轉載)

    来源: 伯乐在线 - 青玉伏案 链接:http://ios.jobbole.com/83229/ 学习OC有接触到一个新词Block(个人感觉又是一个牛气冲天的词),但不是新的概念,不是新的东西.学过 ...

  2. .Net Core静态文件中间件StaticFiles的使用

    以前,当我们的网站需要显示图片的时候,直接在网站目录下新建文件夹,把图片放在这个文件夹下,然后通过文件夹的路径就可以访问到. 但是在.net core中不可以这样,要通过中间件StaticFiles配 ...

  3. hiberante中get和load方法的区别

    1.从返回结果上对比: load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常 get方法检索不到的话会返回null 2.从检索执行机制上对比: ...

  4. windows7下硬盘安装ubuntu14.04

    windows7 ubuntu1404双系统 准备软件 安装步骤 step 1 step 2 step 3 step 4 windows7 + ubuntu14.04双系统 准备软件 1)grub4d ...

  5. 《Java并发编程实战》第十五章 原子变量与非堵塞同步机制 读书笔记

    一.锁的劣势 锁定后假设未释放.再次请求锁时会造成堵塞.多线程调度通常遇到堵塞会进行上下文切换,造成很多其它的开销. 在挂起与恢复线程等过程中存在着非常大的开销,而且通常存在着较长时间的中断. 锁可能 ...

  6. iOS开发——UI篇OC篇&不规则排列的图片布局

    不规则排列的图片布局 一直在500px上看照片,发照片.以前看它的首页图片展示就只是觉得好看,洋气,也没想过自己在iOS上实现一下.昨天不知怎么的就开始想其中的算法了,现在我把思考的过程在这里贴出来分 ...

  7. 5.6 在线DDL (online DDL)详解

    ONLINE ddl 原理: online ddl相关参数 测试原表数据是否能进行ONLINE DDL方法: online DDL 局限性 测试

  8. LINUX SHELL条件判断

    算术运算的条件判断 [] [[]]: -eq -ne -lt -le -gt -ge (( )):><>=<== [root@monitor ~]# if (( 2 == 3) ...

  9. JavaScript网站设计实践(五)编写photos.html页面,实现点击缩略图显示大图的效果

    一.photos.html页面,点击每一张缩略图,就在占位符的位置那里,显示对应的大图. 看到的页面效果是这样的: 1.实现思路 这个功能在之前的JavaScript美术馆那里已经实现了. 首先在页面 ...

  10. java_泛型 TreeSet 判断hashcode/length(升序排列)

    package ming; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; ...