HDU 6157(The Karting-dp)

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

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

题面

卡丁车锦标赛

一条直路上有NNN个关键点,关键点iii与i+1i+1i+1之间的路段难度为DiD_iDi( 可能为负)。

选出恰好MMM个作为检查点,用来组成若干条路线:

每条路线至少包含 2 个检查点。每个关键点最多当一次检查点,不同路线不能共用检查点。

选手按顺序依次经过路线上的检查点,最后回到第一个检查点。例如检查点顺序为 1,3,2,4 时,实际行驶顺序为 1,2,3,2,3,4,3,2,1,共掉头 4 次(回到 1 时也算一次)。

路线难度的计算方式:

初始为 0。

每经过一次路段 (i,i+1)(i, i+1)(i,i+1),难度加 DiD_iDi。

每掉头一次,难度加 D0D_0D0。

求所有路线难度之和的最大值。

首先路线之和=D0l+∑i,pathp=p1,p2,⋯ ,pl,pl=1(spi+1−spi)∗(pi+1>pi?1:−1)=D0l+∑i,pathp=p1,p2,⋯ ,pl,pl+1=12spi(左端点?−1:1)=D_0l+\sum_{i,path_p=p_1,p_2,\cdots,p_l,p_l=1}(s_{p_{i+1}}-s_{p_i})*(p_{i+1}>p_i?1:-1)=D_0l+\sum_{i,path_p=p_1,p_2,\cdots,p_l,p_l+1=1}2s_{p_i}(左端点?-1:1)=D0l+∑i,pathp=p1,p2,⋯,pl,pl=1(spi+1−spi)∗(pi+1>pi?1:−1)=D0l+∑i,pathp=p1,p2,⋯,pl,pl+1=12spi(左端点?−1:1)

设前iii个点有jjj个掉头检查点,其中kkk个ttt个顺路检查点

dp,下一个点i+1i+1i+1的情况

fj,k,t′=max⁡{fj,k,t,不选,fj−1,k−1,t−2si+1,左点,j≥1, k≥1,fj−1,k,t+2si+1,右点,j≥1, 2k≥j,fj,k,t−1,顺路检查点,t≥1, 2k>j.} f'{j,k,t} =\max\left\{ \begin{array}{ll} f{j,k,t}, & \text{不选},\\4pt f_{j-1,k-1,t}-2s_{i+1}, & \text{左点},j\ge1,\ k\ge1,\\4pt f_{j-1,k,t}+2s_{i+1}, & \text{右点},j\ge1,\ 2k\ge j,\\4pt f_{j,k,t-1}, & \text{顺路检查点},t\ge1,\ 2k>j. \end{array} \right\} fj,k,t′=max⎩ ⎨ ⎧fj,k,t,fj−1,k−1,t−2si+1,fj−1,k,t+2si+1,fj,k,t−1,不选,左点,j≥1, k≥1,右点,j≥1, 2k≥j,顺路检查点,t≥1, 2k>j.⎭ ⎬ ⎫

表示满足上述数量要求和扫描过程合法性的所有选择中,

道路贡献表达式的最大值。

cpp 复制代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<vector>
#include<set>
#include<string>
#include<queue>
#include<complex>
#include<stack>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a+b);}
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
struct FastIO {
    static const int S = 5 << 20; //MB
    int wpos; char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar() {
        static char buf[S];
        static int len = 0, pos = 0;
        if(pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if(pos == len) return -1;
        return buf[pos ++];
    }
    inline int xuint() {
        int c = xchar(), x = 0;
        while(~c && c <= 32) c = xchar();
        for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint() {
        int s = 1, c = xchar(), x = 0;
        while(c <= 32) c = xchar();
        if(c == '-') s = -1, c = xchar();
        for(; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char* s) {
        int c = xchar();
        while(c <= 32) c = xchar();
        for(; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x) {
        if(wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos ++] = x;
    }
    inline void wint(ll x) {
        if(x < 0) wchar('-'), x = -x;

        char s[24];
        int n = 0;
        while(x || !n) s[n ++] = '0' + x % 10, x /= 10;
        while(n--) wchar(s[n]);
    }
    inline void wstring(const char* s) {
        while(*s) wchar(*s++);
    }
    ~FastIO() {
        if(wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
#define MAXN (110)

int d[110],s[110],n;
ll f[MAXN][MAXN][MAXN]; 
int main()
{
    while(scanf("%d",&n)!=EOF) {
        int m=read();
        Rep(i,n) d[i]=read();
        s[1]=0;
        Fork(i,2,n) s[i]=s[i-1]+d[i-1];
        MEMi(f)
        ll inf=f[0][0][0];
        f[0][0][0]=0;
        Rep(i,n) {
            RepD(j,min(i,m-1)) Rep(k,j+1) RepD(t,min(i-j,m-j-1)) if (f[j][k][t]!=inf) {
                ll v=f[j][k][t];
                if ((j+1+1)/2<=k+1) gmax(f[j+1][k+1][t],v-2*s[i+1]);
                if ((j+1+1)/2<=k) {
                    gmax(f[j+1][k][t],v+2*s[i+1]);
                    gmax(f[j][k][t+1],v);
                }
            }
        }
        ll ans=inf;
        for(int nowm=2;nowm<=m/2*2;nowm+=2)
            gmax(ans,f[nowm][nowm/2][m-nowm]+d[0]*nowm);
        printf("%lld\n",ans);
    }
    return 0;
}
相关推荐
Cerman17 天前
VESA 发展史介绍
dp·video·monitor·vesa·edp
9527华安20 天前
FPGA纯verilog实现DP视频接口,支持2K60帧分辨率,提供3套工程源码和技术支持
fpga开发·音视频·dp
lvwangshu1 个月前
P15569 [COCI 2025/2026 #5] 结构 / Struktura 题解
矩阵·题解·dp·推导题·矩阵加速
lvwangshu1 个月前
P8981 「DROI」Round 1 距离 题解
动态规划·图论·题解·dp·树形dp·技巧·换根dp·性质题·思维好题
lvwangshu1 个月前
CF1882D Tree XOR 题解
动态规划·题解·贪心·dp·树形dp·换根dp·性质题·思维好题·推导题
所以遗憾是什么呢?1 个月前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
ting_zh1 个月前
通过 AUX 通道调试 DP Link Traning
dp·aux
longlongzihan2 个月前
LeetCode 1004.最大连续1的个数III 滑动窗口及动态规划题解
c++·算法·leetcode·dp·滑动窗口
惆怅客1232 个月前
UVa11640 Mayor Election
dp·fft·icpc·uva·生成函数的乘法