Submission #284593

#TimeUsernameProblemLanguageResultExecution timeMemory
284593dooweyJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1091 ms33924 KiB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = 30010;
map<int, int> dist;
bool has[N];
vector<int> dj[N];

int getid(int x, int y){
    return x * N + y;
}

int main(){
    fastIO;
    int n, m;
    cin >> n >> m;
    deque<pii> ff;
    int xi, pi;
    int goalx = 0;
    int idi;
    for(int i = 0 ; i < m ; i ++ ){
        cin >> xi >> pi;
        if(i == 0){
            idi = getid(xi, pi);
            if(!dist.count(idi)){
                dist[idi] = 0;
                ff.push_back(mp(xi, pi));
            }
        }
        if(i == 1){
            goalx = xi;
        }
        dj[xi].push_back(pi);
    }
    int node, dd;
    int cur;
    int nidi;
    while(!ff.empty()){
        node = ff.front().fi;
        dd = ff.front().se;
        ff.pop_front();
        idi = getid(node, dd);
        cur = dist[idi];
        if(node == goalx){
            cout << cur << "\n";
            return 0;
        }
        if(!has[node]){
            for(auto x : dj[node]){
                nidi = getid(node, x);
                if(!dist.count(nidi)){
                    dist[nidi] = cur;
                    ff.push_front(mp(node, x));
                }
            }
            has[node]=true;
        }
        if(node - dd >= 0){
            nidi = getid(node - dd, dd);
            if(!dist.count(nidi)){
                dist[nidi] = cur + 1;
                ff.push_back(mp(node - dd, dd));
            }
        }
        if(node + dd < n){
            nidi = getid(node + dd, dd);
            if(!dist.count(nidi)){
                dist[nidi] = cur + 1;
                ff.push_back(mp(node + dd, dd));
            }
        }
    }
    cout << "-1\n";
    return 0;
}
#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...