zoukankan      html  css  js  c++  java
  • hdu 2829 Lawrence(四边形不等式优化dp)

    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
     
    题意:n(1<=n<=1000)个数,将其分成m + 1 (0 <= m < n)组,要求每组数必须是连续的而且要求得到的价值最小。
    一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0
    思路:可以把题目理解为整数划分类型的题目,关键是打表发现可以用四边形不等式优化
    dp[i][j] 前i个数 分成j组  dp[i][j]=min(dp[k][j-1]+(d[i]-(sum[i]-sum[k])*sum[k]-d[k]); d[]表示前缀的任意两点的权值和   sum[]为前缀和
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    #define ll long long int
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    int a[1007];
    int sum[1007];
    int d[1007];
    int dp[1007][1007]; //前i个点分成j组
    int s[1007][1007];
    int main(){
        ios::sync_with_stdio(false);
        int n,m;
        while(cin>>n>>m){
            if(!n&&!m) break;
            memset(dp,inf,sizeof(dp));
            for(int i=1;i<=n;i++)
                cin>>a[i],sum[i]=sum[i-1]+a[i];
            for(int i=1;i<=n;i++){
                d[i]=a[i]*sum[i-1]+d[i-1];
            }
            for(int i=1;i<=n;i++){
                dp[i][1]=d[i];
                s[i][1]=1;
            }
            for(int j=2;j<=m+1;j++){
                s[n+1][j]=n;
                for(int i=n;i>=j;i--){
                    for(int k=s[i][j-1];k<=s[i+1][j];k++){
                        if(dp[i][j]>dp[k][j-1]+d[i]-(sum[i]-sum[k])*sum[k]-d[k]){
                            dp[i][j]=dp[k][j-1]+d[i]-(sum[i]-sum[k])*sum[k]-d[k];
                            s[i][j]=k;
                        }
                    }
                }
            }
            cout<<dp[n][m+1]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    PHP数组创建的的两种办法
    Apache配置文件(httpd.conf)的语法检查
    Apache的配置文件,使用了自己的目录,添加了别名的使用
    Apache2.4虚拟主机+别名配置
    margin外边距合并详解
    资源文件res中各种标签的使用
    http://119.145.255.64:10010/repositories/
    一个App的整个流程
    网页快捷键的使用
    oracle 日期字段的处理
  • 原文地址:https://www.cnblogs.com/wmj6/p/10719509.html
Copyright © 2011-2022 走看看