#include <bits/stdc++.h>
using namespace std;
long long n,m;
long long a[2009];
long long p[2009];
long long done[2009][2009];
long long dp[2009][2009];
vector<long long>v[10009];
long long inf=1000000000000009ll;
long long bt(long long id,long long dog,long long yes){
if(yes==1)return inf;
done[id][dog]=1;
if(id<0 || id>n)return inf;
if(id==a[1])return 0;
long long &ret=dp[id][dog];
if(ret!=-1)return ret;
ret=inf;
if(id+p[dog]<=n && id-p[dog]>0)ret=min(bt(id+p[dog],dog,done[id+p[dog]][dog]),bt(id-p[dog],dog,done[id-p[dog]][dog]))+1;
else if(id+p[dog]<=n) ret=min(ret,bt(id+p[dog],dog,done[id+p[dog]][dog]))+1;
else if(id-p[dog]>0) ret=min(ret,bt(id-p[dog],dog,done[id-p[dog]][dog]))+1;
for(int i=0;i<v[id].size();i++){
long long D=v[id][i];
ret=min(ret,bt(id,D,done[id][D]));
}
return ret;
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>a[i]>>p[i];
v[a[i]].push_back(i);
}
long long x=bt(a[0],0,0);
if(x!=inf) cout<<x<<endl;
else cout<<-1<<endl;
}
Compilation message
skyscraper.cpp: In function 'long long int bt(long long int, long long int, long long int)':
skyscraper.cpp:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[id].size();i++){
~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
32248 KB |
Output is correct |
2 |
Correct |
24 ms |
32000 KB |
Output is correct |
3 |
Correct |
29 ms |
32120 KB |
Output is correct |
4 |
Correct |
24 ms |
32128 KB |
Output is correct |
5 |
Correct |
24 ms |
32128 KB |
Output is correct |
6 |
Correct |
23 ms |
32128 KB |
Output is correct |
7 |
Correct |
23 ms |
32128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
32256 KB |
Output is correct |
2 |
Correct |
24 ms |
32128 KB |
Output is correct |
3 |
Correct |
23 ms |
32120 KB |
Output is correct |
4 |
Correct |
23 ms |
32120 KB |
Output is correct |
5 |
Correct |
22 ms |
32256 KB |
Output is correct |
6 |
Correct |
24 ms |
32220 KB |
Output is correct |
7 |
Correct |
24 ms |
32256 KB |
Output is correct |
8 |
Correct |
23 ms |
32252 KB |
Output is correct |
9 |
Correct |
24 ms |
32384 KB |
Output is correct |
10 |
Incorrect |
25 ms |
33024 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
32256 KB |
Output is correct |
2 |
Correct |
24 ms |
32100 KB |
Output is correct |
3 |
Correct |
26 ms |
32120 KB |
Output is correct |
4 |
Correct |
24 ms |
32120 KB |
Output is correct |
5 |
Correct |
25 ms |
32256 KB |
Output is correct |
6 |
Correct |
24 ms |
32248 KB |
Output is correct |
7 |
Correct |
30 ms |
32256 KB |
Output is correct |
8 |
Correct |
25 ms |
32248 KB |
Output is correct |
9 |
Correct |
28 ms |
32376 KB |
Output is correct |
10 |
Incorrect |
27 ms |
32896 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
32152 KB |
Output is correct |
2 |
Correct |
24 ms |
32128 KB |
Output is correct |
3 |
Correct |
25 ms |
32120 KB |
Output is correct |
4 |
Correct |
25 ms |
32120 KB |
Output is correct |
5 |
Correct |
37 ms |
32120 KB |
Output is correct |
6 |
Correct |
24 ms |
32128 KB |
Output is correct |
7 |
Correct |
26 ms |
32248 KB |
Output is correct |
8 |
Correct |
25 ms |
32252 KB |
Output is correct |
9 |
Correct |
25 ms |
32248 KB |
Output is correct |
10 |
Incorrect |
27 ms |
32888 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
32120 KB |
Output is correct |
2 |
Correct |
24 ms |
32128 KB |
Output is correct |
3 |
Correct |
24 ms |
32120 KB |
Output is correct |
4 |
Correct |
24 ms |
32128 KB |
Output is correct |
5 |
Correct |
24 ms |
32256 KB |
Output is correct |
6 |
Correct |
24 ms |
32256 KB |
Output is correct |
7 |
Correct |
24 ms |
32128 KB |
Output is correct |
8 |
Correct |
25 ms |
32208 KB |
Output is correct |
9 |
Correct |
28 ms |
32248 KB |
Output is correct |
10 |
Incorrect |
28 ms |
32888 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |