zoukankan      html  css  js  c++  java
  • HDU 2829 Lawrence

    Lawrence

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2829
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

     
    解题:斜率优化
     $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])$

     

    $可以发现此时线kx + y的斜率k是sum[i]$ $x 是sum[k],y是dp[k][j-1] + cost[1][i] - cost[1][k] + sum^2[k]$

     

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1010;
     4 int sum[maxn],cost[maxn],q[maxn],dp[maxn][maxn],n,m;
     5 bool check(int a,int b,int i,int j){
     6    int tmp = dp[a][j-1] - cost[a] + sum[a]*sum[a];
     7    tmp -= dp[b][j-1] - cost[b] + sum[b]*sum[b];
     8    return tmp > sum[i]*(sum[a] - sum[b]);//a 劣于 b
     9 }
    10 int up(int a,int b,int j){
    11     int y1 = dp[a][j-1] - cost[a] + sum[a]*sum[a];
    12     int y2 = dp[b][j-1] - cost[b] + sum[b]*sum[b];
    13     return y1 - y2;
    14 }
    15 int down(int a,int b){
    16     return sum[a] - sum[b];
    17 }
    18 int main(){
    19     while(scanf("%d%d",&n,&m),n||m){
    20         sum[0] = cost[0] = 0;
    21         for(int i = 1; i <= n; ++i){
    22             scanf("%d",sum + i);
    23             cost[i] = cost[i-1] + sum[i-1]*sum[i];
    24             sum[i] += sum[i-1];
    25             dp[i][0] = cost[i];
    26             dp[i][i-1] = 0;
    27         }
    28         for(int j = 1; j <= m; ++j){
    29             int hd = 0,tl = 0;
    30             q[tl++] = j;
    31             for(int i = j + 1; i <= n; ++i){
    32                 while(hd + 1 < tl && check(q[hd],q[hd+1],i,j)) ++hd;
    33                 dp[i][j] = dp[q[hd]][j-1] + cost[i] - cost[q[hd]] - sum[q[hd]]*(sum[i] - sum[q[hd]]);
    34                 while(hd + 1 < tl && up(q[tl-1],q[tl-2],j)*down(i,q[tl-1]) >= up(i,q[tl-1],j)*down(q[tl-1],q[tl-2])) --tl;
    35                 q[tl++] = i;
    36             }
    37         }
    38         printf("%d
    ",dp[n][m]);
    39     }
    40     return 0;
    41 }
    View Code

    传说中的平行四边形优化

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 1010;
     5 int sum[maxn],cost[maxn],dp[maxn][maxn],s[maxn][maxn],n,m;
     6 int main(){
     7     while(scanf("%d%d",&n,&m),n||m){
     8         sum[0] = cost[0] = 0;
     9         for(int i = 1; i <= n; ++i){
    10             scanf("%d",sum + i);
    11             cost[i] = cost[i-1] + sum[i-1]*sum[i];
    12             sum[i] += sum[i-1];
    13             dp[i][0] = cost[i];
    14             dp[i][i-1] = 0;
    15             s[n+1][i] = n;
    16             s[i][0] = 0;
    17         }
    18         for(int j = 1; j <= m; ++j){
    19             for(int i = n; i > j; --i){
    20                 dp[i][j] = INF;
    21                 for(int k = s[i][j-1]; k <= s[i+1][j]; ++k){
    22                     int tmp = cost[i] - cost[k] - sum[k]*(sum[i] - sum[k]);
    23                     if(dp[k][j-1] + tmp < dp[i][j]){
    24                         dp[i][j] = dp[k][j-1] + tmp;
    25                         s[i][j] = k;
    26                     }
    27                 }
    28             }
    29         }
    30         printf("%d
    ",dp[n][m]);
    31     }
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    Java第二十五天,多线程之等待唤醒机制
    Java第二十四天,线程安全
    多线程第二十三天,多线程
    Java第二十二天,异常
    数据结构之广义表
    MS Office使用技巧
    标准C++中的String类的使用
    SQL中创建外键约束
    关系数据标准语言SQL之数据查询
    Windows命令
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4809428.html
Copyright © 2011-2022 走看看