AcWing 3666. 士兵排阵
原题链接
简单
作者:
JustDoIt11
,
2023-10-01 15:34:35
,
所有人可见
,
阅读 71
// package 算法保持;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
int[] x = new int[n];
int[] y = new int[n];
for (int i = 0; i < n; i ++ ) {
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
Arrays.sort(x);
for (int i = 0; i < n; i ++ ) {
x[i] -= i; // 少移动多少
}
Arrays.sort(x);
Arrays.sort(y);
int ax = 0;
int ay = 0;
for (int i = 0; i < n; i ++) {
ax += Math.abs(x[i] - x[n / 2]);
ay += Math.abs(y[i] - y[n / 2]);
}
System.out.println(ax + ay);
}
}
只需要让x再次统一即可, 减去i代表着少走几步