Submission #1014282

#TimeUsernameProblemLanguageResultExecution timeMemory
1014282ByeWorldJakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
375 ms5840 KiB
#include <bits/stdc++.h>
// #define int long long
#define fi first
#define se second
 
using namespace std;
 
const int inf = 1e9;
 
int b[30000], p[30000], dist[30000];
vector<int> v[30000];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
 
int32_t main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        
        int n, m;
        cin >> n >> m;
 
        for (int i = 0; i < m; i++) {
                cin >> b[i] >> p[i];
 
                v[b[i]].push_back(p[i]);
        }
 
        for (int i = 0; i < n; i++) {
                dist[i] = inf;
                sort(v[i].begin(), v[i].end());
                v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
        }
 
        int ans = -1;
        dist[b[0]] = 0;
        pq.push({dist[b[0]], b[0]});
        while (!pq.empty()) {
                auto [w, x] = pq.top();
                pq.pop();
 
                if (w > dist[x]) continue;
 
                if (x == b[1]) {
                        ans = w;
                        break;
                }
 
                for (auto k : v[x]) {
                  	int cnt = 1;
                        for (int i = x + k; i < n; i += k) {
                                if (w + cnt < dist[i]) {
                                        dist[i] = w + cnt;
                                        pq.push({dist[i], i});
                                }
                          cnt++;
                        }
 	cnt = 1;
                        for (int i = x - k; i >= 0; i -= k) {
                                if (w + cnt < dist[i]) {
                                        dist[i] = w + cnt;
                                        pq.push({dist[i], i});
                                }
                          cnt++;
                        }
                }
        }
 
        cout << ans << '\n';
        
        return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:38:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   38 |                 auto [w, x] = pq.top();
      |                      ^
#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...