OpenJudge/Poj 1723 SOLDIERS
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的更多相关文章
- POJ 1723 SOLDIERS (中位数)
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...
- poj 1723 SOLDIERS 带权中位数
题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...
- POJ 1723 SOLDIERS
SOLDIERS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 1 ...
- poj 1723 Soldiers【中位数】By cellur925
题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...
- poj 1723 中位数
最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...
- 【POJ 1723】 SOLDIERS
[题目链接] http://poj.org/problem?id=1723 [算法] 中位数 [代码] #include <algorithm> #include <bitset&g ...
- OpenJudge / Poj 2141 Message Decowding
1.链接地址: http://poj.org/problem?id=2141 http://bailian.openjudge.cn/practice/2141/ 2.题目: Message Deco ...
- OpenJudge/Poj 2105 IP Address
1.链接地址: http://poj.org/problem?id=2105 http://bailian.openjudge.cn/practice/2105 2.题目: IP Address Ti ...
- OpenJudge/Poj 2027 No Brainer
1.链接地址: http://bailian.openjudge.cn/practice/2027 http://poj.org/problem?id=2027 2.题目: 总Time Limit: ...
随机推荐
- 46 关于Linux的I/O重定向
I/O重定向是一个过程,这个过程捕捉一个文件.或命令.或程序.或脚本.甚至代码块(code block)的输出,然后把捕捉到的输出,作为输入发送给另外一个文件.或命令.或程序.或脚本. 1.I/O重定 ...
- 初学Node.js第一天
最近开始下班到家不知道该做啥,因为水平太菜,要学的东西实在太多,反而陷入了不知道该学什么的困境,结果天天就是看别人的博客,看到什么标题比较感兴趣就点进去,没有一个目标. 今天突然兴起,决定要捣鼓捣鼓N ...
- UNIX标准化及实现之功能测试宏
在头文件中定义了很多POSIX.1和XSI的符号.但是除了POSIX.1和XSI的定义之外,大多数实现在这些头文件中也加上了它们自己的定义.如果在编译一个程序时,希望它只使用POSIX定义而不使用任何 ...
- php测试代码工具类
<?php error_reporting (E_ALL); ini_set ('display_errors', 'on'); ?> <meta http-equiv=&quo ...
- linux crt
1.仿真 终端选linux ANSI颜色[有颜色了] 使用颜色方案[颜色加深了] 2.外观 选传统的 ,utf-8 就不会乱码了
- FBReaderJ 编译Jni
最近要做一个电子书项目用到FBReaderJ 第一步,也是最难的一步,要编译他的Jni 文件,对于android开发还是小白的我,只能说难!好难!非常难!,于是乎百度了一下,找到几篇有价值性的文章 第 ...
- ArcGIS中的影像色彩校正(转)
ArcGIS中的影像色彩校正 在遥感影像处理工作中,影像镶嵌是最常用影像处理方式之一,关于影像镶嵌的相关功能,在ArcGIS中有多种实现途径,如使用Mosaic工具.创建镶嵌数据集来进行虚拟镶嵌等等. ...
- JAVA_Reflection1
package com.qf.reflection1; import java.lang.reflect.Constructor; import java.lang.reflect.Field; im ...
- Disruptor框架
http://developer.51cto.com/art/201306/399370.htm
- 一些ecplise 快捷键
1 F5:下一步,可以进入下一个函数栈 2 F6:当前函数的下一步,不会进入其他的函数. 3 F8:下一个断点. 4 选中一个变量或者表达式,按ctrl+shift+i 来查看内容或者添加监视的方式. ...