Submission #654334

#TimeUsernameProblemLanguageResultExecution timeMemory
654334nguyentunglamJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1095 ms56524 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 + 10, M = 1e6;
int b[N], p[N], cnt, f[M];
bool check[N];
pair<int, int> a[M];
vector<int> v[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();
    map<pair<int, int>, int> d;
    for(int i = 0; i < m; i++) {
        cin >> b[i] >> p[i];
        v[b[i]].push_back(p[i]);
        if (!d[{b[i], p[i]}]) {
            for(int j = b[i] % p[i]; j < n; j += p[i]) {
                d[{j, p[i]}] = ++cnt;
                a[cnt] = {j, p[i]};
            }
        }
    }
    if (cnt > M - 10) return 0;
//    cout << cnt << endl;
//    for(int i = 1; i <= cnt; i++) cout << a[i].fi << " " << a[i].se << endl;
    queue<int> q;
//    cout << cnt << endl;
    for(int j : v[b[0]]) {
        int id = d[{b[0], j}];
        if (!f[id]) f[id] = 1, q.push(id);
    }
    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]) {
                int stt = d[{u - w, j}];
                f[stt] = f[id] + 1, q.push(stt);
            }
        }
        if (u + w < n && !check[u + w]) {
            check[u + w] = 1;
            for(int j : v[u + w]) {
                int stt = d[{u + w, j}];
                f[stt] = f[id] + 1, q.push(stt);
            }
        }
        if (u - w >= 0) {
            int stt = d[{u - w, w}];
            if (!f[stt]) f[stt] = f[id] + 1, q.push(stt);
        }
        if (u + w < n) {
            int stt = d[{u + w, w}];
            if (!f[stt]) f[stt] = f[id] + 1, q.push(stt);
        }
    }
    cout << f[d[{b[1], p[1]}]] - 1;
    int finish = clock();
    cerr << 1.0 * (finish - start) / CLOCKS_PER_SEC;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
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);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...