斜率优化DP。

。。。

对数组排序后。dp【i】【j】表示对前j个物品分i段的最少代价,dp【i】【j】= min{ dp【i-1】【k】+(a【k+1】-a【j】)^2 }复杂度m*n^2      斜率优化一下就能够了。

Division

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 3008    Accepted Submission(s): 1173

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
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int maxn=11000;int n,m;
int dp[maxn/2][maxn],a[maxn];
int q[maxn],head,tail;int main()
{int T_T,cas=1;scanf("%d",&T_T);while(T_T--){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d",a+i);sort(a+1,a+n+1);for(int i=1;i<=n;i++)dp[1][i]=(a[i]-a[1])*(a[i]-a[1]);for(int i=2;i<=m;i++){head=tail=0;q[tail++]=i-1;for(int j=i;j<=n;j++){while(head+1<tail){int p1=q[head];int p2=q[head+1];int x1=a[p1+1],x2=a[p2+1];int y1=dp[i-1][p1]+x1*x1;int y2=dp[i-1][p2]+x2*x2;if((y2-y1)<=(x2-x1)*2*a[j]) head++;else break;}int k=q[head];dp[i][j]=dp[i-1][k]+(a[k+1]-a[j])*(a[k+1]-a[j]);while(head+1<tail){int p1=q[tail-2],p2=q[tail-1],p3=j;int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1];int y1=dp[i-1][p1]+x1*x1;int y2=dp[i-1][p2]+x2*x2;int y3=dp[i-1][p3]+x3*x3;if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2)) tail--;else break;}q[tail++]=j;}}printf("Case %d: %d\n",cas++,dp[m][n]);}return 0;
}

转载于:https://www.cnblogs.com/mengfanrong/p/5066344.html

HDOJ 3480 Division相关推荐

  1. java 判断 中文字符_java中判断字符串中是否有中文字符

    package com.meritit.test; public class TestChart { public static void main(String[] args) throws Exc ...

  2. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  3. 无法打开此程序因为计算机丢失,解决X-Scan安装后“无法启动此程序,因为计算机丢失NPPTools.dll”...

    配置CENTOS YUM更新源 众所周知,Centos 有个很方便的软件安装工具  yum,但是默认安装完centos,系统里使用的是国外的centos更新源,这就造成了我们使用默认更新源安装或者更新 ...

  4. 动态规划总结与题目分类

    源博客链接:http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少 ...

  5. dp进阶之FFT加速+数据结构优化+不等式优化

    快速傅里叶变换 快速傅里叶变换(英语:Fast Fourier Transform, FFT),是快速计算序列的离散傅里叶变换(DFT)或其逆变换的方法.傅里叶分析将信号从原始域(通常是时间或空间)转 ...

  6. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  7. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  8. 《动态规划》— 动态规划分类

    动态规划(英语:Dynamic programming,DP)是一种在数学.计算机科学和经济学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法. 动态规划常常适用于有重叠子问题和最 ...

  9. 转:动态规划题目分类

    https://blog.csdn.net/cc_again/article/details/25866971 一.简单基础dp 这类dp主要是一些状态比较容易表示,转移方程比较好想,问题比较基本常见 ...

最新文章

  1. 不占用多余空间实现值的交换——异或运算
  2. linux内核中等待队列
  3. php excel文件导出之二 图像导出
  4. 量子计算机新科技未来,能够“预测多个未来”的量子计算机诞生
  5. java lock可重入_Java源码解析之可重入锁ReentrantLock
  6. CSDN中markdown字体颜色,大小,首行缩进,居中排布
  7. StringBuilder 和 String拼接10万个字符串的速度测试差别太大了
  8. 程序员第一定律:关于技能与收入
  9. 如何在CentOS 7上安装Percona XtraDB集群
  10. 聚焦国内名企开源!OSCAR 开源先锋日(1020)全部议程首次曝光
  11. linux中c语言生日快乐_生日快乐,Linux:27岁
  12. JScript中的条件注释详解(转载自网络)
  13. mysql之查询某段时间范围的数据
  14. Q79:怎么用三角形网格(Triangle Mesh)细分曲面
  15. 旋转音乐html,css3可控旋转音乐播放按钮
  16. iOS 颜色选择器 仿ps 调色板
  17. arm实验使用keil自带的MDK仿真教程
  18. Tessellation细分曲面技术(DX11)
  19. 电脑软件快捷方式不显示图标
  20. BZOJ_3772_精神污染_主席树

热门文章

  1. php 数据导出到excel文件,PHP - 如何将数据动态导出到Excel文件(.xls)?
  2. 6种java垃圾回收算法_学习java垃圾回收
  3. 计算机学office有必要吗,计算机二级office要学多久
  4. 读写分离oracle redis,redis集群主从之读写分离
  5. 【特别版】计算机哲学对学习生活借鉴的几个例子
  6. 【NOIP2015】【Luogu2678】跳石头
  7. android电子书控件,Android控件大全.pdf
  8. python requests示例_Python3中requests库学习01(常见请求示例)
  9. 编程让鼠标一直动_相见恨晚的效率提升工具,低价捡漏可以自定义编程快捷键的鼠标...
  10. router优点 vue_Vue 出场率99%的面试题