Hdu 6157 The Karting 多维DP

来源:互联网 发布:jqueryrotate.js教程 编辑:程序博客网 时间:2024/05/22 10:40

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 21 1 1 -1
 

Sample Output
6
 

Source
2017中国大学生程序设计竞赛 - 网络选拔赛


这题可以的,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;}