POJ1723----SOLDIERS
来源:互联网 发布:行知小学 程洁 编辑:程序博客网 时间:2024/05/21 06:29
利用了中位数思想。
借鉴内容1 y方向:要使士兵最后位于同一水平线,则最终所有士兵的y坐标相同。将所有坐标的y值从小到大排序,对于首尾两个y值,移动到它们之间的任何y值所需要的步数是相同的,所以 最终的y值取中位数。y方向的步数y_steps=|y[0]-y_mid|+|y[1]-y_mid|+...+|y[n-1]-y_mid|,y_mid=y[n/2]。2 x方向:x方向稍微复杂点,先对所有x坐标从小到大排序,由于要移动步数最少,所以最终的x坐标相对位置与排序后的x坐标相对位置相同。
#include<stdio.h>#include<algorithm>#include<iostream>#include<math.h>#include<string>#include<string.h>#include<algorithm>using namespace std;int main(){ int n; int x[10010],y[10010]; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%d%d",&x[i],&y[i]); sort(y,y+n); int k=y[n/2]; int s=0; for(int i=0;i<n;i++) s+=fabs(k-y[i]); sort(x,x+n); for(int i=0;i<n;i++) x[i]-=i;//相对位置不变 sort(x,x+n); k=x[n/2]; for(int i=0;i<n;i++) s+=fabs(k-x[i]); printf("%d\n",s); }}
0 0
- POJ1723----SOLDIERS
- POJ1723.SOLDIERS 排序+思维
- POJ1723 SOLDIERS【中位数+排序】
- POJ1723,SOLDIERS,纯数学,猜想+证明
- poj1723
- poj1723
- poj1723
- poj1723
- poj1723
- soldiers
- poj1723(排序)
- Soldiers(0696)
- poj1723(士兵移动)
- poj1723 排序+中位数
- poj1723 中位数的应用
- PKU 1723 SOLDIERS
- spoj227. Ordering the Soldiers
- POJ 1723 SOLDIERS
- RAC 环境 下 抓取ID 大于 应用ID
- poj1009——编码题
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- codeforces 622f
- Java中内存分配及堆栈比较
- POJ1723----SOLDIERS
- Python练习(4):牛顿拉复生算法求解根
- Objective c手动写代码块
- DISPLAY environment variable not set!
- 树莓派系统终端中让文件和文件夹显示不同颜色的设置
- Codeforces 593A 2Char
- s2sh
- bzoj1208:[HNOI2004]宠物收养所-splay
- bootstrap + angularjs + springmvc + mybatis框架之加载log4j日志