POJ 1723 SOLDIERS (水)
来源:互联网 发布:linux重启oracle命令 编辑:程序博客网 时间:2024/06/06 18:06
Description
给出士兵的位置(x, y)让士兵排成一排,计算士兵移动的最小的距离
Sample Input
5
1 2
2 2
1 3
3 -2
3 3
Sample Output
8
贪心
先将他们移动到同一横排,这横排应该是他们纵坐标的中位数,才能使得此过程总步数最少。然后要把他们紧凑起来。我们先把横坐标排序,并假设起点是a,那么我们就是要求abs(a+i - xi),i=1~n的加和。即abs(a-(xi-i)),i=1~n的加和。我们构建一个新数列zi = xi - i,当a等于z的中位数时原式的值最小
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int MAX = 10010;int x[MAX];int y[MAX];int z[MAX];int main(){ int N; while (~scanf("%d", &N)) { memset(x, 0, sizeof(x)); memset(y, 0, sizeof(x)); memset(z, 0, sizeof(x)); for (int i = 0; i < N; i++) { scanf("%d%d", &x[i], &y[i]); } sort(x, x + N); sort(y, y + N); for (int i = 0; i < N; i++) { z[i] = x[i] - i; } sort(z, z + N); int ans_x = z[N / 2], ans_y = y[N / 2], res = 0; for (int i = 0; i < N; i++) { res += abs(z[i] - ans_x); res += abs(y[i] - ans_y); } printf("%d", res); } return 0;}
阅读全文
0 0
- POJ 1723 SOLDIERS (水)
- POJ 1723 SOLDIERS(水~)
- POJ 1723 SOLDIERS
- POJ--1723--SOLDIERS
- Poj 1723 SOLDIERS
- POJ 1723 SOLDIERS
- poj 1723 SOLDIERS 中位数
- poj 1723(SOLDIERS)
- POJ 1723 SOLDIERS
- POJ 1723 SOLDIERS 笔记
- poj 1723Soldiers 带全中位数
- [POJ 1723]SOLDIERS(带权中位数问题)
- POJ - 1723 Soldiers 士兵站队 排序+中位数
- PKU 1723 SOLDIERS
- 北大oj 1723 SOLDIERS
- soldiers
- POJ1723----SOLDIERS
- Soldiers(0696)
- 总结: Java BigInteger类在ACM中的应用
- tensorflow学习系列(一)
- AJAX请求状态码返回200却跳到error的function
- iOS架构设计-URL缓存(下)
- 基础矩阵与本质矩阵
- POJ 1723 SOLDIERS (水)
- 数据库字段 驼峰风格 互相转换
- 暑期项目开发实训 Day22
- 标识符和数据类型
- HDU 5831 Rikka with Parenthesis II (括号匹配)
- 代码风格规范
- 算法题练习系列之(十三): 月饼
- tomcat 启动失败问题:Server Tomcat v8.0 Server at localhost failed to start.
- 无法将项目和文件的编码格式从UTF-8转为GBK