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.
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: 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
- Division (斜率dp)
- HDU 3480 Division (斜率DP)
- hdu3480 Division(斜率优化+二维DP)
- HDOJ 3480 Division(斜率优化DP)
- HDU-3480 Division 【DP+斜率优化(二维)】
- hdu 3480 Division(斜率优化DP)
- HDU 3480 Division 斜率优化DP
- hdu-3480-Division-dp-斜率优化
- hdu 3480 Division 斜率优化dp
- dp斜率优化 Hdu 3480 Division 题解
- DP(斜率优化)
- Lawrence (斜率dp)
- (初识)斜率dp
- Contestants Division (树形dp)
- hdu 3480 Division (斜率优化||四边形不等式)
- hdu 3480 Division(DP-DP优化)
- hdu 2829 Lawrence, hdu 3045 Picnic Cows,hdu 3480 Division , 斜率dp
- bzoj3156(斜率优化DP)
- 中科院软件所、计算所保研复试回忆
- 数据库设计三大范式
- window下caffe图像数据转换
- 5-特权级
- log4j配置示例&Spring集成log4j
- Division (斜率dp)
- 面试题:两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
- 2016四川省赛 Floyd-Warshall
- [LeetCode]--67. Add Binary
- Linux 时钟模块分析2
- |洛谷|动态规划|P1156 垃圾陷阱
- framebuffer简介与应用
- Cache-Control no-cache与max-age=0的区别
- 机器学习笔记(十五)——HMM序列问题和维特比算法