hdu 2829 Lawrence 斜率优化

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829

Lawrence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2910    Accepted Submission(s): 1281


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
 


如果不会斜率优化 可以看下这个 一维的题

http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html

题意:

n长度的轨道,截m处。 每段的价值是 这段里 每个数两两相乘的和。问不同的截法最大的价值和。


做法:

i是轨道长度,j是炸药的量

cost[i]是前i个数字 两两相乘的和。

dp[i][j]=min{dp[k][j-1]+cost[i]-cost[k]-sum[k]*(sum[i]-sum[k])}

可以推倒出来:

 y(i)=dp[i][]-cost[i]+sum[i]^2

x(i)=sum[i]

斜率是 sum[i]


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>

#define inf 0x7f7f77f
#define ll __int64

ll sum[1010],cost[1010];
ll num[1010];
ll dp[1010][1010];
ll que[1010];

ll cc;
ll y(ll j)
{
	return dp[j][cc-1]-cost[j]+sum[j]*sum[j];
}

int main()
{
	ll n,m;
	
	while(scanf("%I64d%I64d",&n,&m),n||m)
	{
		//memset(dp,0,sizeof dp);
		dp[1][0]=sum[0]=cost[0]=0;
		for(ll i=1;i<=n;i++)
		{
			scanf("%I64d",num+i);
			sum[i]=sum[i-1]+num[i];
			dp[i][0]=cost[i]=cost[i-1]+num[i]*sum[i-1];
		}



		for(ll c=1;c<=m;c++)//几个地方炸
		{
			ll tou=0,wei=0;

			que[wei++]=c;
			
			dp[c+1][c]=0;
			cc=c;
			for(ll i=c+1;i<=n;i++)//长度//  i>j
			{
				while(wei-tou>=2&&(y(que[tou+1])-y(que[tou]))<=sum[i]*(sum[que[tou+1]]-sum[que[tou]]))
					tou++;
				dp[i][c]=dp[que[tou]][c-1]+cost[i]-cost[que[tou]]-sum[que[tou]]*(sum[i]-sum[que[tou]]);
				//if(dp[i][c])
				//printf("%I64d ",dp[i][c]);
				
				while(wei-tou>=2&&(sum[i]-sum[que[wei-1]])*(y(que[wei-1])-y(que[wei-2]))>=(sum[que[wei-1]]-sum[que[wei-2]])*(y(i)-y(que[wei-1])))//g(b,a)>g(c,b)
					wei--;
				que[wei++]=i;
			}
			//puts("");
		}
		printf("%I64d\n",dp[n][m]);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值