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. 教你50招提升ASP.NET性能(四):精选的技巧

    (4)A selection of tips 招数4: 精选的技巧 Make sure HTTP compression is turned on for any uncompressed conte ...

  2. IPv6介绍

    一.为什么需要IPv6 为了扩大地址空间,拟通过IPv6重新定义地址空间.IPv4采用32位地址长度,只有大约43亿个地址,估计在2005-2010年间将被分配完毕,而IPv6采用128位地址长度,几 ...

  3. android驱动[置顶] 我的DIY Android之旅--驱动并控制你的Android开发板蜂鸣器

    改章节个人在深圳游玩的时候突然想到的...这几周就有想写几篇关于android驱动的博客,所以回家到之后就奋笔疾书的写出来发布了 这些天一直在想Android驱动框架层的实现,本文借助老罗教师的博客和 ...

  4. 配置Ubuntu开发环境

    前言 新买了一台ThinkPad E431,主要看中了硬盘500G和7200转/分钟的速度,因此准备从x220上把工作环境迁移到新买的笔记本上. 为什么不要公司的电脑,是由于160G的ssd硬盘实在是 ...

  5. CentOS命令行无线上网

    (1)首先关闭开发板的有线网卡[root@FriendlyARM /]# ifconfig eth0 down(2)加载USB WiFi无线网卡[root@FriendlyARM /]# ifconf ...

  6. IOS开发-加载本地音乐

    IOS开发-加载本地音乐 $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text() ...

  7. python爬虫之採集——360联想词W2版本号

    http://blog.csdn.net/recsysml/article/details/30541197,我的这个博文介绍了对应的简单的方法做一个联想词的爬虫,并且还承诺了下面优化: 下一版本号的 ...

  8. J2EE的13个规范之(三) Servlet简单介绍

    Servlet是一种server端脚本,它是一个特殊的Java类,继承自HttpServlet.开发中主要用于处理和响应client的请求. Servlet在容器中执行,事实上例的和销毁创建由容器进行 ...

  9. procps工具集 ----Linux中的可用内存指的是什么?

    https://gitlab.com/procps-ng/procps free - Report the amount of free and used memory in the system k ...

  10. unix 网路编程(卷一)第一个程序编译过程

    unix卷一去年暑假买的到现在才开始看无比惭愧,而且惭愧第一个程序就断断续续弄了几天,要好好写程序了,马上要找工作了,下面介绍下把本书第一个程序跑起来的过程: 搜各种博客 我用系统的是ubuntu 1 ...