The Karting 2017ccpc网络赛 1008

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.
 
建模:
dp[i][j][k]表示考虑到了第i个点,选了j个检查站,左检查站比右检查站的个数的差值为k时的情况。(此处右检查站表示车行驶到该处先------>再<----)
状态转移有三种可能:
1.点i被选作右检查站,那么对答案贡献2*d[i]+d[0],d[i]此处表示从最左边行驶到i处的困难度的和。
2.点i被选作左检查站,对答案贡献为-2*d[i]+d[0]
3.点i被选作可直行的检查站,对答案无影响
 
由于对i从左往右扫描,左检查站总是比右检查站多,故不用考虑k为负。
 
代码如下:
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
int d[110];
int dp[110][110][110];
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        scanf("%d",d);d[1]=0;
        rep(i,2,n) scanf("%d",&d[i]);
        rep(i,2,n) d[i]+=d[i-1];
        rep(i,0,n) rep(j,0,n) rep(k,0,n) dp[i][j][k]=-1e8;
        dp[0][0][0]=0;
        rep(i,1,n)
        {
            dp[i][0][0]=0;
            rep(j,1,i)
            {
                rep(k,0,j) dp[i][j][k]=dp[i-1][j][k];
                rep(k,1,j) dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k-1]-2*d[i]+d[0]); //在该点设从左向右掉头的检查站
                rep(k,0,j-1) dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k+1]+2*d[i]+d[0]); //在该点设从右向左掉头的检查站
                rep(k,0,j) dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k]);            //在该点设不需掉头的检查站
            }
        }
        printf("%d
",dp[n][m][0]);
    }
    return 0;
}
 
 
 
原文地址:https://www.cnblogs.com/zhixingr/p/7454543.html