Division

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)

Total Submission(s): 2664    Accepted Submission(s): 1050

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

/*分析:
首先对于斜率dp我有个总结:
斜率dp一般应用于连续的一段或几段求最值
既1~k,k+1~j,j+1~...这样分段而不能跳开来求
仅仅有连续段才干用单调队列维护最值然后
dp[i]=dp[j]+(j+1~i)的值。 对于本题:
题目要求m个子数组的最值。而子数组中的元素不一定是原数组连续的
所以肯定不能直接用斜率优化,经过分析能够发现先进行从小到大排序
然后连续的m段最值就是能够求最值了。 所以:先对原数组进行从小到大排序
dp[i][j]表示以i结尾的j段的最值
从k+1~i作为一段
则:dp[i][j]=dp[k][j-1]+(s[i]-s[k+1])^2
如今就是怎样求到这个k使得dp[i][j]最小
如果k2<=k1<i
若:dp[k1][j-1]+(s[i]-s[k1+1])^2 <= dp[k2][j-1]+(s[i]-s[k2+1])^2
=>dp[k1][j-1]+s[k1+1]^2 - (dp[k2][j-1]+s[k2+1]^2) / (2s[k1+1]-2s[k2+1]) <= s[i]
所以:
y1 = dp[k1][j-1]+s[k1+1]^2
x1 = 2s[k1+1]
y2 = dp[k2][j-1]+s[k2+1]^2
x2 = 2s[k2+1] =>(y1 - y2)/(x1 - x2) <= i
单调队列维护下凸折线
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#include <limits.h>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX = 10000+10;
int n,m,index;
int q[MAX];
int s[MAX],dp[2][MAX];//採用滚动数组 int GetY(int k1,int k2){
return dp[index^1][k1]+s[k1+1]*s[k1+1] - (dp[index^1][k2]+s[k2+1]*s[k2+1]);
} int GetX(int k1,int k2){
return 2*(s[k1+1]-s[k2+1]);
} int DP(){
int head=0,tail=1;
index=0;
for(int i=1;i<=n;++i)dp[index][i]=INF;//初始化
//dp[index][0]=0;
for(int i=1;i<=m;++i){
index=index^1;
head=tail=0;
q[tail++]=0;
for(int j=1;j<=n;++j){
//dp[index^1][0]=(i-1)*(s[j]-s[1])*(s[j]-s[1]);
while(head+1<tail && GetY(q[head+1],q[head]) <= GetX(q[head+1],q[head])*s[j])++head;
while(head+1<tail && GetY(j,q[tail-1])*GetX(q[tail-1],q[tail-2]) <= GetY(q[tail-1],q[tail-2])*GetX(j,q[tail-1]))--tail;
q[tail++]=j;
int k=q[head];
dp[index][j]=dp[index^1][k]+(s[j]-s[k+1])*(s[j]-s[k+1]);
}
}
return dp[index][n];
} int main(){
int t,num=0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)scanf("%d",s+i);
sort(s+1,s+1+n);
printf("Case %d: %d\n",++num,DP());
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

hdu3480二维斜率优化DP的更多相关文章

  1. 2019.03.28 bzoj3594: [Scoi2014]方伯伯的玉米田(二维bit优化dp)

    传送门 题意咕咕咕 思路:直接上二维bitbitbit优化dpdpdp即可. 代码: #include<bits/stdc++.h> #define N 10005 #define K 5 ...

  2. 斜率优化DP学习笔记

    先摆上学习的文章: orzzz:斜率优化dp学习 Accept:斜率优化DP 感谢dalao们的讲解,还是十分清晰的 斜率优化$DP$的本质是,通过转移的一些性质,避免枚举地得到最优转移 经典题:HD ...

  3. BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1010 思路 [斜率优化DP] 我们知道,有些DP方程可以转化成DP[i]=f[j]+x[i ...

  4. [CEOI2004]锯木厂选址 斜率优化DP

    斜率优化DP 先考虑朴素DP方程, f[i][k]代表第k个厂建在i棵树那里的最小代价,最后答案为f[n+1][3]; f[i][k]=min(f[j][k-1] + 把j+1~i的树都运到i的代价) ...

  5. bzoj1010: [HNOI2008]玩具装箱toy(斜率优化DP)

    Orz CYC帮我纠正了个错误.斜率优化并不需要决策单调性,只需要斜率式右边的式子单调就可以了 codevs也有这题,伪·双倍经验233 首先朴素DP方程很容易看出:f[i]=min(f[j]+(i- ...

  6. 【转】斜率优化DP和四边形不等式优化DP整理

    (自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...

  7. HDU2829 Lawrence —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-2829 Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  8. BZOJ 3675 APIO2014 序列切割 斜率优化DP

    题意:链接 方法:斜率优化DP 解析:这题BZ的数据我也是跪了,特意去网上找到当年的数据后面二十个最大的点都过了.就是过不了BZ. 看到这道题自己第一发DP是这么推得: 设f[i][j]是第j次分第i ...

  9. 【学习笔记】动态规划—斜率优化DP(超详细)

    [学习笔记]动态规划-斜率优化DP(超详细) [前言] 第一次写这么长的文章. 写完后感觉对斜优的理解又加深了一些. 斜优通常与决策单调性同时出现.可以说决策单调性是斜率优化的前提. 斜率优化 \(D ...

随机推荐

  1. oracle nologging用法(转)

    一.oracle日志模式分为(logging,force logging,nologging) 默认情况是logging,就是会记录到redo日志中,force logging是强制记录日志,nolo ...

  2. RMQ之ST算法

    #include <stdio.h> #include <string.h> ; int a[N]; ]; inline int min(const int &a, c ...

  3. Golang在Linux环境下的POSIX风格socket编程

    这里给出一个服务端和client,服务端能够接受多个连接,而且利用Go的杀手特性go和channel来替代select进行数据的接收. 服务端: package main import ( " ...

  4. UVA 10831 - Gerg&#39;s Cake(数论)

    UVA 10831 - Gerg's Cake 题目链接 题意:说白了就是给定a, p.问有没有存在x^2 % p = a的解 思路:求出勒让德标记.推断假设大于等于0,就是有解,小于0无解 代码: ...

  5. android 读,写图片sd网卡资源

    <pre name="code" class="html"><!--<span style="font-family: Ari ...

  6. 依法使用Linux,反对Linux国产化

    10月28日.中国操作系统应用推广大会在京举行,包含中央办公厅.公安部.工信部在内的多个中央国家机关部委出席该会.此次推广大会的召开或标志着操作系统国产化进程的进一步推进. 会上.中国project院 ...

  7. 【从翻译mos文章】Weblogic AdminServer 启动fail,报错&quot;unable to get file lock, will retry&quot;

    Weblogic AdminServer 启动fail,报错"unable to get file lock, will retry" 参考原始: Weblogic AdminSe ...

  8. Codeforces Helpful Maths

    Xenia the beginner mathematician is a third year student at elementary school. She is now learning t ...

  9. net搭建热插拔式web框架

    net搭建热插拔式web框架(重造Controller) 由于.net MVC 的controller 依赖于HttpContext,而我们在上一篇中的沙箱模式已经把一次http请求转换为反射调用,并 ...

  10. docker-gitlab(转)

    Issues Docker is a relatively new project and is active being developed and tested by a thriving com ...