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


code1

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



code2

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值