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

题目链接:【TopCoder 10664】RowGame

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

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

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

最后,对于每一个点,用 cost[i]+(kdist[i]) 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

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值