Hdu 6157 The Karting 多维DP

The Karting

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


Problem Description
The Karting championship will be held on a straight road. There are N keypoints on the road. The path between keypoint i and i+1 has a degree of difficulty Di(Di may be negative if the path is too smooth). Now the Organizers want to darw up some routes among these keypoints(The number of routes can be many to avoid a boring match). The organizers will choose some checkpoints from the keypoints for the routes(Each route shold include at least two checkpoints, and each keypoint can not be chosen as checkpoint more than once. Two routes can not share one checkpoint). The players should drive their karts to pass the checkpoints in the given order and return to the first checkpoint. 

For example, if there are 4 checkpoints 1,3,2,4 in order in a route, players shold drive pass keypoint 1,2,3,2,3,4,3,2,1 in order. In this example, the players should make a 180 degree turn 4 times in the route(When players return to checkpoint 1, they also need to make a 180 degree turn). Makeing a 180 degree turn also has a degree of difficulty D0. The difficulty of a route is defined as follow. The initial difficluty is 0. Each time the players in the route need to pass the path between keypoint i and i+1, the difficulty shold increase Di, and each time the players need to make a 180 degree turn, the difficulty should increase D0.

To make the championship more exciting, the organizers want to maximize the sum of difficulty of all routes. They will choose exactly M keypoints to set up checkpoints. So what is the maximum sum of difficulty of all routes?
 

Input
There are multiple test cases. 
The first line of each test case contains two integers N and M(2<=M<=N<=100). 
The second line contains N integers D0,D1,D2,...,Dn-1(-100<=Di<=100).
 

Output
One integer in a single line for each test case, the maximum sum of difficulty of all routes.
 

Sample Input
  
  
4 2 1 1 1 -1
 

Sample Output
  
  
6
 

Source


这题可以的,DP状态很难想。


dp[i][j][k]表示前 i 个点中选了 j 个ckeckpoint , 其中从左向右拐弯的比从右向左拐弯的多k个。

用sum[i]表示从1号点到 i 号点的代价。当dp到 i 号点的时候,有四种可能:

1.什么也不做,代价是dp[i-1][j][k]

2.选择 i 点做不需要拐弯的checkpoint,代价是dp[i-1][j-1][k]

3.选择 i 点做从右向左拐弯的checkpoint,代价是dp[i-1][j-1][k+1]+d0+2*sum[i]

4.选择 i 点做从左向右拐弯的checkpoint,代价是dp[i-1][j-1][k-1]+d0-2*sum[i]


最后答案就是dp[n][m][0]


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=105,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int dp[maxn][maxn][maxn];
int sum[maxn];

int main() {
	int n,m;
	while (scanf("%d%d",&n,&m)!=EOF) {
		int i,j,k,x,c;
		for (i=0;i<=n;i++) 
			for (j=0;j<=n;j++) 
				for (k=0;k<=n;k++) 
				    dp[i][j][k]=-inf;
		dp[0][0][0]=0;
		scanf("%d",&x);
		sum[1]=0;
		for (i=2;i<=n;i++) {
			scanf("%d",&c);
			sum[i]=sum[i-1]+c;
		}
		for (i=1;i<=n;i++) {
			dp[i][0][0]=0;
			for (j=1;j<=min(i,m);j++) {
				for (k=0;k<=j;k++) 
				    dp[i][j][k]=max(dp[i][j][k],max(dp[i-1][j][k],dp[i-1][j-1][k]));
				for (k=1;k<=j;k++)
				    dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k-1]+x-2*sum[i]);
				for (k=0;k<j;k++)
				    dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k+1]+x+2*sum[i]);
			}
		}
		printf("%d\n",dp[n][m][0]);
	} 
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值