【DP】 Codeforces Round #286 A - Mr. Kitayuta, the Treasure Hunter

1911 단어 codeforces
걸음 수가 최대 250까지 바뀐 것을 알아차렸고 그 다음은 단순 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 100000
#define maxm 10005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#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
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//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;}
// head

int a[maxn];
int dp[maxn][505];
int n, d;

void read(void)
{
	int x;
	scanf("%d%d", &n, &d);
	for(int i = 1; i <= n; i++) scanf("%d", &x), a[x]++;
}

void work(void)
{
	dp[d][250] = 1 + a[d];
	int ans = 0;
	for(int i = 1; i <= 30000; i++) {
		for(int j = 0; j <= 500; j++) {
			if(!dp[i][j]) continue;
			int t = j - 250 + d;
			ans = max(ans, dp[i][j]);
			if(t == 1) {
				dp[i+t][j] = max(dp[i+t][j], dp[i][j] + a[i+t]);
				dp[i+t+1][j+1] = max(dp[i+t+1][j+1], dp[i][j] + a[i+t+1]);
			}
			else {
				dp[i+t-1][j-1] = max(dp[i+t-1][j-1], dp[i][j] + a[i+t-1]);
				dp[i+t][j] = max(dp[i+t][j], dp[i][j] + a[i+t]);
				dp[i+t+1][j+1] = max(dp[i+t+1][j+1], dp[i][j] + a[i+t+1]);
			}
		}
	}
	printf("%d
", --ans); } int main(void) { read(); work(); return 0; }

좋은 웹페이지 즐겨찾기