AcWing 123. 士兵(Java)
原题链接
中等
作者:
上杉
,
2021-05-04 13:22:17
,
所有人可见
,
阅读 398
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/**
* @program: 算法题
* @author: 上杉
* @create: 2021-05-04 12:40
**/
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] xs = new int[n];
int[] ys = new int[n];
for (int i = 0; i < n; i++) {
xs[i] = input.nextInt();
ys[i] = input.nextInt();
}
Arrays.sort(ys);
int l = 0;int r = n - 1;
long res = 0;
while (l < r){
res += (ys[r--] - ys[l++]);
}
Arrays.sort(xs);
for (int i = 0,add = n/2; i < n; i++,add--) {
xs[i] += add;
}
Arrays.sort(xs);
l = 0;
r = n - 1;
while (l < r){
res += (xs[r--] - xs[l++]);
}
System.out.println(res);
}
}