2014
02-17

# Lawrence

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.

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.

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.

4 1
4 5 1 2
4 2
4 5 1 2
0 0

17
2

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1010;
int dp[N][N],s[N][N];
int val[N],sv[N],dv[N];
const int infinity=(-1)^(1<<31);
int S(int i,int j){ // i到j作为一段
return dv[i]-dv[j]-(sv[i]-sv[j])*sv[j+1];
}
int DP(int n,int m){
if(m >= n-1) return 0;
m++;
for(int i=1;i<=n;i++) dp[i][1]=S(i,n),s[i][1]=n;
for(int mm=2;mm<=m;mm++){
int maxi=n-mm+1,tmp;
dp[maxi][mm]=0; s[maxi][mm]=maxi;
//printf("dp[%d][%d] = %d, s = %d\n",maxi,mm,dp[maxi][mm],s[maxi][mm]);
for(int i=maxi-1;i>0;i--){
dp[i][mm]=infinity;
for(int k=i;k<=s[i+1][mm];k++)
if((tmp=dp[k+1][mm-1]+S(i,k)) < dp[i][mm])
dp[i][mm]=tmp,s[i][mm]=k;
//printf("dp[%d][%d] = %d, s = %d\n",i,mm,dp[i][mm],s[i][mm]);
}
}
return dp[1][m];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)){
if(n<=0 && m<=0) break;
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
sv[n+1]=0,dv[n+1]=0;
for(int i=n;i>0;i--)
dv[i]=sv[i+1]*val[i]+dv[i+1],sv[i]=sv[i+1]+val[i];// printf("dv[i] = %d, sv[i] = %d\n",dv[i],sv[i]);
//int i,j; while(cin>>i>>j) printf("sij = %d\n",S(i,j));
printf("%d\n",DP(n,m));
}
return 0;
}

1. “再把所有不和该节点相邻的节点着相同的颜色”，程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的

2. 我没看懂题目
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
第二个是7 0 6 -1 1 -6 7输出是14 7 7
不知道题目例子是怎么得出来的

3. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同，其树的前、中、后序遍历是相同的，但在此处不能使用中序遍历，因为，中序遍历的结果就是排序的结果。经在九度测试，运行时间90ms，比楼主的要快。