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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a) {
os << '{';
for(int i=0;i<sz(a);i++)
{
if(i>0&&i<sz(a)-1)
os << ", ";
os << a[i];
}
os << '}';
return os;
}
int n;
bool inside(int tr)
{
return tr>=0&&tr<n;
}
int main()
{
int m;
scanf("%i %i",&n,&m);
vector<set<int,greater<int> > > jumps(n);
map<pair<int,int>,bool> visited;
queue<pair<int,pair<int,int> > > q;
int b,p;
scanf("%i %i",&b,&p);
q.push({0,{b,p}});
scanf("%i %i",&b,&p);
int e=b;
for(int i=2;i<m;i++)
{
scanf("%i %i",&b,&p);
jumps[b].insert(p);
}
while(!q.empty())
{
int num=q.front().f;
int pos=q.front().s.f;
int d=q.front().s.s;
//printf("%i %i %i\n",num,pos,d);
if(pos==e)
{
printf("%i\n",num);
return 0;
}
q.pop();
for(auto p:jumps[pos])
{
if(inside(pos-p)&&!visited[{pos-p,p}])
{
visited[{pos-p,p}]=true;
q.push({num+1,{pos-p,p}});
}
if(inside(pos+p)&&!visited[{pos+p,p}])
{
visited[{pos+p,p}]=true;
q.push({num+1,{pos+p,p}});
}
}
if(inside(pos-d)&&!visited[{pos-d,d}])
{
visited[{pos-d,d}]=true;
q.push({num+1,{pos-d,d}});
}
if(inside(pos+d)&&!visited[{pos+d,d}])
{
visited[{pos+d,d}]=true;
q.push({num+1,{pos+d,d}});
}
}
printf("-1\n");
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&m);
~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&b,&p);
~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&b,&p);
~~~~~^~~~~~~~~~~~~~~
skyscraper.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&b,&p);
~~~~~^~~~~~~~~~~~~~~
# | 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... |