zoukankan      html  css  js  c++  java
  • HDU 2829 Lawrence(斜率优化DP)

    Lawrence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1248    Accepted Submission(s): 557


    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
     

    Recommend
    gaojie
     

    Statistic | Submit | Discuss | Note
    Hangzhou Dianzi University Online Judge 3.0
    Copyright © 2005-2012 HDU ACM Team. All Rights Reserved.
    Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
    Total 0.000597(s) query 1, Server time : 2012-08-27 11:18:43, Gzip enabled

    斜率DP

    设dp[i][j]表示前i点,炸掉j条边的最小值。j<i

    dp[i][j]=min{dp[k][j-1]+cost[k+1][i]}

    又由得出cost[1][i]=cost[1][k]+cost[k+1][i]+sum[k]*(sum[i]-sum[k])

    cost[k+1][i]=cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k])

    代入DP方程

    可以得出 y=dp[k][j-1]-cost[1][k]+sum[k]^2

    x=sum[k].

    斜率sum[i]

    可以用斜率优化,也可以用四边形不等式优化,四边形不等式我在前面已经写了。

    下面是斜率优化的代码

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MAXN=1010;
    int a[MAXN];
    int sum[MAXN];
    int cost[MAXN];//cost[1][i]
    int q[MAXN];
    int head,tail;
    int n,m;
    int dp[MAXN][MAXN];
    
    int DP()
    {
        for(int i=1;i<=n;i++)
        {
            dp[i][0]=cost[i];
            dp[i][i-1]=0;
        }
        for(int j=1;j<=m;j++)
        {
            head=tail=0;
            q[tail++]=j;
            for(int i=j+1;i<=n;i++)
            {
                while(head+1<tail)
                {
                    int p1=q[head];
                    int p2=q[head+1];
                    int x1=sum[p1];
                    int x2=sum[p2];
                    int y1=dp[p1][j-1]-cost[p1]+sum[p1]*sum[p1];
                    int y2=dp[p2][j-1]-cost[p2]+sum[p2]*sum[p2];
                    if((y2-y1)<=sum[i]*(x2-x1)) head++;
                    else break;
                }
                int k=q[head];
                dp[i][j]=dp[k][j-1]+cost[i]-cost[k]-sum[k]*sum[i]+sum[k]*sum[k];
                while(head+1<tail)
                {
                    int p1=q[tail-2];
                    int p2=q[tail-1];
                    int p3=i;
                    int x1=sum[p1];
                    int x2=sum[p2];
                    int x3=sum[p3];
                    int y1=dp[p1][j-1]-cost[p1]+sum[p1]*sum[p1];
                    int y2=dp[p2][j-1]-cost[p2]+sum[p2]*sum[p2];
                    int y3=dp[p3][j-1]-cost[p3]+sum[p3]*sum[p3];
                    if((y2-y1)*(x3-x2)>=(y3-y2)*(x2-x1))tail--;
                    else break;
                }
                q[tail++]=i;
            }
        }
        return dp[n][m];
    }
    
    int main()
    {
       // freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        while(scanf("%d%d",&n,&m)==2)
        {
            if(n==0&&m==0)break;
            sum[0]=0;
            cost[0]=0;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                sum[i]=sum[i-1]+a[i];
                cost[i]=cost[i-1]+sum[i-1]*a[i];
            }
            printf("%d\n",DP());
    
        }
        return 0;
    }
  • 相关阅读:
    16.ajax_case08
    16.ajax_case07
    16.ajax_case06
    16.ajax_case05
    9.2 sun.py
    9.1 mongo_python.py
    pandas-如何得到某一个值所在的行
    [已解决]ValueError: row index was 65536, not allowed by .xls format
    [已解决]python FileNotFoundError: [WinError 3] for getsize(filepath)
    Tomcat6连接数设置:permsize
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2658311.html
Copyright © 2011-2022 走看看