Lawrence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2484 Accepted Submission(s): 1105
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.
4 5 1 2
4 2
4 5 1 2
0 0
2
最有代价用d[i,j]表示
d[i,j]=min{d[i,k-1]+d[k+1,j]}+w[i,j]
其中w[i,j]=sum[i,j]
四边形不等式
w[a,c]+w[b,d]<=w[b,c]+w[a,d](a<b<c<d) 就称其满足凸四边形不等式
决策单调性
w[i,j]<=w[i',j'] ([i,j]属于[i',j']) 既 i'<=i<j<=j'
于是有以下三个定理
定理一: 如果w同时满足四边形不等式 和 决策单调性 ,则d也满足四边形不等式
定理二:当定理一的条件满足时,让d[i,j]取最小值的k为K[i,j],则K[i,j-1]<=K[i,j]<=K[i+1,j]
定理三:w为凸当且仅当w[i,j]+w[i+1,j+1]<=w[i+1,j]+w[i,j+1]
由定理三知 判断w是否为凸即判断 w[i,j+1]-w[i,j]的值随着i的增加是否递减
于是求K值的时候K[i,j]只和K[i+1,j] 和 K[i,j-1]有关,所以 可以以i-j递增为顺序递推各个状态值最终求得结果 将O(n^3)转为O(n^2)
The Strategic Value of the remaining railroad is 5*1 + 5*2 + 1*2 = 17.
(设w[i][j]表示i到j的value,f[i][j]表示为前j个仓库放i个炸弹,那么可以得到f[i][j]=min{f[i-1][k-1]+w[k][j]}(i<k<=j),如果直接dp的话是O(M*N^2)的复杂度。
然而这个状态转移方程和POJ_1160的状态转移方程是一样的,因而只要我们能够证明w为凸的话,就可以证明f[i][j]为凸,那么就可以用四边形不等式进行优化了。
要证w为凸,只要证w[i][j]+w[i+1][j+1]<=w[i][j+1]+w[i+1][j]即可,也就是证明w[i+1][j]-w[i][j]是关于j单调递减的。又因为w[i][j]=w[i+1][j]+(A[j]-A[i])*a[i],其中a[i]表示i点权,A[i]表示1,2,…,i的点权和,代入到w[i+1][j]-w[i][j]中就可以得到w[i+1][j]-w[i][j]=(A[i]-A[j])*a[i],显然是随j的增加而递减的。这样我们就证明出了w为凸,进而可以证明f[i][j]的求解可以用四边形不等式优化。)
#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[];
LL cost[M][M];
LL d[M][M];
int s[M][M];
void init()
{
memset(d,,sizeof(d));
memset(cost,,sizeof(cost));
memset(s,,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=;i<=n;i++){
LL sum=;
cost[i][i]=;
for(int j=i+;j<=n;j++){
sum+=a[j-];
cost[i][j]=cost[i][j-]+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=;i<=n;i++)
{
s[i][]=;
s[n+][i]=n;
d[i][]=cost[][i];
}
for(int k=;k<=m;k++)
for(int i=n;i>=;i--)
{
d[i][k]=1e18;
for(int j=s[i][k-];j<=s[i+][k];j++)
{
if((d[j][k-]+cost[j+][i])<d[i][k])
{
d[i][k]= d[j][k-]+cost[j+][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==&&m==)
break;
init();
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
solve();
}
return ;
}