1. 程式人生 > >[Vijos 1283][OIBH杯NOIP2006第二次模擬賽]佳佳的魔杖

[Vijos 1283][OIBH杯NOIP2006第二次模擬賽]佳佳的魔杖

端點 targe int urn 超過 如果 pre include nbsp

Source

這是DP題,f[i][j] 表示左端點不超過i且右端點不超過j的最優方案。

很明顯轉移方程之一是f[i][j] = max( f[i - 1][j], f[i][j - 1] );

還有如果長度滿足,可以f[i][j] = max( f[i][j], f[i - 1][j - 1] + 得到魔力值 );

話不多說,上代碼——

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1005
#define LL long long

int n, lo, hi;
LL L[MAXN], M[MAXN];
LL f[MAXN][MAXN];

int main(){ scanf( "%d%d%d", &n, &lo, &hi ); for ( int i = 1; i <= n; ++i ) scanf( "%lld", &L[i] ), L[i] += L[i - 1]; for ( int i = 1; i <= n; ++i ) scanf( "%lld", &M[i] ), M[i] += M[i - 1]; for ( int i = 1; i <= n; ++i ) for ( int j = 1; j <= i; ++j ){ f[i][j]
= max( f[i - 1][j], f[i][j - 1] ); if ( L[i] - L[j - 1] >= lo && L[i] - L[j - 1] <= hi ) f[i][j] = max( f[i][j], f[i - 1][j - 1] + M[i] - M[j - 1] ); } printf( "%lld", f[n][n] ); return 0; }

[Vijos 1283][OIBH杯NOIP2006第二次模擬賽]佳佳的魔杖