博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SOLDIERS
阅读量:5766 次
发布时间:2019-06-18

本文共 2271 字,大约阅读时间需要 7 分钟。

问题 b: SOLDIERS

时间限制: 1 Sec  内存限制: 128 MB
提交: 32  解决: 17
[] [] [] [命题人:]

题目描述

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.
 

 

样例输入

复制样例数据

51 22 21 33 -23 3

样例输出

8 思路:y坐标取中位数,x坐标转换为右值相等即可。
#include
#include
using namespace std;#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)#define PER(i, a, b) for(int i = (a); i >= (b); -- i)#define inf 1e50template
inline void rd(T &ret){ char c; ret = 0; while ((c = getchar()) < '0' || c > '9'); while (c >= '0' && c <= '9'){ ret = ret * 10 + (c - '0'), c = getchar(); }}const int maxn=1e4+5;int n,p[maxn],q[maxn];long long ans;int main(){ rd(n); REP(i,1,n){ cin>>p[i]>>q[i]; } sort(p+1,p+n+1); sort(q+1,q+n+1); REP(i,1,n)p[i]-=i; sort(p+1,p+n+1); int a=p[n/2+1],b=q[n/2+1]; REP(i,1,n){ ans+=(abs(p[i]-a)+abs(q[i]-b)); } cout<
<

 

转载于:https://www.cnblogs.com/czy-power/p/10362355.html

你可能感兴趣的文章
okhttp cancel() 导致Crash NetworkOnMainThreadExcepti
查看>>
Spring概述
查看>>
最穷无非讨饭,不死终会出头
查看>>
日志记录
查看>>
用cookie防刷新当前位置菜单
查看>>
h5 CSS RESET
查看>>
apache shiro与spring整合、动态filterChainDefinitions、以及认证、授权
查看>>
Linux的uniq和diff命令
查看>>
Linux下修改时区的方法
查看>>
docker在已有的tomcat镜像上打新的镜像的Dockerfile编写说明
查看>>
极客学院Python文本爬虫
查看>>
输入框input类型为number时,去掉上下箭头
查看>>
Spring MVC AOP通过注解方式拦截Controller等实现日志管理
查看>>
Hadoop HA现有解决方案
查看>>
【自用】 Selection sort by Python
查看>>
Tomcat性能调优-让小猫飞奔
查看>>
UITextField一些技巧总结
查看>>
python selectors 模块应用
查看>>
关于smarty中cache的设置
查看>>
关于Spring中自动装配的总结
查看>>