热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU3480Division(斜率优化DP)

TimeLimit:100005000MS(JavaOthers)    MemoryLimit:999999400000K(JavaOthers)TotalSubmission(

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


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

HDU 3480  Division(斜率优化DP)


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
 

 

Recommend
zhengfeng
 
 
 
 

首先从小到大排序。

然后设 dp[i][j]表示前j个数分成i组的最小花费。

则 dp[i][j]=min{dp[i-1][k]+(a[j]-a[k+1])^2}  0

利用斜率优化DP,整理下就可以出来了。

 

#include
#include<string.h>
#include
#include
using namespace std;
const int MAXN=10010;
const int MAXM=5010;
int a[MAXN];
int dp[MAXM][MAXN];
int n,m;
int q[MAXN];
int head,tail;
int DP()
{
    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];
                int x2=a[p2+1];
                int y1=dp[i-1][p1]+x1*x1;
                int y2=dp[i-1][p2]+x2*x2;
                if((y2-y1)<=2*a[j]*(x2-x1))head++;
                else break;
            }
            int k=q[head];
            dp[i][j]=dp[i-1][k]+(a[j]-a[k+1])*(a[j]-a[k+1]);
            while(head+1<tail)
            {
                int p1=q[tail-2];
                int p2=q[tail-1];
                int p3=j;
                int x1=a[p1+1];
                int x2=a[p2+1];
                int 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][j]+x3*x3;
                if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2))tail--;
                else break;
            }
            q[tail++]=j;
        }
    }
    return dp[m][n];
}
int main()
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    int iCase=0;
    while(T--)
    {
        iCase++;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
           scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        printf("Case %d: %d\n",iCase,DP());
    }
    return 0;
}

 

 

 

 

另外还有四边形不等式优化,还没有非常理解,正在学习中

 

/*
HDU  3840
C++ 2884ms C++

*/

#include
#include
#include<string.h>
#include
using namespace std;
const int MAXN=10010;
const int MAXM=5010;

int a[MAXN];

int s[MAXN][MAXM];
int dp[MAXN][MAXM];
int main()
{
    int n,m;
    int T;
    scanf("%d",&T);
    int iCase=0;
    while(T--)
    {
        iCase++;
        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[i][1]=(a[i]-a[1])*(a[i]-a[1]);
            s[i][1]=1;
        }

        for(int k=2;k<=m;k++)
        {
            s[n+1][k]=n-1;
            for(int i=n;i>=k;i--)
            {
                dp[i][k]=dp[k-1][k-1]+(a[i]-a[k])*(a[i]-a[k]);
                s[i][k]=k;
                for(int j=s[i][k-1];j<=s[i+1][k];j++)
                {
                    int temp=dp[j][k-1]+(a[i]-a[j+1])*(a[i]-a[j+1]);
                    if(temp<dp[i][k])
                    {
                        dp[i][k]=temp;
                        s[i][k]=j;
                    }
                }
            }
        }
        printf("Case %d: %d\n",iCase,dp[n][m]);
    }
    return 0;
}

 


推荐阅读
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 题目描述Takuru是一名情报强者,所以他想利用他强大的情报搜集能力来当中间商赚差价。Takuru的计划是让Hinae帮他去市场上买一个商品,然后再以另一个价格卖掉它。Takur ... [详细]
  • DescriptionclickmeSolution套路的状压期望DP题。。。考虑倒退期望:设fi,jrolepresentationstyleposi ... [详细]
  • JZOJ 1266. 玉米田
    1266.玉米田(cowfood.pasccpp)(FileIO):input:cowfood.inoutput:cowfood.outTimeLimits:1000msMemor ... [详细]
  • 开发笔记:城市建设
    本文由编程笔记#小编为大家整理,主要介绍了城市建设相关的知识,希望对你有一定的参考价值。本文涉及:cdq分治、MST一道十分精妙的cdq分 ... [详细]
  • 题面传送门Solution看到什么最大值最小肯定二分啊。check直接跑一个二分图匹配就好了。orzztl!!!代码实现*mail:mle ... [详细]
author-avatar
管怡6440_368
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有