HDU 3480 Division(斜率优化+二维DP)

                Division

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

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

2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU

【思路】

斜率优化+分配式DP。

设f[i][j]表示将前i个分作j个集合所得最小消费,则有转移方程式:

f[i][j]=min{ f[k][j-1]+(A[k]-A[j+1])^2 }

若有k>l,且决策k优于决策l则有:

f[k][j-1]-f[l][j-1]+sq(A[k+1])-sq(A[l+1]) <= 2*(A[k+1]-A[l+1])*A[i]

先进行j循环枚举f[][j],每一层维护一个单调队列即可。

乘除耗费时间悬殊,如果直接除这个题就超时了。

【代码】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 typedef double Do;
 7 const int N = 1e4+10;
 8 const int M = 5000+10;
 9
10 int f[N][M],A[N],q[N];
11 int n,m,L,R;
12 int sq(int x) { return x*x; }
13 int UP(int l,int k,int j) {
14     return f[k][j-1]-f[l][j-1]+sq(A[k+1])-sq(A[l+1]);
15 }
16 int DN(int l,int k,int j) {
17     return 2*(A[k+1]-A[l+1]);
18 }
19 void read(int& x) {
20     char c=getchar(); while(!isdigit(c)) c=getchar();
21     x=0; while(isdigit(c)) x=x*10+c-‘0‘ , c=getchar();
22 }
23 int main() {
24     int T,kase=0;
25     read(T);
26     while(T--) {
27         read(n),read(m);
28         for(int i=1;i<=n;i++) read(A[i]);
29         sort(A+1,A+n+1);
30         for(int i=1;i<=n;i++) f[i][1]=sq(A[i]-A[1]);    //初始化第一层
31         for(int j=2;j<=m;j++) {
32             L=R=0;
33             for(int i=1;i<=n;i++) {
34                 while(L<R && UP(q[L],q[L+1],j)<=A[i]*DN(q[L],q[L+1],j)) L++;
35                 int t=q[L];
36                 f[i][j]=f[t][j-1]+sq(A[i]-A[t+1]);
37                 while(L<R && UP(q[R-1],q[R],j)*DN(q[R],i,j)>=UP(q[R],i,j)*DN(q[R-1],q[R],j)) R--;
38                 q[++R]=i;
39             }
40         }
41         printf("Case %d: %d\n",++kase,f[n][m]);
42     }
43     return 0;
44 }
时间: 2024-12-05 17:30:35

HDU 3480 Division(斜率优化+二维DP)的相关文章

HDU 4901 The Romantic Hero(二维dp)

题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候按照给的先后数序取数,后面的里面的所有的元素的下标一定比前面的大.问你有多上种放元素的方法可以使得前面异或的值和后面与的值相等. dp[x][y] 表示走到第x步,得到y这个数字一共有多少种方法. 但是需要注意这里得分一下,不能直接用dp数组存种数,你需要分一下从上一层过来的次数,和这一层自己可以到达的次数.然后取和的时候前后两个集合的种数进行乘法,注意边乘边取余. 顺便给一组数据: 4 3

HDU 5074 Hatsune Miku(简单二维dp)

题目大意:给你一些音符之间的联系,给你一个串,让你求出这个串的最大值.-1的时候可以任意替代,其他情况必须为序列上的数. 解题思路:简单二维dp,分情况处理就可以了啊. Hatsune Miku Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 637    Accepted Submission(s): 458 Problem De

HDU 3480 - Division - [斜率DP]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others) Little D is really interested in the theorem of sets recently. There's a problem that confused him a long time.   

(hdu)5234 Happy birthday 二维dp+01背包

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234 Problem Description Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin says that she wants to eat many cakes. Thus, her mother takes her to a cake garden. The garden

hdu 3480 Division (斜率优化)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 2676    Accepted Submission(s): 1056 Problem Description Little D is really interested in the theorem of sets recently. There's a pro

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4990    Accepted Submission(s): 1509 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

二维dp

原题http://acm.hdu.edu.cn/showproblem.php?pid=3127 WHUgirls Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2050    Accepted Submission(s): 780 Problem Description There are many pretty girls i

hdu 2660 Accepted Necklace (二维背包)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2530    Accepted Submission(s): 999 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

二维dp(O(N^3)实现) zoj3230

1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #define maxn 125 5 using namespace std; 6 7 int cost[maxn][maxn],w[maxn][maxn]; 8 int dp[maxn][maxn]; 9 int N,M; 10 int main(){ 11 while(cin>>N>>M){ 12 if (N==0