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

题意:

数轴上点[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 > f[N]  (pair 是 步长:目前最大value ) 不过时间会很多:100+ms vs 700+ms 。。。


code1

#include
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;
}



code2

#include
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;
int n, d, limi, min_step, max_step, vis[Maxn];
LL v[Maxn+5];
vector > f[Maxn+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;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
#endif
    SPEED_UP
    init();
    memset(f, -1, sizeof(f));
    f[d].push_back( make_pair(d, v[d]) );
    LL ans = v[d];
    rep(i, d+1, Maxn) {
        if (i > limi) break;
        rep(j, min_step, max_step)
            if (i - j >= 0 && f[i-j].size()) {
                int t = i-j;
                // 按照步长排序
                if (!vis[t]) {
                    if (f[t].size() > 1)
                        sort(f[t].begin(), f[t].end());
                    vis[t] = 1;
                }
                pair tp = make_pair(max(1, j-1), 0);
                auto it1 = lower_bound(f[t].begin(), f[t].end(), tp);
                tp = make_pair(j+2, 0);
                auto it2 = lower_bound(f[t].begin(), f[t].end(), tp);
                LL _max = -1;
                //cout <<"i: " << i << " j: " << j << " t: " << t << " range: " << it1->first << ' ' << it2->first << endl;
                for (auto it = it1;it != it2;++it) {
                    int tmp = it->first;
                    LL val = it->second;
                    if (tmp == max(1, j-1) || tmp == j || tmp == j+1) {
                        _max = max(_max, val);
                    }
                   // cout <<"i: " << i << " j: " << j << " t: " << t << " range: " << it1->first << ' ' << it2->first << endl;
                }
                if (_max != -1) {
                    f[i].push_back(make_pair(j, _max + v[i]));
                    ans = max(ans, _max + v[i]);
                    //cout <<"i: " << i << " j: " << j << " t: " << t << " range: " << it1->first << ' ' << it2->first << endl;
                }
            }
    }
    cout << ans << endl;
    return 0;
}


你可能感兴趣的:(codeforces,ACM.DP)