【DP】 467C George and Job

简单DP。。。每个点有选和不选两种情况,推一下就好了。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 5005
#define maxm 200005
#define eps 1e-10
#define mod 10000007
#define INF 1e17
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head

LL dp[maxn][maxn], sum[maxn];
int n, m, k;
void read(void)
{
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1; i <= n; i++) scanf("%I64d", &sum[i]);
	for(int i = 1; i <= n; i++) sum[i] += sum[i-1];
}
void work(void)
{
	memset(dp, -1, sizeof dp);
	dp[0][0] = 0;
	for(int i = 0; i < n; i++)
		for(int j = 0; j <= k; j++) {
			if(dp[i][j] == -1) continue;
			dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
			if(i + m <= n) dp[i+m][j+1] = max(dp[i+m][j+1], dp[i][j] + sum[i+m] - sum[i]);
		}
	printf("%I64d\n", dp[n][k]);
}
int main(void)
{
	read();
	work();
	return 0;
}


你可能感兴趣的:(codeforces)