Submission #1197553

#TimeUsernameProblemLanguageResultExecution timeMemory
1197553TahirAliyevJakarta Skyscrapers (APIO15_skyscraper)C++20
100 / 100
772 ms195448 KiB
#include <bits/stdc++.h>
 
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #define int long long
#define pii pair<int, int>
#define ld long double
#define all(v) v.begin(), v.end()
using namespace std;
const int oo = 1e9 + 9;
const int MAX = 3e4 + 6;
int MOD = 998244353;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

void solve(){
    int n, m;
    cin >> n >> m;
    set<int> arr[n + 1];
    pii d0, d1;
    for(int i = 0; i < m; i++){
        int x, p; cin >> x >> p;
        if(i==0) d0 = {x, p};
        if(i==1) d1 = {x, p};
        arr[x].insert(p);
    }    
    gp_hash_table<int, int> dp[n + 1];
    deque<array<int, 3>> dq;
    dq.push_back({0, d0.first, d0.second});
    dp[d0.first][d0.second] = 0;
    while(dq.size()){
        auto [d, i, p] = dq.front();
        dq.pop_front();
        if(d != dp[i][p]) continue;
        for(int a : arr[i]){
            if(dp[i].find(a) == dp[i].end() || d < dp[i][a]){
                dp[i][a] = d;
                dq.push_front({d, i, a});
            }
        }
        arr[i].clear();
        for(int j = i - p; j <= i + p; j += 2 * p){
            if(!(0 <= j && j < n)) continue;
            if(dp[j].find(p) == dp[i].end() || d + 1 < dp[j][p]){
                dp[j][p] = d + 1;
                dq.push_back({d + 1, j, p});
            } 
        }
    }
    if(dp[d1.first].find(d1.second) == dp[d1.first].end()) cout << "-1\n";
    else cout << dp[d1.first][d1.second] << '\n';
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
}
#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...