HDU 3480 斜率dp
Division
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 5053 Accepted Submission(s): 1980
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.
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.
The answer will fit into a 32-bit signed integer.
#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的更多相关文章
- B - Lawrence HDU - 2829 斜率dp dp转移方程不好写
B - Lawrence HDU - 2829 这个题目我觉得很难,难在这个dp方程不会写. 看了网上的题解,看了很久才理解这个dp转移方程 dp[i][j] 表示前面1~j 位并且以 j 结尾分成了 ...
- hdu 3507 斜率dp
不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个 ...
- D - Pearls HDU - 1300 斜率dp+二分
D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...
- 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做法 ...
- HDU 3480 Division DP斜率优化
解题思路 第一步显然是将原数组排序嘛--然后分成一些不相交的子集,这样显然最小.重点是怎么分. 首先,我们写出一个最暴力的\(DP\): 我们令$F[ i ][ j ] $ 为到第\(i\)位,分成\ ...
- hdu 2993 斜率dp
思路:直接通过斜率优化进行求解. #include<iostream> #include<cstdio> #include<algorithm> #include& ...
- hdu 4258 斜率DP
思路:dp[i]=dp[j]+(num[i]-num[j+1])^2; #include<iostream> #include<cstring> #include<alg ...
- hdu 3480 Division(斜率优化DP)
题目链接:hdu 3480 Division 题意: 给你一个有n个数的集合S,现在让你选出m个子集合,使这m个子集合并起来为S,并且每个集合的(max-min)2 之和要最小. 题解: 运用贪心的思 ...
- HDU 3480 - Division - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
随机推荐
- Python 3 利用 Dlib 实现摄像头人脸检测特征点标定
0. 引言 利用 Python 开发,借助 Dlib 库捕获摄像头中的人脸,进行实时人脸 68 个特征点标定: 支持多张人脸: 有截图功能: 图 1 工程效果示例( gif ) 图 2 工程效果示例( ...
- 技本功丨请带上纸笔刷着看:解读MySQL执行计划的type列和extra列
本萌最近被一则新闻深受鼓舞,西工大硬核“女学神”白雨桐,获6所世界顶级大学博士录取 货真价值的才貌双全,别人家的孩子 高考失利与心仪的专业失之交臂,选择了软件工程这门自己完全不懂的专业.即便全部归零, ...
- #Ubuntu 18.04 安装tensorflow-gpu 1.9
参考 https://tensorflow.google.cn/install/install_linux http://nvidia.com/cuda http://developer.nvidia ...
- JavaScript 之 ajax
1. AJAX 的概念 AJAX,即 Asynchronous JavaScript and XML(异步的 JavaScript 和 XML) 同步:前面的代码不执行完毕,后面的代码无法执行 异步: ...
- 常用DOS指令备忘
1.删除整个目录,包括空目录 rd D:\管理\2012新同学练习\.svn /s/q /s 删除当前目录及子目录 /q 不询问直接删除 2.拷贝目录树 xcopy D:\管理\2012新同学练习 E ...
- python sys模块使用详情
python常用模块目录 sys模块提供了一系列有关Python运行环境的变量和函数.1.sys.argv可以用sys.argv获取当前正在执行的命令行参数的参数列表(list).变量解释sys.ar ...
- Tempter of the Bone HDU 1010(DFS+剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
- Segments CodeForces 909B (找规律)
Description You are given an integer N. Consider all possible segments (线段,划分)on the coordinate axis ...
- AOP:spring 的Annotation配置
1.文件目录: 2.实体类 package com.wangcf.po; public class User { private int id; private String name; privat ...
- AVAudioPlayer播放音乐
1:首先创建一个新的项目,继承自UIViewController 2:导入框架AVFoundation.framework 右键工程名,在Build Phases的Link Binary With L ...