zoukankan      html  css  js  c++  java
  • HDU2829 Lawrence —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-2829

    Lawrence

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


    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

    题意:

    给出一个序列,定义一个连续段的值为:连续段内每每两个数的积之和。现要求将序列断开m处,即把序列断成m+1断子序列。使得每段的值之和最小。

    题解:

    1.设dp[i][j]为:第j个数属于第i段时,值之和的最小值。可得:dp[i][j] = min(dp[i-1][k] + cost[k+1][i]) 其中 i-1<=k<=j-1。

    2.对于cost[i][j]:

      可知:cost[1][j] = cost[1][k] + cost[k+1][j] + sum[k]*(sum[j] - sum[k]),

      移项:cost[k+1][j] = cost[1][j] - cost[1][k] -  sum[k]*(sum[j] - sum[k]),因而cost数组可改成一维,sum为前缀和。

      因此:dp[i][j] = min(dp[i-1][k] + cost[j] - cost[k] - sum[k]*(sum[j] - sum[k])) 其中 i-1<=k<=j-1。

      此步转化的目的是要分离 i 和 j,使得在使用斜率优化时能够去掉 i ,变成只与 j 、k有关的式子。

    3.斜率优化DP,与此题HDU3480 Division的形式一样。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int mod = 1e9+7;
    17 const int MAXM = 1e5+10;
    18 const int MAXN = 1e3+10;
    19 
    20 int sum[MAXN], cost[MAXN], dp[MAXN][MAXN];
    21 int q[MAXN], head, tail;
    22 
    23 int getUP(int i, int k1, int k2)
    24 {
    25     return (dp[i-1][k1] - cost[k1] + sum[k1]*sum[k1])-
    26             (dp[i-1][k2] - cost[k2] + sum[k2]*sum[k2]);
    27 }
    28 
    29 int getDOWN(int k1, int k2)
    30 {
    31     return sum[k1]-sum[k2];
    32 }
    33 
    34 int getDP(int i, int j, int k)
    35 {
    36     return dp[i-1][k] + cost[j] - cost[k] - sum[k]*(sum[j]-sum[k]);
    37 }
    38 
    39 int main()
    40 {
    41     int n, m;
    42     while(scanf("%d%d", &n,&m)&&(m||n))
    43     {
    44         sum[0] = cost[0] = 0;
    45         for(int i = 1; i<=n; i++)
    46         {
    47             int val;
    48             scanf("%d", &val);
    49             sum[i] = sum[i-1] + val;
    50             cost[i] = cost[i-1] + val*sum[i-1];
    51         }
    52 
    53         for(int i = 1; i<=n; i++)   //初始化第一阶段
    54             dp[1][i] = cost[i];
    55         for(int i = 2; i<=m+1; i++)   //从分成i-1段的状态转移到分成i段的状态
    56         {
    57             head = tail = 0;
    58             q[tail++] = i-1;  //因为分成i-1段最少需要i-1个数,故备选状态从i-1开始
    59             for(int j = i; j<=n; j++) //因为分成i段最少需要i个数,故从i开始
    60             {
    61                 while(head+1<tail && getUP(i, q[head+1],q[head])<=
    62                       getDOWN(q[head+1],q[head])*sum[j])  head++;
    63                 dp[i][j] = getDP(i, j, q[head]);
    64 
    65                 while(head+1<tail && getUP(i, j, q[tail-1])*getDOWN(q[tail-1],q[tail-2])<=
    66                       getUP(i, q[tail-1],q[tail-2])*getDOWN(j,q[tail-1]))  tail--;
    67                 q[tail++] = j;
    68             }
    69         }
    70         printf("%d
    ", dp[m+1][n]);
    71     }
    72 }
    View Code
  • 相关阅读:
    如何用Django建立一个后台CRM系统02
    如何用Django建立一个后台CRM系统01
    多线程的UDP聊天器
    文件被导入时,能够直接执行的代码不需要被执行
    主动抛出异常
    python异常
    初始化动作只执行一次
    单例设计模式代码实现
    类的继承
    多态的案例演示
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8207113.html
Copyright © 2011-2022 走看看