#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=3e4+5;
const ll inf=2e18;
int n,m,b[maxN+1],p[maxN+1];
namespace sub2
{
int d[101][2001];
struct node
{
int u,power;
};
queue<node> q;
vector<int> v[101];
void solve()
{
memset(d,-1,sizeof(d));
q.push({b[0],p[0]});
d[b[0]][p[0]]=0;
ll dem=0;
while(!q.empty())
{
node tmp=q.front();
q.pop();
int u=tmp.u,power=tmp.power;
//cout<<u<<" "<<power<<'\n';
if(u==b[1])
{
cout<<d[u][power];
return;
}
if(u+power<m)
{
if(d[u+power][power]==-1)
{
d[u+power][power]=d[u][power]+1;
q.push({u+power,power});
}
}
if(u-power>=0)
{
if(d[u-power][power]==-1)
{
d[u-power][power]=d[u][power]+1;
q.push({u-power,power});
}
}
for(int i=0;i<n;i++)
{
if(u+power<m&&b[i]==u+power)
{
if(d[u+power][p[i]]==-1)
{
d[u+power][p[i]]=d[u][power]+1;
q.push({u+power,p[i]});
}
}
if(u-power>=0&&b[i]==u-power)
{
if(d[u-power][p[i]]==-1)
{
d[u-power][p[i]]=d[u][power]+1;
q.push({u-power,p[i]});
}
}
}
}
cout<<-1;
}
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>m>>n;
for(int i=0;i<n;i++)
{
cin>>b[i]>>p[i];
}
sub2::solve();
}
Compilation message
skyscraper.cpp: In function 'void sub2::solve()':
skyscraper.cpp:24:12: warning: unused variable 'dem' [-Wunused-variable]
24 | ll dem=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1240 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1176 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |