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. ASCII码中的可见字符

    ASCII码中 包括空格的可见字符从32到126共95个 不包括则为94

  2. 数据库MySql在python中的使用

    随着需要存储数据的结构不断复杂化,使用数据库来存储数据是一个必须面临的问题.那么应该如何在python中使用数据库?下面就在本篇博客中介绍一下在python中使用mysql. 首先,本博客已经假定阅读 ...

  3. Yii2 UploadedFile上传文件

    通过 UploadFile::getInstance($model, $attribute); UploadFile::getInstances($model, $attribute); Upload ...

  4. MySql 8 命令

    1-创建用户 create user 用户名@'%' identified by '密码'; create user 用户名@'localhost' identified by '密码';   2-授 ...

  5. DB2定位锁等待

    在应用中,我们经常会碰到sql执行很慢,但是数据库cpu和内存使用率又不高的情况,类似的问题基本上由于锁,排序等原因造成,本文主要描述如何去定位锁等待问题,谁在锁等待?等待谁持有的锁?锁在那个表? 一 ...

  6. 把a文件删除b文件中的相同的行

    grep -vxFf b.txt a.txt > newa.txt 更好的方法是 comm - - b.txt a.txt > newa.txt 来自Tool in unix to sub ...

  7. 微信小程序 跳坑

    http://www.wxapp-union.com/forum.php?mod=viewthread&tid=3270

  8. Halcon 笔记3 形态学

    Halcon 三大数据类型: (1)图像 (2)区域 (3)XLD  查看时间工具 如果想让图像减少,则进行腐蚀(或者使用开运算),反之,则进行膨胀(或闭运算) 腐蚀后再进行膨胀,相当于进行开运算.因 ...

  9. PHP简单模拟登录功能实例分享

    1.curl实现模拟登录的代码,(只是实现服务器与服务器建立会话,其实并没有在客户端与服务器之间建立会话) <?php $cookie_jar = tempnam('./tmp','cookie ...

  10. linux自启动、定时启动脚本

    linux开机自启动 想让一个程序.脚本开机自启动,可以在/etc/rc.d目录下面找到rc.local文件,编辑该文件,在尾部加上需要运行的命令即可. 如: #cd /etc/rc.d #sudo ...