【DP+四边不等式优化】Division

本文解析了HDU 3480分割问题,通过使用动态规划结合四边不等式优化的方法求解最小总成本。输入包含多个测试案例,输出为按最优方式分割集合后的最小总成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Division


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的四边不等式优化,看了一个晚上,终于对理论上的有了一点理解。

这题还是参考别人的代码改的。


#include<cstdio>
#include<algorithm>
using namespace std;
#define N 10010
#define M 5010
int val[N];
int dp[M][N];//dp[i][j]代表i堆,前j个数的最优解 
int s[M][N];//s[i,j]为dp[i,j]的决策量,即dp[i,j]=dp[i,s[i,j]]+(val[j]-val[s[i,j]+1])^2)
//dp[i][j]=min(dp[i-1][k]+(val[j]-val[k+1])^2)
int n,m;
int fun(int x)
{
    return x*x;
}
int main()
{
    int t,ti=1;
    scanf("%d",&t);
    for(;t--;)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
            scanf("%d",&val[i]);
        sort(val+1,val+1+ n);
        for(int i=1;i<=n;++i)
        {
            dp[1][i]=fun(val[i]-val[1]);//前i个数分为1堆的最优解 
            s[1][i]=0;//dp[1][i]的最优决策是dp[i][0]+(val[i]-val[1])^2 
            dp[i][i]=0;//前i个数分为i堆的最优解 
        }
        for(int i=2;i<=m;++i)
        {
            s[i][n+1]=n-1;
            for(int j=n;j>i;--j)
            {
                dp[i][j]=-1;
                for(int k=s[i-1][j];k<=s[i][j+1]&&k<j;++k)//四边不等式优化 
                {
                    int temp=dp[i-1][k]+fun(val[j]-val[k+1]);
                    if(dp[i][j]==-1||dp[i][j]>temp)//保留最优解 
                    {
                        dp[i][j]=temp;
                        s[i][j]=k;
                    }
                }
            }
        }
        printf("Case %d: %d\n",ti++,dp[m][n]);
    }
    return 0;
}


区间DP四边不等式优化是一种用于优化区间DP的方法。四边不等式可以用来简化具有特定转移方式的DP问题。具体来说,如果转移方程满足区间包含单调性和四边不等式,那么可以使用四边不等式优化来减少计算量。 要判断一个转移方程是否满足区间包含单调性和四边不等式,可以先进行表格计算来观察。如果观察到满足条件,就可以使用四边不等式优化。 引理表明,如果一个转移方程满足区间包含单调性和四边不等式,那么优化后的转移方程也会满足四边不等式。这意味着通过四边不等式优化可进一步减少计算复杂度。 因此,区间DP四边不等式优化是一种有效的优化方法,可以提高算法的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【DP四边不等式优化详解(一)](https://blog.youkuaiyun.com/qq_37656398/article/details/103537173)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [四边不等式优化区间DP技巧)](https://blog.youkuaiyun.com/AC__dream/article/details/123668489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值