码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3480 斜率dp

时间:2017-07-29 23:10:48      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:type   linker   link   cost   and   use   --   pre   das   

Division

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


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
2
3 2
1 2 4
4 2
4 7 10 1
 

 

Sample Output
Case 1: 1
Case 2: 18
Hint
The answer will fit into a 32-bit signed integer.
 

 

Source
题意:给你一个容量为n的集合 现在选取 m个子集 并且要求m个子集的并集为原集合  每个集合的代价为集合内(MAX – MIN)^2 求最少的代价
题解:
 1 #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <cctype>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <bitset>
13 #include <string>
14 #include <complex>
15 #define ll __int64
16 #define mod 1000000007
17 using namespace std;
18 int t;
19 int n,m;
20 int a[10004];
21 int dp[5005][10004];
22 int q[10004],head,tail;
23 int main()
24 {
25     scanf("%d",&t);
26     for(int s=1; s<=t; s++)
27     {
28         scanf("%d %d",&n,&m);
29         for(int j=1; j<=n; j++)
30             scanf("%d",&a[j]);
31         sort(a+1,a+1+n);
32         for(int j=1; j<=n; j++)
33             dp[1][j]=(a[j]-a[1])*(a[j]-a[1]);
34         for(int i=2; i<=m; i++)
35         {
36             head=tail=0;
37             q[tail++]=i-1;
38             for(int j=i; j<=n; j++)
39             {
40                 while(head+1<tail)
41                 {
42                     int p1=q[head],p2=q[head+1];
43                     int x1=a[p1+1],x2=a[p2+1];
44                     int y1=dp[i-1][p1]+x1*x1,y2=dp[i-1][p2]+x2*x2;
45                     if(y2-y1<2*a[j]*(x2-x1))
46                         head++;
47                     else
48                         break;
49                 }
50                 int k=q[head];
51                 dp[i][j]=dp[i-1][k]+(a[j]-a[k+1])*(a[j]-a[k+1]);
52                 while(head+1<tail&&j!=n)
53                 {
54                     int p1=q[tail-2],p2=q[tail-1],p3=j;
55                     int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1];
56                     int y1=dp[i-1][p1]+x1*x1,y2=dp[i-1][p2]+x2*x2,y3=dp[i-1][p3]+x3*x3;
57                     if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))
58                         tail--;
59                     else
60                         break;
61                 }
62                 q[tail++]=j;
63             }
64         }
65         printf("Case %d: %d\n",s,dp[m][n]);
66     }
67     return 0;
68 }

 

HDU 3480 斜率dp

标签:type   linker   link   cost   and   use   --   pre   das   

原文地址:http://www.cnblogs.com/hsd-/p/7257643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!