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: ...
随机推荐
- mac不可或缺的套件管理器Homebrew
Homebrew 1.简介 可以帮助我们快速安装软件, 如 brew install svn 快速卸载软件 , 如 brew uninstall svn 2.下载 打开终端, /usr/bin/ru ...
- .NET常用工具类集锦
不错的地址: http://www.cnblogs.com/flashbar/archive/2013/01/23/helper.html https://github.com/chrisyanghu ...
- Jfinal极速开发微信系列教程(一)--------------Jfinal_weixin demo的使用分析
概述: Jfinal_weixin已经出了有好一段时间了!一直在关注当中......最近工作上有需要到这个东西,所以,话了两个小时来看看这个东西,看完demo以后,豁然开朗,原理微信和一般的web项目 ...
- HDU 5002 Tree LCT 区间更新
Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- Nginx详细配置
#运行用户#user nobody; #启动进程,通常设置成和cpu的数量相等或者2倍于cpu的个数(具体结合cpu和内存).默认为1worker_processes 1; #全局的错误日志和日志 ...
- js操作json添加元素和数据的方法
function addServerUrlToJson() { var json_tem = [{"name":"a","value":1} ...
- iOS开发——数据持久化Swift篇&iCloud云存储
iCloud云存储 import UIKit class ViewController: UIViewController { override func viewDidLoad() { super. ...
- Web.xml 中增加log4j
配置文件例如以下.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- C#_delegate - combine function
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dele ...
- phpcms v9 模板标签说明整理
1.{template "content","header"} 2.网站网址调用:{siteurl($siteid)}: 3.标签get:分页,{pc:get ...