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 求最少的代价
题解:
 #pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
#define ll __int64
#define mod 1000000007
using namespace std;
int t;
int n,m;
int a[];
int dp[][];
int q[],head,tail;
int main()
{
scanf("%d",&t);
for(int s=; s<=t; s++)
{
scanf("%d %d",&n,&m);
for(int j=; j<=n; j++)
scanf("%d",&a[j]);
sort(a+,a++n);
for(int j=; j<=n; j++)
dp[][j]=(a[j]-a[])*(a[j]-a[]);
for(int i=; i<=m; i++)
{
head=tail=;
q[tail++]=i-;
for(int j=i; j<=n; j++)
{
while(head+<tail)
{
int p1=q[head],p2=q[head+];
int x1=a[p1+],x2=a[p2+];
int y1=dp[i-][p1]+x1*x1,y2=dp[i-][p2]+x2*x2;
if(y2-y1<*a[j]*(x2-x1))
head++;
else
break;
}
int k=q[head];
dp[i][j]=dp[i-][k]+(a[j]-a[k+])*(a[j]-a[k+]);
while(head+<tail&&j!=n)
{
int p1=q[tail-],p2=q[tail-],p3=j;
int x1=a[p1+],x2=a[p2+],x3=a[p3+];
int y1=dp[i-][p1]+x1*x1,y2=dp[i-][p2]+x2*x2,y3=dp[i-][p3]+x3*x3;
if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))
tail--;
else
break;
}
q[tail++]=j;
}
}
printf("Case %d: %d\n",s,dp[m][n]);
}
return ;
}

HDU 3480 斜率dp的更多相关文章

  1. B - Lawrence HDU - 2829 斜率dp dp转移方程不好写

    B - Lawrence HDU - 2829 这个题目我觉得很难,难在这个dp方程不会写. 看了网上的题解,看了很久才理解这个dp转移方程 dp[i][j] 表示前面1~j 位并且以 j 结尾分成了 ...

  2. hdu 3507 斜率dp

    不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个 ...

  3. D - Pearls HDU - 1300 斜率dp+二分

    D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...

  4. hdu 2829 斜率DP

    思路:dp[i][x]=dp[j][x-1]+val[i]-val[j]-sum[j]*sum[i]+sum[j]*sum[j]; 其中val[i]表示1~~i是一段的权值. 然后就是普通斜率dp做法 ...

  5. HDU 3480 Division DP斜率优化

    解题思路 第一步显然是将原数组排序嘛--然后分成一些不相交的子集,这样显然最小.重点是怎么分. 首先,我们写出一个最暴力的\(DP\): 我们令$F[ i ][ j ] $ 为到第\(i\)位,分成\ ...

  6. hdu 2993 斜率dp

    思路:直接通过斜率优化进行求解. #include<iostream> #include<cstdio> #include<algorithm> #include& ...

  7. hdu 4258 斜率DP

    思路:dp[i]=dp[j]+(num[i]-num[j+1])^2; #include<iostream> #include<cstring> #include<alg ...

  8. hdu 3480 Division(斜率优化DP)

    题目链接:hdu 3480 Division 题意: 给你一个有n个数的集合S,现在让你选出m个子集合,使这m个子集合并起来为S,并且每个集合的(max-min)2 之和要最小. 题解: 运用贪心的思 ...

  9. HDU 3480 - Division - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

随机推荐

  1. spring JDBC 事务管理

    spring JDBC 事务管理 一.Spring 中的JDBC Spring中封装了JDBC的ORM框架,可以用它来操作数据,不需要再使用外部的OEM框架(MyBatis),一些小的项目用它. 步骤 ...

  2. Digital Roots:高精度

    C - Digital Roots Description The digital root of a positive integer is found by summing the digits ...

  3. kafka浅谈

    关键词 producer       生产者 broker          缓存代理 consumer     消费者 partition       分区 topic            主题 ...

  4. US Customs bond DDP 船运

    客户提供目的港门点地址,提供美国进口产品的关税税率基本上就可以了关于ISF信息到时候你发给老外让老外填填好就可以了BAND 货值*0.575%POA  货值*0.335%这二个费用如果国内付就付了,国 ...

  5. 如何更改Arcmap里经纬度小数点后面的位数?

    customize>arcmap option>data view >round coordinate to 改成想要显示的小数位数

  6. [linux] vim在源代码中自动添加作者信息(转载)

    原文出处: http://www.vimer.cn/2009/10/用vim在源代码中添加你的个人信息.html vim ~/.vimrc "进行版权声明的设置 "添加或更新头 m ...

  7. 1019psp

    1.本周psp: 2.本周进度条: 3.累计进度图(折线图): 4.psp饼状图:

  8. centos7环境下mysql安装

    1.去官网下载合适的yum源安装包 https://dev.mysql.com/downloads/repo/yum/ 2.yum 本地安装 命令:yum localinstall mysql57-c ...

  9. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  10. 程序员必看电影:Java 4-ever

    http://blog.csdn.net/zdwzzu2006/article/details/5863068