hdu 3480 Division 斜率优化
来源:互联网 发布:华为云智软件 编辑:程序博客网 时间:2024/05/16 00:54
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480
Division
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others)Total Submission(s): 3864 Accepted Submission(s): 1479
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.
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.
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: 18HintThe answer will fit into a 32-bit signed integer.
题意:把n个数字 分成m个集合。 每个集合的价值是 这个集合中 (max-min)^2。 输出最少的价值
做法:如果想价值最小,不妨先排个序。这样 最后的答案肯定在排序后的数组中 是连续的。
可以得到数组 dp[i][c]=min{dp[j][c-1]+(num[i]-num[j+1])^2
直接dp 复杂度 N*N*M 瞬间爆炸。
可以斜率优化来写;
推导出
y(j)=dp[j][c-1]+num[j+1]*num[j+1]
x(j)=num[j+1]
当 k<j<i
[y(j)-y(k)]/[x[k]-x[j]]<=2*num[i]
当不等式成立是,j的取值更优。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <malloc.h>#include <ctype.h>#include <math.h>#include <string>#include <iostream>#include <algorithm>using namespace std;#include <stack>#include <queue>#include <vector>#include <deque>#include <set>#include <map>#define inf 0x7f7f77f#define ll __int64int num[10100];int dp[10100][6010];int que[10100];int cc;int y(int j){ return dp[j][cc-1]+num[j+1]*num[j+1];}int main(){ int n,m; int t; scanf("%d",&t); int cas=1; while(t--) { scanf("%d%d",&n,&m);//分成m个 memset(dp,0,sizeof dp); dp[0][0]=0; dp[0][1]=0; for(int i=1;i<=n;i++) { //num[i]=i; scanf("%d",num+i); dp[i][0]=0; } sort(num+1,num+1+n); for(int i=1;i<=n;i++) dp[i][1]=(num[i]-num[1])*(num[i]-num[1]);// 分成0 个 显然不行 0 for(int c=2;c<=m;c++)// 分成c个 { int tou=0,wei=0; que[wei++]=c-1; cc=c; for(int i=c;i<=n;i++)//长度// i>=j { while(wei-tou>=2) { int a=que[tou]; int b=que[tou+1]; int y1=y(a); int y2=y(b); int x1=num[a+1]; int x2=num[b+1]; if((y2-y1)<=2*num[i]*(x2-x1)) tou++; else break; } int tem=que[tou]; dp[i][c]=dp[tem][c-1]+(num[i]-num[tem+1])*(num[i]-num[tem+1]); while(wei-tou>=2)//g(b,a)>g(c,b) { int a=que[wei-2]; int b=que[wei-1]; int c=i; int y1=y(a); int y2=y(b); int y3=y(c); int x1=num[a+1]; int x2=num[b+1]; int x3=num[c+1]; if((y2-y1)*(x3-x2)>=(x2-x1)*(y3-y2)) wei--; else break; } que[wei++]=i; } //puts(""); } printf("Case %d: %d\n",cas++,dp[n][m]); } return 0;}
0 0
- hdu 3480 Division 斜率优化
- HDU - 3480 Division(斜率优化)
- hdu 3480 Division(斜率优化DP)
- HDU 3480 Division 斜率优化DP
- hdu-3480-Division-dp-斜率优化
- hdu 3480 Division 斜率优化dp
- dp斜率优化 Hdu 3480 Division 题解
- Division - HDU 3480 斜率优化,四边形不等式优化
- hdu 3480 Division (斜率优化||四边形不等式)
- HDU-3480 Division 【DP+斜率优化(二维)】
- HDOJ 3480 Division(斜率优化DP)
- HDU 3480 Division (斜率DP)
- HDU 3480 (斜率优化)
- HDU - 3480 Division(四边形优化)
- HDU 3480 DP+斜率优化
- HDU 3480 斜率优化DP
- hdu3480 Division(斜率优化+二维DP)
- HDU 3480 Division【dp四边形不等式优化】
- sqlite3使用
- expdp\impdp及exp\imp
- 7.1Android服务绑定
- NOIP 2010 乌龟棋 DP (COGS 519)
- Kafka深度解析
- hdu 3480 Division 斜率优化
- 关于windows保护模式的一些个人见解
- VMware下Ubuntu与宿主Windows共享文件夹
- UML的作用、分类
- 图解 & 深入浅出 JavaWeb:Servlet 再说几句
- Oracle学习笔记20150903事务隔离级别
- System.ArgumentException: 输入数组长度大于此表中的列数。
- gcc -l参数和-L参数
- 程序设计基石与实践之旋转方阵