您的位置:首页 > 其它

hdu 2829 Lawrence(二维DP+斜率优化)

2012-09-17 19:50 260 查看


Lawrence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1299 Accepted Submission(s): 580



Problem Description

T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in the Arabian theater and led a group of Arab nationals in guerilla strikes against the Ottoman Empire. His primary targets were the railroads. A highly fictionalized
version of his exploits was presented in the blockbuster movie, "Lawrence of Arabia".

You are to write a program to help Lawrence figure out how to best use his limited resources. You have some information from British Intelligence. First, the rail line is completely linear---there are no branches, no spurs. Next, British Intelligence has assigned
a Strategic Importance to each depot---an integer from 1 to 100. A depot is of no use on its own, it only has value if it is connected to other depots. The Strategic Value of the entire railroad is calculated by adding up the products of the Strategic Values
for every pair of depots that are connected, directly or indirectly, by the rail line. Consider this railroad:



Its Strategic Value is 4*5 + 4*1 + 4*2 + 5*1 + 5*2 + 1*2 = 49.

Now, suppose that Lawrence only has enough resources for one attack. He cannot attack the depots themselves---they are too well defended. He must attack the rail line between depots, in the middle of the desert. Consider what would happen if Lawrence attacked
this rail line right in the middle:



The Strategic Value of the remaining railroad is 4*5 + 1*2 = 22. But, suppose Lawrence attacks between the 4 and 5 depots:



The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17. This is Lawrence's best option.

Given a description of a railroad and the number of attacks that Lawrence can perform, figure out the smallest Strategic Value that he can achieve for that railroad.



Input

There will be several data sets. Each data set will begin with a line with two integers, n and m. n is the number of depots on the railroad (1≤n≤1000), and m is the number of attacks Lawrence has resources for (0≤m<n). On the next line will be n integers, each
from 1 to 100, indicating the Strategic Value of each depot in order. End of input will be marked by a line with n=0 and m=0, which should not be processed.



Output

For each data set, output a single integer, indicating the smallest Strategic Value for the railroad that Lawrence can achieve with his attacks. Output each integer in its own line.



Sample Input

4 1
4 5 1 2
4 2
4 5 1 2
0 0




Sample Output

17
2




Source

2009 Multi-University Training Contest 2 - Host
by TJU



Recommend

gaojie



题目:http://acm.hdu.edu.cn/showproblem.php?pid=2829

题意:给你n个数,要你将这n个数分割成m+1块,假设 i 到 j 分成一块,那么这块的值为 a[ i ]*( a[ i+1 ] + a[ i+2 ] + ... + a[ j ])+ a[ i+1 ]*( a[ i+2 ]+ ... +a[ j ])+. ....+ a[ j-1 ] * a[ j ],求一种分割方案使得每块的值的总和最小

分析:这题可以想到一个简单的状态转移方程:f[ i ] [ k ]=min{ f[ j-1 ][ k-1 ] + w [ j , i ] } (1<=j<=i , 1 <=k <=m+1)

w[ l, r ]=a[ l ]*( a[ l+1 ] + a[ l+2 ] + ... + a[ r ])+ a[ l+1 ]*( a[ l+2 ]+ ... +a[ r ])+. ....+ a[ r-1 ] * a[ r ]

这个一开始想不出怎么转换了,数列学得不好= =,后来努力的YY了下,还是推出来了 ^_^

首先,设 s[ i ]=sum{ a[1] + a[2 ]+...+ a[i ]}

那么有:

w[ l , r ]= a[ l ]*( s[ r ] - s[ l ]) + a[ l+1 ]*( s[ r ]- s[ l+1]) + ... +a[ r-1 ]*( s[ r ] - s[ r-1 ])

= (a[ l ]+ a[ l+1 ]+...+ a[ r-1 ])*s[ r ] - (a[ l ]* s[ l ] + a[ l+1 ]* s[ l+1 ]+ .... + a[ r-1 ]* s[ r-1 ])

局势一下子明朗了,设 ss[ i ]=sum{ a[ 1 ]* s[ 1 ] + a[ 2 ]* s[ 2 ]+ .... + a[ i ]* s[ i ] }

则:w[ l , r ]=( s[ r-1 ]-s[ l-1 ] )*s[ r ] - ( ss[ r-1 ] - ss[ l-1 ] ),代回转移方程:

f[ i ] [ k ]=min{ f[ j-1 ][ k-1 ] + s[ j-1 ] -s[ i ] * s[ j-1 ] } +s[ i-1 ]*s[ i ] - ss[ i-1 ];

设a=s[ i ] x=s[ j-1 ] y=f[ j-1 ][ k-1 ] + s[ j-1 ]

原问题转换成求G=-ax+y,即y=ax+G

又是斜率优化问题。。。。

PS:只要有耐心,什么问题都能迎刃而解,就像这题,一开始了无头绪,动手之后,就越见明朗,所有放弃是不可取的= =

代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int mm=1111;
int f[mm][mm],a[mm],s[mm],ss[mm],q[mm];
int i,j,k,n,m,l,r;
bool TRight(int ax,int ay,int bx,int by,int cx,int cy)
{
    return (ax-bx)*(cy-by)>=(ay-by)*(cx-bx);
}
int gx(int i)
{
    return s[i-1];
}
int gy(int i,int j)
{
    return f[i-1][j-1]+ss[i-1];
}
int get(int i,int j,int a)
{
    return gy(i,j)-a*gx(i);
}
int main()
{
    while(scanf("%d%d",&n,&m),n+m)
    {
        a[0]=s[0]=ss[0]=0;
        for(i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            s[i]=s[i-1]+a[i];
            ss[i]=ss[i-1]+s[i]*a[i];
        }
        ++m;
        for(i=0;i<=n;++i)
            for(j=0;j<=m;++j)
                f[i][j]=2e9;
        f[0][0]=0;
        for(j=1;j<=m;++j)
            for(l=0,r=-1,i=1;i<=n;++i)
            {
                while(l<r&&TRight(gx(q[r-1]),gy(q[r-1],j),gx(q[r]),gy(q[r],j),gx(i),gy(i,j)))--r;
                q[++r]=i;
                while(l<r&&get(q[l],j,s[i])>=get(q[l+1],j,s[i]))++l;
                f[i][j]=get(q[l],j,s[i])+s[i-1]*s[i]-ss[i-1];
            }
        printf("%d\n",f
[m]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: