제출 #610585

#제출 시각아이디문제언어결과실행 시간메모리
610585nohaxjustsofloJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1089 ms45124 KiB
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> order_set;
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//uniform_int_distribution<int> gen; ///(min, max)
//int random() {return gen(mt_rand);}
const int mxN=1e5;
const int mod=998244353;
const int mxlogN=40;
const int mxK=26;
const int inf=1e9;
const int K=600;
struct st
{
    int x,p,d;
};
vector<int> who[mxN];
map<pair<int,int>, int> mp;
deque<st> q;
int n, m, a[mxN], b[mxN];
void check(int x, int p, int d, bool b)
{
    if(x<0||x>=n) return;
    if(!mp.count({x,p})||mp[{x,p}]>d)
    {
        mp[{x,p}]=d;
        if(b) q.push_back({x,p,d});
        else q.push_front({x,p,d});
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for(int i=0; i<m; i++) cin >> a[i] >> b[i];
    for(int i=1; i<m; i++) who[a[i]].push_back(i);
    q.push_back({a[0],b[0],0});
    mp[{a[0],b[0]}]=0;
    while(q.size())
    {
        auto par=q.front(); q.pop_front();
        int x=par.x,p=par.p,d=par.d;
        if(d!=mp[{x,p}]) continue;
        while(who[x].size())
        {
            int j=who[x].back(); who[x].pop_back();
            if(j==1)
            {
                cout << d << "\n";
                return 0;
            }
            int x2=a[j], p2=b[j], d2=d;
            check(x2,p2,d2,0);
        }
        check(x-p,p,d+1,1);
        check(x+p,p,d+1,1);
    }
    cout << "-1" << "\n";
    return 0;
}
/*
6 2 2
8 1 2 1 5 4
*/
#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...