小易将n个棋子摆放在一张无限大的棋盘上。第i个棋子放在第x[i]行y[i]列。同一个格子允许放置多个棋子。每一次操作小易可以把一个棋子拿起并将其移动到原格子的上、下、左、右的任意一个格子中。小易想知道要让棋盘上出现有一个格子中至少有i(1 ≤ i ≤ n)个棋子所需要的最少操作次数.
输入描述:
输入包括三行,第一行一个整数n(1 ≤ n ≤ 50),表示棋子的个数 第二行为n个棋子的横坐标x[i](1 ≤ x[i] ≤ 10^9) 第三行为n个棋子的纵坐标y[i](1 ≤ y[i] ≤ 10^9)
输出描述:
输出n个整数,第i个表示棋盘上有一个格子至少有i个棋子所需要的操作数,以空格分割。行末无空格 如样例所示: 对于1个棋子: 不需要操作 对于2个棋子: 将前两个棋子放在(1, 1)中 对于3个棋子: 将前三个棋子放在(2, 1)中 对于4个棋子: 将所有棋子都放在(3, 1)中
示例1
输入
4 1 2 4 9 1 1 1 1
输出
0 1 3 10
错误解法(会超内存):
class Help{
//递归求解组合数C(n,m),将组合结果放到allList中
public static void recurse(int k, int n, int m, List<List<Integer>> allList, List<Integer> nowList){
//k代表当前组合到了第几个数
List<Integer> newNowList = new ArrayList<>(nowList);
if(k == m){//递归出口,最后一位数仍然有n-m+1种情况,所以都列出
List<Integer> alPut = new ArrayList<>();
for(int c = 0; c < n - m + 1; c++){
newNowList = new ArrayList<>(nowList);
for(int i = nowList.size() > 0 ? nowList.get(nowList.size() - 1) + 1 : 0; i < n; i++){//取出上次排的最后一个元素,并加1为下一个元素
if(!newNowList.contains(i) && !alPut.contains(i)){
newNowList.add(i);
alPut.add(i);
allList.add(newNowList);
break;//插入了就跳出这个循环
}
}
}
return;
}
newNowList = new ArrayList<>(nowList);
//而且最大的一个数是n-m,因为是组合,所以上界停在这里(只针对第一种情况),所以要判断一下
int top = 0;
if(k == 1){
top = n - m + 1;
} else{
top = n;
}
//取出上次排的最后一个元素,并加1为下一个元素
for(int i = nowList.size() > 0 ? nowList.get(nowList.size() - 1) + 1 : k - 1; i < top; i++){//每次考虑n位数,由于这里是组合,所以就从k开始,前面的数已经放进去了,就不考虑了
newNowList = new ArrayList<>(nowList);
if(!newNowList.contains(i)){
newNowList.add(i);
recurse(k + 1, n, m, allList, newNowList);
}
}
}
}
public class Test {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] x = new int[n];
int[] y = new int[n];
int[] res = new int[n];
for(int i = 0; i < n; i++){
x[i] = in.nextInt();
}
for(int i = 0; i < n; i++){
y[i] = in.nextInt();
}
//挑选出n个格子的边界范围
//a,b代表边界矩形的左上角坐标
int a = Integer.MAX_VALUE;
int b = Integer.MIN_VALUE;
//c,d代表边界矩形的右下角坐标
int c = Integer.MIN_VALUE;
int d = Integer.MAX_VALUE;
for(int i = 0; i < n; i++){
a = Math.min(a, x[i]);
c = Math.max(c, x[i]);
b = Math.max(b, y[i]);
d = Math.min(d, y[i]);
}
//System.out.println("a:" + a + " b:" + b);
//System.out.println("c:" + c + " d:" + d);
//开始计算结果
res[0] = 0;//根据题意
for(int i = 2; i <= n; i++){
int min = Integer.MAX_VALUE;
//挑选出n个格子的边界矩形,依次判断哪个格子能放的长度最小(最小不一定是来自之前放了棋子的格子),但还是要挑选,只不过放到的格子的范围扩大了
//现在放i个格子到边界矩形中的某个格子中去
int tempSum = Integer.MAX_VALUE;//用于求和
for(int j = a; j <= c; j++){//行
for(int k = d; k <= b; k++){//列 (j,k)才是格子的坐标
List<List<Integer>> allList = new ArrayList<>();
List<Integer> nowList = new ArrayList<>();
//从n个格子中挑出i个格子
Help.recurse(1, n, i, allList, nowList);
for(int ll = 0; ll < allList.size(); ll++){
nowList = allList.get(ll);
//System.out.println("当前格子坐标:(" + k + "," + j + ") " + nowList);
tempSum = 0;
for(int ff = 0; ff < nowList.size(); ff++){
int loc = nowList.get(ff);
tempSum += Math.abs(x[loc] - j) + Math.abs(y[loc] - k);
}
min = Math.min(min, tempSum);
}
}
}
min = Math.min(min, tempSum);
res[i - 1] = min;
}
for(int i = 0; i < n - 1; i++){
System.out.print(res[i] + " ");
}
System.out.println(res[n - 1]);
in.close();
}
}