小易将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
输出例子1:
0 1 3 10
思路:将x和y坐标组合起来一共有n*n钟,所有的情况都只会在这n*n个坐标上;这是可以证明的,只是说起来比较繁琐
AC代码:
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>
#include <iostream>
#include <math.h>
#include <string>
#include <queue>
#include <stdlib.h>
#include <map>
#include <set>
# define f(i,m,n) for(int i=m; i<=n; i++)
# define sf(x) scanf("%d", &x)
# define pfln(x) printf("%d\n", x);
# define pf(x) printf("%d", x);
# define mem(s) memset(s, 0, sizeof(s));
using namespace std;
typedef long long int ll;
const ll inf=(ll)2000000000*100;
ll x[110], y[110];
ll n;
vector<ll> v;
ll ans[100];
int main(){
sf(n);
f(i, 1, n)sf(x[i]);
f(i, 1, n)sf(y[i]);
f(k, 0, n-1)ans[k]=inf;
f(i, 1, n){
f(j, 1, n){
v.clear();
f(k, 1, n){
v.push_back(abs(x[k]-x[i])+abs(y[k]-y[j]));
}
sort(v.begin(), v.end());
ll sum=0;
f(k, 0, n-1){
sum=sum+v[k];
if(sum<ans[k])ans[k]=sum;
}
}
}
f(i, 0, n-1){
if(i==n-1){
printf("%lld\n", ans[i]);
}
else
printf("%lld ", ans[i]);
}
return 0;
}