Submission #1357364

#TimeUsernameProblemLanguageResultExecution timeMemory
1357364Desh03Jakarta Skyscrapers (APIO15_skyscraper)C++20
22 / 100
7 ms664 KiB
#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> v(n);
    set<pair<int, int>> st;
    int u, w;
    for (int i = 0; i < m; i++) {
        int b, p;
        cin >> b >> p;
        if (i == 0) u = b;
        if (i == 1) w = b;
        if (p >= n || st.find({b, p}) != st.end()) continue;
        v[b].push_back(p);
        st.insert({b, p});
    }
    for (int i = 0; i < n; i++) {
        sort(v[i].rbegin(), v[i].rend());
    }
    vector<int> d(n, inf), cnt(n);
    d[u] = 0;
    while (true) {
        auto nd = d;
        int tt = 0;
        for (int j = 0; j < n; j++) {
            if (d[j] == inf || v[j].empty()) {
                tt++;
                continue;
            }
            bool ok = 1;
            for (int k : v[j]) {
                for (int t = j % k; t < n; t += k) {
                    if (nd[t] > d[j] + abs(t - j) / k) {
                        nd[t] = d[j] + abs(t - j) / k;
                        ok = 0;
                    }
                }
            }
            v[j].pop_back();
            if (ok) {
                ++cnt[j];
                ++tt;
            }
            if (cnt[j] > 10) {
                v[j].clear();
            }
        }
        d.swap(nd);
        if (tt == n) break;
    }
    cout << (d[w] == inf ? -1 : d[w]) << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...