Submission #861629

#TimeUsernameProblemLanguageResultExecution timeMemory
861629anha3k25cvpJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
129 ms262144 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

int n, m;
vector <int> b, p;

namespace subtask123 {
    void main() {
        vector <vector <int>> g(n);
        for (int i = 1; i <= m; i ++)
            g[b[i]].push_back(i);
        vector <vector <II>> adj(m + 1);
        for (int i = 1; i <= m; i ++) {
            if (p[i] == 0)
                continue;
            int x = b[i], w = 0;
            while (x >= 0) {
                for (int j : g[x])
                    adj[i].push_back({j, w});
                x -= p[i];
                w ++;
            }
            x = b[i]; w = 0;
            while (x + p[i] < n) {
                x += p[i];
                w ++;
                for (int j : g[x])
                    adj[i].push_back({j, w});
            }
        }
        vector <int> f(m + 1, inf);
        priority_queue <II> q;
        q.push({-(f[1] = 0), 1});
        while (!q.empty()) {
            int du = -q.top().st, u = q.top().nd;
            q.pop();
            if (f[u] < du)
                continue;
            for (auto z : adj[u]) {
                int v = z.st, w = z.nd;
                if (du + w < f[v])
                    q.push({-(f[v] = du + w), v});
            }
        }
        if (f[2] == inf)
            f[2] = -1;
        cout << f[2];
    }
}

namespace subtask4 {
    void main() {
        vector <vector <int>> g(n), f(n, vector <int> (30001, inf));
        for (int i = 1; i <= m; i ++)
            g[b[i]].push_back(p[i]);
        vector <queue <II>> q(30001);
        q[0].push({b[1], p[1]});
        f[b[1]][p[1]] = 0;
        for (int val = 0; val <= 30000; val ++)
            while (!q[val].empty()) {
                int u = q[val].front().st, w = q[val].front().nd;
                q[val].pop();
                for (int w_ : g[u])
                    if (f[u][w] < f[u][w_]) {
                        q[val].push({u, w_});
                        f[u][w_] = val;
                    }
                for (int i = -1; i <= 1; i ++)
                    if (i != 0) {
                        int v = u + w * i;
                        if (v >= 0 && v < n && f[u][w] + 1 < f[v][w]) {
                            q[val + 1].push({v, w});
                            f[v][w] = val + 1;
                        }
                    }
            }
        int ans = inf;
        for (int i = 1; i <= 30000; i ++)
            ans = min(ans, f[b[2]][i]);
        if (ans == inf)
            ans = -1;
        cout << ans;
    }
}

int main() {
#define TASKNAME ""
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen (TASKNAME".inp", "r", stdin);
        freopen (TASKNAME".out", "w", stdout);
    }
    cin >> n >> m;
    b.assign(m + 1, 0);
    p.assign(m + 1, 0);
    for (int i = 1; i <= m; i ++)
        cin >> b[i] >> p[i];
    if (n <= 2000 && m <= 2000)
        subtask123 :: main();
    else
        subtask4 :: main();
    return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:101:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen (TASKNAME".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:102:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen (TASKNAME".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...