codeforces 505C Mr. Kitayuta, the Treasure Hunter(DP)

来源:互联网 发布:怎么域名备案 编辑:程序博客网 时间:2024/04/29 18:10

题意:

数轴上点[0, 30000]。先从 0 跳到 d,  以后每次跳的步数是 {prev-1, prev, prev+1}。跳到某个点上可以得到相应的value,求可以得到最大的value。

思路:

n, d 最大都是30000, 所以 min_step >= d-246, max_step<= d-246

所以方程 f[i, j] 当前在 i, 前一次跳的长度是 j

注意要把 [min_step, max_step] 映射到 [0, 2*246] 上面来。。

或者直接开 vector< pair<int, LL> > f[N]  (pair 是 步长:目前最大value )

#include<bits/stdc++.h>  using namespace std;    #define SPEED_UP iostream::sync_with_stdio(false);  #define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);  #define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))  #define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))    typedef long long LL;    const int Maxn = 30000;  const int D = 250;  const int Maxn2 = D*2;    int n, d, limi, min_step, max_step, offset;  LL v[Maxn+5], f[Maxn+5][Maxn2+5];    void init() {      cin >> n >> d;      limi = -1;      rep(i, 1, n) {          int t;cin >> t;++v[t];limi = max(limi, t);      }      min_step = max(1, d - D);      max_step = d + D;      offset = min_step;  }    int test(int x) {      int sum = 0, k = x;      while (sum <= Maxn) sum += k++;      max_step = k;      sum = 0, k = 1;      while(true) {          if ((k+x)*(x-k+1)/2 < Maxn) break;          ++k;      }      return max_step - k+1;  }    int main() {  #ifndef ONLINE_JUDGE      freopen("input.in", "r", stdin);  #endif      SPEED_UP      init();        memset(f, -1, sizeof(f));      f[d][d-offset] = v[d];      LL ans = f[d][d-offset];      rep(i, d+1, Maxn) {          if (i > limi) break;          rep(j, min_step, max_step)              if (i - j >= 0) {                  //cout << "Calc f[" << i << ", " << j << "]\n";                  int t = i-j;                  LL _max = -1;                  if (f[t][j+1-offset] != -1) _max = max(_max, f[t][j+1-offset]);                  if (f[t][j-offset]   != -1) _max = max(_max, f[t][j-offset]);                  if (f[t][j-1-offset] != -1) _max = max(_max, f[t][j-1-offset]);                  if (_max != -1) f[i][j-offset] = _max + v[i];                  ans = max(ans, f[i][j-offset]);              }      }      cout << ans << endl;      return 0;  }  



阅读全文
0 0
原创粉丝点击