zoukankan      html  css  js  c++  java
  • hdu2829 Lawrence

    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

    这题要先初始化cost[i][j],表示从i到j累加的总分,然后用dp[i][j]表示前i个数炸j次最后得到的最小值,要用四边形优化。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define ll long long
    ll dp[1005][1006],a[1006],cost[1006][1006];
    int s[1006][1006];
    ll min(ll a,ll b){
        return a<b?a:b;
    }
    
    
    int main()
    {
        int n,m,i,j,k;
        ll sum,maxn;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0 && m==0)break;
            for(i=1;i<=n;i++){
                scanf("%lld",&a[i]);
            }
            for(i=1;i<=n;i++){
                sum=a[i];
                cost[i][i]=0;
                for(j=i+1;j<=n;j++){
                    cost[i][j]=cost[i][j-1]+sum*a[j];
                    sum+=a[j];
                }
            }
            maxn=cost[1][n]+10;
            for(i=2;i<=n;i++){
                dp[i][1]=maxn;
                for(k=1;k<i;k++){
                    dp[i][1]=min(dp[i][1],cost[1][k]+cost[k+1][i]);
                }
                s[i][1]=2;
    
            }
            /*printf("--->%lld
    ",dp[n][1]);*/
    
            for(j=2;j<=m;j++){
                s[n+1][j]=n-1;
                for(i=n;i>j;i--){
                    dp[i][j]=maxn;
                    for(k=s[i][j-1];k<=s[i+1][j];k++){
                        if(dp[i][j]>dp[k][j-1]+cost[k+1][i]){
                            dp[i][j]=dp[k][j-1]+cost[k+1][i];
                            s[i][j]=k;
                        }
                    }
    
                    /*for(k=j;k<i;k++){
                        dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[k+1][i]);
                    }*/
    
                }
                /*printf("--->%d %lld
    ",j,dp[n][j]);*/
            }
            printf("%lld
    ",dp[n][m]);
        }
        return 0;
    }

    这题也可以用斜率优化做,dp[i][j]=min{dp[k][j-1]+cost[k+1][i]},cost[i][j]表示i到j的总花费,那么cost[1][i]=cost[1][k]+cost[k+1][i]+sum[k]*(sum[i]-sum[k]),那么dp[i][j]=dp[k][j-1]+cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k]),
    那么可以设y=dp[k][j-1]-cost[1][k]+sum[k]*sum[k],x=sum[k],斜率为sum[i].并设g(i,j)=(yj-yi)/(xj-xi).
    当g(k1,k2)<=sum[i]时,说明k2比k1优,可以删除k2,如果g(k1,k2)>=g(k2,i)时,可以删去k2,由这两个来维护上凸的图形。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define inf 999999999
    #define maxn 1006
    ll cost[maxn],dp[maxn][maxn],sum[maxn],a[maxn],q[1111111];
    int i,j,n,m;
    ll getup(int k){
        return dp[k][j-1]-cost[k]+sum[k]*sum[k];
    }
    ll getdown(int k){
        return sum[k];
    }
    
    
    int main()
    {
        int front,rear;
        ll k;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==0 && m==0)break;
            sum[0]=cost[0]=0;
            for(i=1;i<=n;i++){
                scanf("%lld",&a[i]);
                sum[i]=sum[i-1]+a[i];
                cost[i]=cost[i-1]+sum[i-1]*a[i];
            }
            for(i=1;i<=n;i++){
                dp[i][0]=cost[i];
            }
            for(j=1;j<=m;j++){
                front=rear=0;
                q[rear]=j;
                for(i=j+1;i<=n;i++){
                    while(front<rear &&  getup(q[front+1])-getup(q[front])<=sum[i]*( getdown(q[front+1])-getdown(q[front]) )      ){
                        front++;
                    }
                    k=q[front];
                    dp[i][j]=dp[k][j-1]+cost[i]-cost[k]-sum[k]*(sum[i]-sum[k]);
                    while(front<rear && ( getup(q[rear])-getup(q[rear-1]) )*(getdown(i)-getdown(q[rear]) )>=( getup(i)-getup(q[rear]) )*(getdown(q[rear])-getdown(q[rear-1]) )  ){
                        rear--;
                    }
                    rear++;
                    q[rear]=i;
                }
    
            }
            printf("%lld
    ",dp[n][m]);
        }
        return 0;
    }
    


  • 相关阅读:
    Java学习笔记12---向上转型-父类的对象引用指向子类对象
    Java学习笔记11---静态成员变量、静态代码块、成员变量及构造方法的初始化或调用顺序
    Java学习笔记10---访问权限修饰符如何控制成员变量、成员方法及类的访问范围
    Java学习笔记9---类静态成员变量的存储位置及JVM的内存划分
    Java学习笔记8---类的静态成员变量与静态成员方法的访问与调用方式
    Java学习笔记7---父类构造方法有无参数对子类的影响
    Java学习笔记6---字符串比较方法compareTo(String str)
    地址总线、数据总线、寻址能力、字长及cpu位数等概念之间的关系
    Alpha事后诸葛亮
    第05组 Alpha冲刺(4/4)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464677.html
Copyright © 2011-2022 走看看