HDU 2829 Lawrence

Source: Internet
Author: User

Lawrencetime limit:1000msmemory limit:32768kbthis problem'll be judged onHDU. Original id:2829
64-bit integer IO format: %i64d Java class name: Main T. E. Lawrence is a controversial figure during World War I. He is a British officer who served in the Arabian Theater and led a group of Arab nationals in guerilla strikes against T He Ottoman Empire. His primary targets were the railroads. A highly fictionalized version of his exploits is presented in the blockbuster movie, "Lawrence of Arabia".

You is to write a program to help Lawrence the figure of how to the best use of his limited resources. You are some information from British Intelligence. First, the rail line was completely linear---there is no branches, no spurs. Next, British Intelligence have assigned a strategic importance to each depot---an integer from 1 to 100. A Depot is the no use in its own, it's only have value if it's 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 is 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 this Lawrence only have enough resources for one attack. He cannot attack the depots themselves---they is 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 V Alue that he can achieve for that railroad.
InputThere would be several data sets. Each data set would begin with a line with the integers, N and M. N is the number of depots on the railroad (1≤n≤1000), and M is the number of attacks Lawrence have resources for (0≤m<n). On the next line would be n integers, each from 1 to, indicating the strategic Value of each depot in order. End of input would be marked by a line with n=0 and m=0, which should isn't be processed.OutputFor each data set, output a single integer, indicating the smallest strategic Value for the railroad that Lawrence can ach Ieve with his attacks. Output each of the integer in it own line.Sample Input
4 14 5 1 24 24 5 1 20 0
Sample Output
172
SourceMulti-university Training Contest 2-host by tju problem solving: slope optimization
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =1010;4 intsum[maxn],cost[maxn],q[maxn],dp[maxn][maxn],n,m;5 BOOLCheckintAintBintIintj) {6    intTMP = dp[a][j-1]-Cost[a] + sum[a]*Sum[a];7TMP-= dp[b][j-1]-Cost[b] + sum[b]*Sum[b];8    returntmp > sum[i]* (sum[a]-sum[b]);//a inferior to B9 }Ten intUpintAintBintj) { One     intY1 = dp[a][j-1]-Cost[a] + sum[a]*Sum[a]; A     inty2 = dp[b][j-1]-Cost[b] + sum[b]*Sum[b]; -     returnY1-Y2; - } the intDownintAintb) { -     returnSum[a]-Sum[b]; - } - intMain () { +      while(SCANF ("%d%d", &n,&m), n| |m) { -sum[0] = cost[0] =0; +          for(inti =1; I <= N; ++i) { Ascanf"%d", Sum +i); atCost[i] = cost[i-1] + sum[i-1]*Sum[i]; -Sum[i] + = sum[i-1]; -dp[i][0] =Cost[i]; -dp[i][i-1] =0; -         } -          for(intj =1; J <= M; ++j) { in             intHD =0, TL =0; -q[tl++] =J; to              for(inti = j +1; I <= N; ++i) { +                  while(HD +1< TL && Check (q[hd],q[hd+1],I,J)) + +HD; -DP[I][J] = dp[q[hd]][j-1] + cost[i]-COST[Q[HD] "-sum[q[hd]]* (Sum[i]-SUM[Q[HD]]); the                  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; *q[tl++] =i; $             }Panax Notoginseng         } -printf"%d\n", Dp[n][m]); the     } +     return 0; A}
View Code

HDU 2829 Lawrence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.