poj1723
来源:互联网 发布:编程和数学的关系 编辑:程序博客网 时间:2024/06/05 19:57
这是一个中规中矩的排序题,只要想清楚中位数的作用即可。
前言
相信大家中学时代一定都做过许多与绝对值相关的数学题,那么其实本题求的是距离,距离是什么??反映到数学上,不就是绝对值吗??
对于一个数列,其各点到此数列的中位数处的距离之和是最短的,这个我们初中就会。
因此,分别对两个数列进行排序对于纵坐标,直接求出各点到中位数处的距离即可。对于横坐标,我们假设所选取的最终基准点第一点为position,那么,所要求的距离即为abs(position+i-x【i】)即可。。即为,(position+(x【i】-i))这样我们构造一个新的数组z【i】=x【i】-i;显然,position为z【i】中位数时所要求的x轴上的路径最短,两者加和
以下为我AC的代码
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define size 10004 //here need't a";"
int main()
{
int x[size];
int y[size];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x[i];
cin>>y[i];
}
sort(x,x+n);
sort(y,y+n);
int middle=(n+1)/2-1;
for(int i=0;i<n;i++)
{
x[i]-=i;
}
sort(x,x+n);
int length=0;
for(int i=0;i<n;i++)
{
length+=(abs(x[i]-x[middle])+abs(y[i]-y[middle]));
}
cout<<length<<endl;
return 0;
}
这其中#include <algorithm>是包含了STL头文件,更多关于此头文件用法可以参阅
祝你好运。。。。。。。。
- poj1723
- poj1723
- poj1723
- poj1723
- poj1723
- poj1723(排序)
- POJ1723----SOLDIERS
- poj1723(士兵移动)
- poj1723 排序+中位数
- poj1723 中位数的应用
- POJ1723.SOLDIERS 排序+思维
- POJ1723 SOLDIERS【中位数+排序】
- POJ1723,SOLDIERS,纯数学,猜想+证明
- 面试题之中位数的应用POJ1723
- ACM: 一道水题 poj1723
- 分治算法---poj1723【士兵排队】Soldier
- 分治算法--士兵排队(poj1723)
- LeetCode: Substring with Concatenation of All Words
- 虚拟网络计算 (Virtual NetWork Computing)
- ADB OVER WIFI
- 用JAVA 编写程序从键盘读入10个整数存入整型数组a中
- POJ 2492 - A Bug's Life
- poj1723
- c/c++-c++中的临时对象问题
- Android4.0.1中各个Player的功能
- 交互方面的UE技术
- c++快速排序
- 机房收费 过程
- JavaEE版本的Eclipse连接MySQL数据库
- AndroidMainfest.xml文件解释
- thrift之compiler源码详解-MD5