Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2484 Accepted Submission(s): 1105
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
2009 Multi-University Training Contest 2 - Host by TJU
Recommend
gaojie | We have carefully selected several similar problems for you: 2830 2832 2828 2827 2833
题目描述 :
在一条具有n个仓库的道路上,放置m个炸弹,被炸断之后的这段铁路[i,j],它的值为a[i]*(a[i+1],,,,a[j])+a[i+1]*a(a[i+2],,,+a[j])+,,,,+a[j-1]*a[j];,求被炸弹分割之后的铁路和的最小值
The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17.
d[i][k]表示在前i个仓库里放k个炸弹,cost[i][j]表示[i,j]区间内的Strategic Value
d[i][k]=min(d[j][k-1]+cost[j+1][i]);
cost[i][j]=cost[i][j-1]+sum*a[j],sum==(a[i]+a[i+1]+a[i+2]+,,,,+a[j-1])
时间复杂度为1000*1000*1000=10^9,肯定会超时。
可以采用斜率优化或者四边形优化。
目前只会四边形优化,还有此题wa了很久的原因是无穷大的设立,inf=1<<20,不够大,inf=1e30才够大,
提醒我们需要注意细节。
设立初始值,d[i][0]=cost[1][i],用s[i][j]表示d[i][j]的决策。s[i][0]=0,s[n+1][i]=n;
1:s[i][k-1]<=s[i][k]<=s[i+1][k];
2:d[i][k]=min(d[j][k-1]+cost[j+1][i]);
观察表达式,k从小到大枚举(2式),i从大到小枚举(1式中s[i][k]<=s[i+1][k])。k在外层循环,i在内层循环(1式中s[i][k]<=s[i+1][k])
#include <iostream> #include <cstdio> #include <cstring> #define M 1100 #define LL long long const int inf =1e18; using namespace std; int n,m; LL a[1100]; LL cost[M][M]; LL d[M][M]; int s[M][M]; void init() { memset(d,0,sizeof(d)); memset(cost,0,sizeof(cost)); memset(s,0,sizeof(s)); } void solve() { /* for(int i=1;i<=n;i++) { LL sum=0; int j; for(int t=1;t<=n;t++) { j=i+t; if(j>n) break; sum+=a[j-1]; cost[i][j]=cost[i][j-1]+sum*a[j]; } }*/ for(int i=1;i<=n;i++){ LL sum=0; cost[i][i]=0; for(int j=i+1;j<=n;j++){ sum+=a[j-1]; cost[i][j]=cost[i][j-1]+sum*a[j]; } } /* for(int i=1;i<=n;i++) {for(int j=1;j<=n;j++) printf("%d ",cost[i][j]); printf("\n"); } printf("\n"); */ for(int i=0;i<=n;i++) { s[i][0]=0; s[n+1][i]=n; d[i][0]=cost[1][i]; } for(int k=1;k<=m;k++) for(int i=n;i>=1;i--) { d[i][k]=1e18; for(int j=s[i][k-1];j<=s[i+1][k];j++) { if((d[j][k-1]+cost[j+1][i])<d[i][k]) { d[i][k]= d[j][k-1]+cost[j+1][i]; s[i][k]=j; } } } /* for(int i=0;i<=n+1;i++) {for(int j=0;j<=m;j++) printf("%d ",s[i][j]); printf("\n"); } printf("\n"); for(int i=0;i<=n;i++) {for(int j=0;j<=m;j++) printf("%d ",d[i][j]); printf("\n"); } printf("\n");*/ printf("%lld\n",d[n][m]); } int main() { //freopen("test.txt","r",stdin); while(scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; init(); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); solve(); } return 0; }