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 ...
随机推荐
- datax 执行流程分析
https://www.jianshu.com/nb/29319571 https://www.jianshu.com/p/b10fbdee7e56
- Vue.js项目中,当图片无法显示时则显示默认图片
使用require将图片进入,写法如下: data: () => ({logo: 'this.src="' + require('../assets/img.png') + '&quo ...
- Java实现网上商城
// 第一个JavaWeb项目 //练手项目没有使用框架 github下载 https://github.com/dejavudwh/Online-Shopping 项目截图 1.基本实现了购物网站该 ...
- leetcode28_C++实现strStr()函数
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- 数据时代的的企业管理 记SAP商业同略会
[PConline 资讯]在2012 SAP中国商业同略会城市论坛深圳站上,自SAP中国的萧洁云总裁和张志琦先生,对SAP中国的战略.SAP的技术战略,以及SAP对于行业趋势分析与媒体进行了沟通,对数 ...
- 第二篇-bmob云端服务器的发现
最近认识了一个Bmob云端服务器,使用它提供的API可以轻松地完成与数据库(bmob)的交互,使开发更加专注于功能的实现. 这很方便对js的学习,完全可以利用前端三板斧来搭建一个网站,并且初步实现简单 ...
- 《剑指offer》---左旋转字符串与右旋转字符串
本文算法使用python3实现 1. 问题1 1.1 题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S, ...
- mysql的程序组成
MySQL的程序组成 1:客户端 mysql:客户端程序 mysqldump:mysql备份工具 mysqladmin:mysql管理工具 mysqlbinlog:二进制日志查询工具 2:服务端 my ...
- 软工实践团队展示——WorldElite
软工实践团队展示--WorldElite 本次作业链接 团队成员 031602636许舒玲(组长) 031602237吴杰婷 031602634吴志鸿 081600107傅滨 031602220雷博浩 ...
- .netMVC Vue axios 获取数据
网页 <link href="~/Content/css/bootstrap-theme.min.css" rel="stylesheet" /> ...