Division (斜率dp)

来源:互联网 发布:打淘宝客服要话费吗 编辑:程序博客网 时间:2024/05/01 20:24

Division

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 4348    Accepted Submission(s): 1709


Problem Description
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.
 

Input
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.

 

Output
For each test case, output one line containing exactly one integer, the minimal total cost. Take a look at the sample output for format.

 

Sample Input
23 21 2 44 24 7 10 1
 

Sample Output
Case 1: 1Case 2: 18
#include <set>#include <map>#include <stack>#include <queue>#include <deque>#include <cmath>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF  0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-9#define maxn 10010#define MOD 1000000007#define LL long longconst int mod = 998244353;int T;int n,m;int dp[12000][5200];int que[12000];int a[12000];void solve(){    for(int i=1;i<=n;i++)        dp[i][1] = (a[i]-a[1])*(a[i]-a[1]);    for(int i=2;i<=m;i++)    {        int fr=0,re=0;        que[re++] = i-1;        for(int j = i;j<=n;j++)        {            while(fr+1<re)            {                int yy = dp[que[fr+1]][i-1] + a[que[fr+1]+1] * a[que[fr+1]+1];                int yz = dp[que[fr]][i-1] + a[que[fr]+1] * a[que[fr]+1];                int xx = a[que[fr+1]+1];                int xz = a[que[fr]+1];                if(a[j] * 2 * (xx-xz)>=yy-yz) fr++;                else break;            }            dp[j][i] = dp[que[fr]][i-1] + (a[j] - a[que[fr]+1])*(a[j] - a[que[fr]+1]);            while(fr+1<re)            {                int yy = dp[que[re-1]][i-1] + a[que[re-1]+1] * a[que[re-1]+1] - (dp[que[re-2]][i-1] + a[que[re-2]+1] * a[que[re-2]+1]);                int yz = dp[j][i-1] + a[j+1] * a[j+1] - (dp[que[re-1]][i-1] + a[que[re-1]+1] * a[que[re-1]+1]);                int xx = a[que[re-1]+1] - a[que[re-2]+1];                int xz = a[j+1] - a[que[re-1]+1];                if(yy*xz >= yz*xx) re--;                else break;            }            que[re++] = j;        }    }}int main(){    int cas=1;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)            scanf("%d",&a[i]);        sort(a+1,a+1+n);        solve();        printf("Case %d: ",cas++);        printf("%d\n",dp[n][m]);    }    return 0;}



0 0
原创粉丝点击