2014
04-04

# Division

Little D is really interested in the theorem of sets recently. There’s a problem that confused him a long time.
Let T be a set of integers. Let the MIN be the minimum integer in T and MAX be the maximum, then the cost of set T if defined as (MAX � MIN)^2. Now given an integer set S, we want to find out M subsets S1, S2, …, SM of S, such that

and the total cost of each subset is minimal.

The input contains multiple test cases.
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains two integers N (≤ 10,000) and M (≤ 5,000). N is the number of elements in S (may be duplicated). M is the number of subsets that we want to get. In the next line, there will be N integers giving set S.

The input contains multiple test cases.
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given.
For any test case, the first line contains two integers N (≤ 10,000) and M (≤ 5,000). N is the number of elements in S (may be duplicated). M is the number of subsets that we want to get. In the next line, there will be N integers giving set S.

2
3 2
1 2 4
4 2
4 7 10 1

Case 1: 1
Case 2: 18

HintThe answer will fit into a 32-bit signed integer. 

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define N 10003
#define M 5002
using namespace std;
int f[N][M];
int s[N][M];
int a[N];
int main(){
int T,n,m;
scanf("%d",&T);
for (int cas=1;cas<=T;cas++){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for (int i=1;i<=n;i++){
f[i][1]=(a[i]-a[1])*(a[i]-a[1]);
s[i][1]=1;
}
for (int i=2;i<=m;i++){
s[n+1][i]=n-1;
for (int j=n;j>=i;j--){
f[j][i]=-1;
for (int k=s[j][i-1];k<=s[j+1][i];k++){
int tp=f[k][i-1]+(a[j]-a[k+1])*(a[j]-a[k+1]);
if (f[j][i]==-1||f[j][i]>tp){
f[j][i]=tp;
s[j][i]=k;
}
//printf("%d %d %d %d %d\n",i,j,k,f[j][i],s[j][i]);
}
}
}
printf("Case %d: %d\n",cas,f[n][m]);
}
return 0;
}


1. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术，也不懂什么是算法，从而也不要求程序员懂什么算法，做程序从来不考虑性能问题，只要页面能显示出来就是好程序，这是国内的现状，很无奈。

2. 算法是程序的灵魂，算法分简单和复杂，如果不搞大数据类，程序员了解一下简单点的算法也是可以的，但是会算法的一定要会编程才行，程序员不一定要会算法，利于自己项目需要的可以简单了解。