This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <complex>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define ld long long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 1000000007ll
#define INF 1e18
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast \
ios_base::sync_with_stdio (false); \
cin.tie (NULL); \
cout.tie (NULL)
using namespace std;
signed main()
{
fast;
ll n,m;
cin>>n>>m;
map <pair<ll,ll>,bool> mp;
ll loc[m+1], p[m+1];
set <ll> l[n];
set <ll> lt;
rep(i,1,m)
{
cin>>loc[i]>>p[i];
if(p[i]<n)
{
mp[{p[i],loc[i]%p[i]}]=true;
l[p[i]].insert(loc[i]);
lt.insert(loc[i]);
}
}
vector <pair<ll,ll>> adj[n];
for(auto [pr,b]:mp)
{
ll last=-1;
for(ll j=pr.ss;j<n;j+=pr.ff)
{
if(l[pr.ff].count(j))
{
if(last==-1)
last=j;
else
{
adj[last].pb({j,(j-last)/pr.ff});
adj[j].pb({last,(j-last)/pr.ff});
last=j;
}
}
}
last=-1;
ll f;
for(ll j=pr.ss;j<n;j+=pr.ff)
{
if(l[pr.ff].count(j))
{
last=j;
}
else if(lt.count(j) && last!=-1)
{
adj[last].pb({j,(j-last)/pr.ff});
}
f=j;
}
last=-1;
for(ll j=f;j>=0;j-=pr.ff)
{
if(l[pr.ff].count(j))
{
last=j;
}
else if(lt.count(j) && last!=-1)
{
adj[last].pb({j,(last-j)/pr.ff});
}
}
}
// rep(i,0,n-1)
// {
// cout<<i<<" : ";
// for(auto [a,b]:adj[i])
// cout<<a<<","<<b<<" ";
// cout<<endl;
// }
vector <ll> d(n,INF);
set <pair<ll,ll>> s;
s.insert({0,loc[1]});
d[loc[1]]=0;
while(!s.empty())
{
auto [dist,v]=(*s.begin());
s.erase({dist,v});
for(auto [u,e]:adj[v])
if(d[u]>e+d[v])
{
s.erase({d[u],u});
d[u]=e+d[v];
s.insert({d[u],u});
}
}
cout<<(d[loc[2]]==INF?-1:d[loc[2]])<<endl;
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:98:23: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
98 | for(ll j=f;j>=0;j-=pr.ff)
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |