QOJ#5415 Ropeway 题解

题意

见原题中文题面

思路

首先考虑 \(O(nq)\),可以令 \(dp_i\) 表示当前取到了第 \(i\) 个点的答案,可以使用滑动窗口转移。对于必选的点可以考虑清空队列实现。

尝试优化,发现 \(k\le3\times10^3\),考虑 \(O(qk)\) 做法。

\(O(nq)\) 做法中的 dp 正反跑两遍。由于修改独立,对于每一次修改可以将答案分为前后两部分,对于修改位置后面的部分储存前缀 min,然后枚举小于修改位置的部分,这部分是 \(O(k)\) 的。

code
#include <bits/stdc++.h>
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define SP << " " <<
#define fish signed
using namespace std;
mt19937 rnd(1209);
const int INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 10;
int n, k, dp[2][N];
int mn[N];
int q[N], a[N];
void solve()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i ++) cin >> a[i];
    string s; cin >> s; s = "Y" + s;
    int hh = 1, tt = 1; q[1] = 0;
    for(int i = 1; i <= n; i ++)
    {
        while(hh <= tt && q[hh] < i - k) hh ++;
        dp[0][i] = dp[0][q[hh]] + a[i]; if(s[i] == '1') hh = tt + 1;
        while(hh <= tt && dp[0][q[tt]] >= dp[0][i]) tt --; q[++ tt] = i;
    }
    hh = 1, tt = 1; q[1] = n + 1; dp[1][n + 1] = 0;
    for(int i = n; i; i --)
    {
        while(hh <= tt && q[hh] > i + k) hh ++;
        dp[1][i] = dp[1][q[hh]] + a[i]; if(s[i] == '1') hh = tt + 1;
        while(hh <= tt && dp[1][q[tt]] >= dp[1][i]) tt --; q[++ tt] = i;
    }
    int t; cin >> t;
    while(t --)
    {
        int pos, x; cin >> pos >> x;
        int up = min(n + 1, pos + k), ans = INF;
        mn[pos] = dp[1][pos] - a[pos] + x;
        for(int i = pos; i <= pos + k && i <= n + 1; i ++)
        {
            if(i != pos) mn[i] = min(mn[i - 1], dp[1][i]);
            if(s[i] == '1') {up = i; break;}
            // cerr << i SP mn[i] << "!!!\n";
        }
        for(int i = pos - 1; i >= pos - k && i >= 0; i --)
        {
            chmin(ans, dp[0][i] + mn[min(i + k, up)]);
            if(s[i] == '1') break;
        }
        cout << ans << "\n";
    }
}
fish main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t; cin >> t;
    while(t --) solve();
    return 0;
}
posted @ 2024-11-25 22:21  lfyszy  阅读(24)  评论(0)    收藏  举报