제출 #311126

#제출 시각아이디문제언어결과실행 시간메모리
311126lohachoJakarta Skyscrapers (APIO15_skyscraper)C++14
36 / 100
511 ms262148 KiB
#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const int MOD = (int)1e9 + 7;
const int NS = (int)3e4 + 4;
int N, M;
int B[NS], P[NS];
vector<pair<int, int>> way[NS];
int far[NS], chk[NS];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> pos[NS];
vector<int> put[NS];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    for(int i = 0; i < M; ++i){
        cin >> B[i] >> P[i];
        if(i == 1 && B[0] == B[1]){
            cout << 0;
            return 0;
        }
        int whe = lower_bound(pos[P[i]].begin(), pos[P[i]].end(), B[i]) - pos[P[i]].begin();
        if(whe < (int)pos[P[i]].size() && pos[P[i]][whe] == B[i]){
            --i; --M;
        }
        pos[P[i]].push_back(B[i]);
        put[B[i]].push_back(i);
    }
    for(int i = 0; i < M; ++i){
        int whe = lower_bound(pos[P[i]].begin(), pos[P[i]].end(), B[i]) - pos[P[i]].begin();
        int now = B[i], mov = 0;
        while(now < N){
            while(whe < (int)pos[P[i]].size() && pos[P[i]][whe] < now){
                ++whe;
            }
            for(auto&in:put[now]){
                way[i].push_back({in, mov});
            }
            if(now > B[i] && whe < (int)pos[P[i]].size() && pos[P[i]][whe] == now){
                break;
            }
            now += P[i], ++mov;
        }
        whe = lower_bound(pos[P[i]].begin(), pos[P[i]].end(), B[i]) - pos[P[i]].begin();
        now = B[i] - P[i], mov = 1;
        while(now >= 0){
            while(whe >= 0 && pos[P[i]][whe] > now){
                --whe;
            }
            for(auto&in:put[now]){
                way[i].push_back({in, mov});
            }
            if(whe >= 0 && pos[P[i]][whe] == now){
                break;
            }
            now -= P[i], ++mov;
        }
    }
    pq.push({0, 0});
    for(int i = 0; i < M; ++i){
        far[i] = MOD;
    }
    far[0] = 0;
    while(!pq.empty()){
        while(!pq.empty() && chk[pq.top().second]){
            pq.pop();
        }
        if(pq.empty()){
            break;
        }
        auto top = pq.top(); pq.pop();
        chk[top.second] = 1;
        for(auto&nxt:way[top.second]){
            if(far[top.second] + nxt.second < far[nxt.first]){
                far[nxt.first] = far[top.second] + nxt.second;
                pq.push({far[nxt.first], nxt.first});
            }
        }
    }
    if(far[1] == MOD){
        cout << -1;
    }
    else{
        cout << far[1];
    }
    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...