【TopCoder 10664】RowGame(有后效性的动态规划)

题目链接:【TopCoder 10664】RowGame

题目大意:给定 n 个格子,每个格子有一个权值 w i 。有一枚棋子,初始在 1 号格子。你要对棋子进行不大于 k 轮移动,奇数轮向右移,偶数轮向左移,每次移动任意格,获得的价值为原来棋子的位置到新棋子位置格子的权值之和。初始分值为 0 ,每次移动后 + = 。要求每次移动后分值非负,求得到的最大分值。

如果没有分值非负的条件,就是求最大子段和。但是,分值非负导致棋子不能在格子中随意移动,所以我们需要 D P

d i s t [ i ] 表示将棋子移动到格子 i 至少要经过几轮, c o s t [ i ] 表示在移动 d i s t [ i ] 轮后走到格子 i 能获得的最大分值。不难转移,但是细节较多。为了方便起见,我们将一个格子拆成两个,分别表示向左移和向右移。令 m a x c [ i ] 表示格子 i 走到格子 j 在走回来得到的最大分值。这样就可以使程序变得简单许多。具体状态转移方程和细节见代码。

最后,对于每一个点,用 c o s t [ i ] + ( k d i s t [ i ] ) 更新答案。完结撒花!┴┴~(≥▽≤)/~┴┴

#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
const ll ninfll = 0xc0c0c0c0c0c0c0c0;
struct RowGame {
    int n, m, dist[maxn];
    ll a[maxn][maxn], maxc[maxn], cost[maxn];
    template <typename type> type max(type a, type b) {
        return a > b ? a : b;
    } 
    ll score(vector<int> vec, int maxs) {
        n = vec.size(), m = n << 1;
        memset(a, 0xc0, sizeof(a));
        for (int i = 1; i <= n; i++) {
            ll sum = 0;
            for (int j = i; j <= n; j++) {
                sum += vec[j - 1];
                a[i][j + n] = a[j + n][i] = sum;
            }
        }
        memset(maxc, 0xc0, sizeof(maxc));
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                maxc[i] = max(maxc[i], a[i][j] + a[j][i]);
            }
        }
        memset(dist, 0x3f, sizeof(dist));
        memset(cost, 0xc0, sizeof(cost));
        dist[1] = cost[1] = 0;
        for (int k = 1; k < m; k++) {
            for (int i = 1; i <= m; i++) {
                if (dist[i] == inf) {
                    continue;
                }
                for (int j = 1; j <= m; j++) {
                    if (a[i][j] == ninfll) {
                        continue;
                    }
                    ll ndist = dist[i], ncost = cost[i];
                    if (ncost + a[i][j] < 0) {
                        if (maxc[i] <= 0) {
                            continue;
                        }
                        ll shua = (-(ncost + a[i][j]) + maxc[i] - 1) / maxc[i];
                        ndist += shua * 2, ncost += shua * maxc[i];
                    }
                    ndist++, ncost += a[i][j];
                    if (ndist < dist[j] || (ndist == dist[j] && ncost > cost[j])) {
                        dist[j] = ndist, cost[j] = ncost;
                    }
                }
            }
        }
        ll ret = 0;
        for (int i = 1; i <= m; i++) {
//          printf("%d %d %lld\n", i, dist[i], cost[i]);
            if (dist[i] > maxs) {
                continue;
            }
            ret = max(ret, cost[i]);
            if (maxc[i] >= 0) {
                ret = max(ret, cost[i] + (maxs - dist[i]) / 2 * maxc[i]);
                if (dist[i] < maxs) {
                    for (int j = 1; j <= m; j++) { 
                        if (a[i][j] != ninfll) {
                            ret = max(ret, cost[i] + (maxs - dist[i] - 1) / 2 * maxc[i] + a[i][j]);
                        }
                    }
                }
            }
        }
        return ret;
    }
};
#ifndef ONLINE_JUDGE
RowGame rg;
vector<int> vec;
int n, m;
int main() {
    scanf("%d %d", &n, &m);
    for (int x, i = 0; i < n; i++) {
        scanf("%d", &x);
        vec.push_back(x);
    }
    printf("%lld\n", rg.score(vec, m));
    return 0;
}
#endif

听说还有不用最短路处理的方法,现在还在探究中。

猜你喜欢

转载自blog.csdn.net/weixin_42068627/article/details/81023647