题目描述
在一个划分成网格的操场上, n个士兵散乱地站在网格点上。由整数 坐标 (x,y) 表示。士兵们可以沿网格边上、下左右移动一步,但在同时刻任一网格点上只能有1名士兵。按照军官的命令,士兵们要整齐地列成个水平队列,即排成 队列,即排成 (x,y),(x+1,y), …,(x+n -1,y) 。如何选择 x 和y的值才能使 士兵们以最少的总移动步数排成一列。
输入
文件的第 1 行是士兵数 n,1≤n≤10000 。接下来 n 行是士兵的初始位置, 每行 2 个整数 x 和y,-10000 ≤x,y≤10000 。
输出
文件中 只有一个整 数是士兵排成一行需要的最少移动步。
样例输入 Copy
5 1 2 2 2 1 3 3 -2 3 3
样例输出 Copy
8
程序如下
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 2e5 + 10000;
int x[N], y[N];
int main()
{
int n,count=1;
cin >> n;
while (count <= n)
{
cin >> x[count] >> y[count];
count++;