Submission #654349

#TimeUsernameProblemLanguageResultExecution timeMemory
654349nguyentunglamJakarta Skyscrapers (APIO15_skyscraper)C++14
0 / 100
114 ms262144 KiB
#include<bits/stdc++.h>
#define forin(i, a, b) for(int i = a; i <= b; i++)
#define forde(i, a, b) for(int i = a; i >= b; i--)
#define forv(a, b) for(auto & a : b)
#define fi first
#define se second
#define ii pair<int, int>
#define endl "\n"
using namespace std;
const int N = 3e4 + 1, M = 2e7 + 1;
int b[N], p[N], cnt, f[M], d[N], c[N];
bool check[N];
pair<int, int> a[M];
vector<int> v[N], adj[M], g[N];
int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int n, m; cin >> n >> m;
    int start = clock();
    for(int i = 0; i < m; i++) {
        cin >> b[i] >> p[i];
        p[i] = min(p[i], n);
        g[p[i]].push_back(b[i]);
    }
    for(int val = 1;  val <= n; val++) {
        vector<int> tmp;
        for(int j : g[val]) {
            if (!c[j % val]) tmp.push_back(j % val), c[j % val] = 1;
        }
        for(int j : tmp) {
            for(int k = j; k < n; k += val) {
                d[k] = ++cnt;
                if (cnt > 1e7) return 0;
                a[cnt] = {k, val};
                if (k >= val) {
                    adj[cnt].push_back(d[k - val]);
                    adj[d[k - val]].push_back(cnt);
                }
            }
        }
        for(int j : g[val]) v[j].push_back(d[j]), c[j % val] = 0;
    }
    queue<int> q;
    for(int j : v[b[0]]) if (!f[j]) f[j] = 1, q.push(j);
    check[b[0]] = 1;
    while (!q.empty()) {
        int id = q.front();
        q.pop();
        int u = a[id].fi, w = a[id].se;
        if (u - w >= 0 && !check[u - w]) {
            check[u - w] = 1;
            for(int j : v[u - w]) f[j] = f[id] + 1, q.push(j);
        }
        if (u + w < n && !check[u + w]) {
            check[u + w] = 1;
            for(int j : v[u + w]) f[j] = f[id] + 1, q.push(j);
        }
        forv(v, adj[id]) if (!f[v]) f[v] = f[id] + 1, q.push(v);
    }
    int ans;
    for(int i = 1; i <= cnt; i++) if (a[i].fi == b[1] && a[i].se == p[1]) ans = i;
    cout << f[ans] - 1;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:9: warning: unused variable 'start' [-Wunused-variable]
   27 |     int start = clock();
      |         ^~~~~
skyscraper.cpp:19:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:20:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:23:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:24:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:70:18: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   70 |     cout << f[ans] - 1;
      |             ~~~~~^
#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...