HDU3480 Division

题目大意

将N个数分成M部分,使每部分的最大值与最小值平方差的和最小。

思路

首先肯定要将数列排序,每部分一定是取连续的一段,于是就有了方程

$\Large f(i,j)=min(f(i-1,k-1)+(a_j-a_k)^2)$

其中$f(i,j)$表示前$j$个数分成$i$部分的最小值

解法一.四边形不等式优化

设$w(i,j)=(a_j-a_i)^2$

方程变为$f(i,j)=min(f(i-1,k-1)+w(k,j))$

很容易想到四边形不等式优化

证明w满足四边形不等式

$w(i,j)-w(i+1,j)=(a_j-a_i)^2-(a_j-a_{i+1})^2=a_i^2-a_{i+1}^2+2*a_j*(a_{i+1}-a_i)$

因为$a_{i+1}-a_i\ge 0$

所以$w(i,j)-w(i+1,j)$关于j单调不减,即$w(i,j)-w(i+1,j)\le w(i,j+1)-w(i+1,j+1)$

所以$w(i,j)+w(i+1,j+1)\le w(i,j+1)+w(i+1,j)$

以下证明具体可参考POJ1160 Post Office

证明f满足四边形不等式

设$f_k(i,j)=f(i-1,k-1)+w(k,j)$

对于$\forall i\le i^{‘}\le j\le j^{‘}$,设$k=s(i,j^{‘}),t=s(i^{‘},j)$

1.如果$k\le t$

有$f(i,j)+f(i^{‘},j^{‘})\le f(i-1,k-1)+w(k,j)+f(i^{‘}-1,t-1)+w(t,j^{‘})$

$f(i,j)+f(i^{‘},j^{‘})\le f(i-1,k-1)+w(k,j^{‘})+f(i^{‘}-1,t-1)+w(t,j)$

即$f(i,j)+f(i^{‘},j^{‘})\le f(i,j^{‘})+f(i^{‘},j)$

2.如果$k\gt t$

则只需证$f(i-1,t-1)+f(i^{‘}-1,k-1)\le f(i-1,k-1)+f(i^{‘}-1,t-1)$即可

设$k_1=s(i-1,k-1),k_2=s(i-2,k_1-1)……k_n=s(i-n,k_{n-1}-1)$

$t_1=s(i^{‘}-1,t-1),t_2=s(i^{‘}-2,t_1-1)……t_n=s(i^{‘}-n,t_{n-1}-1)$

如果$k_1\le t_1$,就用1去证明

否则,递归2证明直到求证$f(1,t_n-1)+f(i_{‘}-i+1,k_n-1)\le f(1,k_n-1)+f(i_{‘}-i+1,t_n-1)$

化简得$w(1,t_n-1)+w(t_{n+1},k_n-1)\le w(1,k_n-1)+w(t_{n+1},t_n-1)$

因为w满足四边形不等式所以$f(i,j)+f(i^{‘},j^{‘})\le f(i,j^{‘})+f(i^{‘},j)$

证明$f(i,j)$的决策$s(i,j)$是单调的

1.设$k=s(i,j)$,对于所有$t\le k$

有$w(t,j)+w(k,j+1)\le w(t,j+1)+w(k,j)$

两边同时加上$f(i,t-1)+f(i,k-1)$得$f_t(i,j)+f_k(i,j+1)\le f_k(i,j)+f_t(i,j+1)$

因为$f_t(i,j)\ge f_k(i,j)$,所以$f_k(i,j+1)\le f_t(i,j+1)$

所以$s(i,j)\le s(i,j+1)$

2.设$k=s(i,j)$,对于所有$t\le k$

有$f(i,t-1)+f(i+1,k-1)\le f(i+1,t-1)+f(i,k-1)$

两边同时加上$w(t,j)+w(k,j)$得$f_t(i,j)+f_k(i+1,j\le f_k(i,j)+f_t(i+1,j)$

因为$f_t(i,j)\ge f_k(i,j)$,所以$f_k(i+1,j)\le f_t(i+1,j)$

所以$s(i,j)\le s(i+1,j)$

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 10005
#define maxm 5005
int f[maxm][maxn],s[maxm][maxn],a[maxn];
void work(){
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    sort(a+1,a+n+1);
    memset(f,0x3f,sizeof(f));
    for(int i=1;i<=n;i++)s[0][i]=1;
    f[0][0]=0;
    for(int i=1;i<=m;i++){
        f[i][i]=0;s[i][i]=i;s[i][n+1]=n;
        for(int j=n;j>i;j--){
            for(int k=s[i-1][j];k<=s[i][j+1];k++){
                if(f[i][j]>f[i-1][k-1]+(a[j]-a[k])*(a[j]-a[k])){
                    f[i][j]=f[i-1][k-1]+(a[j]-a[k])*(a[j]-a[k]);
                    s[i][j]=k;
                }
            }
        }
    }
    printf("%d\n",f[m][n]);
}
int main(){
    int t;scanf("%d",&t);
    for(int i=1;i<=t;i++)printf("Case %d: ",i),work();
    return 0;
} 

原文地址:https://www.cnblogs.com/bennettz/p/9065572.html

时间: 2024-11-05 14:26:03

HDU3480 Division的相关文章

HDU-3480 Division (四边形不等式优化DP)

题目大意:将n个数分成m组,将每组的最大值与最小值的平方差加起来,求最小和. 题目分析:先对数排序.定义状态dp(i,j)表示前 j 个数分成 i 组得到的最小和,则状态转移方程为dp(i,j)=min(dp(i,k-1)+w(k,j)),其中w(i,j)=(a[i]-s[j])*(a[i]-a[j]).很显然,dp(i,j)满足凸四边形不等式. 代码如下: # include<iostream> # include<cstdio> # include<cstring>

hdu3480之二维斜率优化DP

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 pro

light oj 1214 - Large Division

1214 - Large Division   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if

fzuoj1607Greedy division

题目链接: 点我点我 题目:  Problem 1607 Greedy division Accept: 436    Submit: 1590 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunate

HDU 3480 Division(斜率优化+二维DP)

Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Total Submission(s): 3984    Accepted Submission(s): 1527 Problem Description Little D is really interested in the theorem of sets recently. There’s a pro

uva725 Division

题目描述: abcde / fghij =N a,b···j 为0-9任意一个数,且互相不同 任意给一个n(2<=n<=79),输出满足条件的可能 思路1:10!只有不到400w,直接暴力枚举即可 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[]= {1,0,2,3,4,5,6,7,8

BZOJ 1385: [Baltic2000]Division expression

题目 1385: [Baltic2000]Division expression Time Limit: 5 Sec  Memory Limit: 64 MB Description 除法表达式有如下的形式: X1/X2/X3.../Xk 其中Xi是正整数且Xi<=1000000000(1<=i<=k,K<=10000) 除法表达式应当按照从左到右的顺序求,例如表达式1/2/1/2的值为1/4.但可以在表达式中国入括号来改变计算顺序,例如(1/2)/(1/2)的值为1.现给出一个除

bzoj1385: [Baltic2000]Division expression

欧几里得算法.可以发现规律,a[2]作为分母,其他作为分子,必定是最好的选择.判断是否为整数即可. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) int read(){ int x=0;char c=getchar(); whil

Codeforces-808D Array Division 【multiset】

Description Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements