跳到正文

题解

题解 - P2254 [NOI2005] 瑰丽华尔兹

tacocat约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年10月9日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 6 分钟
题目
P2254

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

题目背景

-麻烦把琴刹关了。\text{-麻烦把琴刹关了。} -Take the brakes off,please.\text{-Take the brakes off,please.} -什么?你疯了?\text{-什么?你疯了?} -What? That’s crazy!\text{-What? That’s crazy!} -相信我,把琴刹关了。\text{-相信我,把琴刹关了。} -Trust me,Just take the brakes off.\text{-Trust me,Just take the brakes off.} ......\text{......}

题目分析

首先考虑对于时间 tt 来 dpdp:

ft,i,jf_{t,i,j} 表示在第 tt 时刻在第 ii 行第 jj 列所能获得的最长距离。

转移方程:ft,i,j=max(ft−1,i,j,ft,i′,j′+1)f_{t,i,j}=max(f_{t-1,i,j},f_{t,i',j'}+1)(i′i',j′j' 为上一个合理的位置)

这样时间复杂度为 O(T⋅n2)O(T\cdot n^2),可以过 50%50\%,但对于 100%100\% TLE 且 MLE。

所以必须优化,首先把时间 tt 换成区间 kk,令 fk,i,jf_{k,i,j} 表示在第 kk 段滑行区间中在位置 (i,j)(i,j) 所能获得最长距离。

注意到在第 kk 段时间内只能向某个方向最多走 xx 步(xx 为区间长度),得到转移方程:

fk,i,j=max(fk−1,i,j,fk,i′,j′+dis(i,j,i′,j′))f_{k,i,j}=max(f_{k-1,i,j},f_{k,i',j'}+dis(i,j,i',j'))(i′i',j′j' 为上一个合理的位置)

这个做法的时间复杂度是 O(k⋅n3)O(k\cdot n^3),会超时,需要进一步优化。

用单调队列优化掉内层的一个 nn,就可以做到 O(k⋅n2)O(k\cdot n^2),可以 AC,本代码中还使用了滚动数组优化。

用单调递减队列求最大值时,遇到障碍清空整个队列即可,另外队列比较时需要加上偏移量 disdis。

附上代码

C++
#include <cstdio>
#include <cstring>
using namespace std;

const int N = 205;
const int inf = 0x3f3f3f3f;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};

struct State {
    int val;
    int pos;
};

int n, m;
int sx, sy;
int k;
char mp[N][N];
int f[N][N];
State q[N];
int ans;

void bemax(int &a, int b) {
    a = a > b ? a : b;
}

void dp(int x, int y, int len, int d) {
    int hd = 1, ta = 0;

    for (int i = 1; x >= 1 && x <= n && y >= 1 && y <= m; i ++ , x += dx[d], y += dy[d]) {
        if (mp[x][y] == 'x')
            hd = 1, ta = 0;
        else {
            while (hd <= ta && q[ta].val + i - q[ta].pos < f[x][y])
                ta -- ;

            ta ++ ;
            q[ta].val = f[x][y];
            q[ta].pos = i;

            if (q[ta].pos - q[hd].pos > len)
                hd ++ ;

            f[x][y] = q[hd].val + i - q[hd].pos;
            bemax(ans, f[x][y]);
        }
    }
}

int main() {
    scanf("%d%d%d%d%d", &n, &m, &sx, &sy, &k);

    for (int i = 1; i <= n; i ++ )
        scanf("%s", mp[i] + 1);

    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            f[i][j] = -inf;

    f[sx][sy] = 0;

    while (k -- ) {
        int s, t, d;
        scanf("%d%d%d", &s, &t, &d);
        int len = t - s + 1;
        d -- ;

        switch (d) {
            case 0:
                for (int i = 1; i <= m; i ++ )
                    dp(n, i, len, d);

                break;
            case 1:
                for (int i = 1; i <= m; i ++ )
                    dp(1, i, len, d);

                break;
            case 2:
                for (int i = 1; i <= n; i ++ )
                    dp(i, m, len, d);

                break;
            case 3:
                for (int i = 1; i <= n; i ++ )
                    dp(i, 1, len, d);

                break;
        }
    }

    printf("%d\n", ans);

    return 0;
}

完结撒花

陆地上的人们浪费了太多时间去问为什么。\text{陆地上的人们浪费了太多时间去问为什么。} I think you land people waste a lot of time wondering why.\text{I think you land people waste a lot of time wondering why.} 冬天来临时,巴望着夏天,\text{冬天来临时,巴望着夏天,} Winter comes and you can’t wait for summer.\text{Winter comes and you can’t wait for summer.} 夏天到来时,就已经开始害怕冬天。\text{夏天到来时,就已经开始害怕冬天。} Summer comes and you live in dread of winter.\text{Summer comes and you live in dread of winter.} 所以人们永不厌倦旅行,\text{所以人们永不厌倦旅行,} That’s why you never tire of traveling.\text{That’s why you never tire of traveling.} 总是在追寻,四季如春的远方。\text{总是在追寻,四季如春的远方。} Always chasing someplace far away where it’s always summer.\text{Always chasing someplace far away where it’s always summer.} ——《海上钢琴师》\text{——《海上钢琴师》}
原文