「CF506A」Mr. Kitayuta, the Treasure Hunter
题解
3W*3W的dp是一种显然的做法,f[i][j]表示到第i个位置,上次跳跃j获得的最大价值
仔细思考后发现,不合法的情况很多,即j并不用从1枚举到30000
j的变化量只有根号级别
于是第二维缩减到根号n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include<iostream> #include<set> #include<map> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #include<vector> #include<queue> #include<algorithm> #include<cmath> #define T 600 #define inf 2000000000 #define pa pair<int,int> #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,d,ans; int p[30005]; int f[30005][605]; int main() { memset(f,-1,sizeof(f)); n=read();d=read(); for(int i=1;i<=n;i++) { int x=read(); p[x]++; } f[d][T/2]=0; for(int i=d;i<=30000;i++) { for(int j=1;j<=T;j++) { int t=j-T/2+d; if(i-t>=0&&t>0) { if(f[i-t][j-1]!=-1)f[i][j]=max(f[i][j],f[i-t][j-1]); if(f[i-t][j]!=-1)f[i][j]=max(f[i][j],f[i-t][j]); if(f[i-t][j+1]!=-1)f[i][j]=max(f[i][j],f[i-t][j+1]); if(f[i][j]!=-1)f[i][j]+=p[i]; ans=max(f[i][j],ans); } } } printf("%d\n",ans); return 0; } |
Subscribe