Official

C - Dash Editorial by nok0


基本的に問題文に言われた通りのシミュレーションを行えばよいですが、計算量の問題があります。アイテムの位置を二次元配列で管理すると、計算量が \(\mathrm{max}(|x_i|,|y_i|)=X\) として \(\mathrm{O}(X^2)\) になってしまいます。

これを改善するには、アイテムの座標を std::set などを用いて管理すればよいです。この工夫により、\(\mathrm{O}((N + M)\log M)\) で解くことが可能です。実装例もご確認ください。

実装例(C++):

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, m, h, k;
    cin >> n >> m >> h >> k;
    string s;
    cin >> s;
    set<pair<int, int>> st;
    for(int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        st.insert({x, y});
    }
    int nx = 0, ny = 0;
    for(int i = 0; i < n; i++) {
        int dx = 0, dy = 0;
        if(s[i] == 'R') dx = 1;
        if(s[i] == 'L') dx = -1;
        if(s[i] == 'U') dy = 1;
        if(s[i] == 'D') dy = -1;
        nx += dx, ny += dy;
        if(--h < 0) {
            cout << "No\n";
            return 0;
        }
        if(h < k and st.count({nx, ny})) {
            h = k;
            st.erase({nx, ny});
        }
    }
    cout << "Yes\n";
}

posted:
last update: