#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][3];
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][yes];
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 |
79 ms |
95352 KB |
Output is correct |
2 |
Correct |
72 ms |
95364 KB |
Output is correct |
3 |
Correct |
69 ms |
95352 KB |
Output is correct |
4 |
Correct |
66 ms |
95352 KB |
Output is correct |
5 |
Correct |
66 ms |
95352 KB |
Output is correct |
6 |
Correct |
65 ms |
95480 KB |
Output is correct |
7 |
Correct |
67 ms |
95352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
95340 KB |
Output is correct |
2 |
Correct |
67 ms |
95296 KB |
Output is correct |
3 |
Correct |
66 ms |
95328 KB |
Output is correct |
4 |
Correct |
65 ms |
95352 KB |
Output is correct |
5 |
Correct |
66 ms |
95352 KB |
Output is correct |
6 |
Correct |
66 ms |
95352 KB |
Output is correct |
7 |
Correct |
65 ms |
95352 KB |
Output is correct |
8 |
Correct |
66 ms |
95480 KB |
Output is correct |
9 |
Correct |
66 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
68 ms |
96120 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
95356 KB |
Output is correct |
2 |
Correct |
66 ms |
95324 KB |
Output is correct |
3 |
Correct |
66 ms |
95352 KB |
Output is correct |
4 |
Correct |
66 ms |
95352 KB |
Output is correct |
5 |
Correct |
66 ms |
95452 KB |
Output is correct |
6 |
Correct |
66 ms |
95352 KB |
Output is correct |
7 |
Correct |
66 ms |
95404 KB |
Output is correct |
8 |
Correct |
66 ms |
95608 KB |
Output is correct |
9 |
Correct |
65 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
66 ms |
96120 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
95464 KB |
Output is correct |
2 |
Correct |
65 ms |
95352 KB |
Output is correct |
3 |
Correct |
67 ms |
95484 KB |
Output is correct |
4 |
Correct |
71 ms |
95352 KB |
Output is correct |
5 |
Correct |
71 ms |
95400 KB |
Output is correct |
6 |
Correct |
88 ms |
95352 KB |
Output is correct |
7 |
Correct |
75 ms |
95324 KB |
Output is correct |
8 |
Correct |
66 ms |
95472 KB |
Output is correct |
9 |
Correct |
65 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
66 ms |
96120 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
95352 KB |
Output is correct |
2 |
Correct |
66 ms |
95368 KB |
Output is correct |
3 |
Correct |
65 ms |
95352 KB |
Output is correct |
4 |
Correct |
65 ms |
95352 KB |
Output is correct |
5 |
Correct |
65 ms |
95352 KB |
Output is correct |
6 |
Correct |
66 ms |
95352 KB |
Output is correct |
7 |
Correct |
65 ms |
95352 KB |
Output is correct |
8 |
Correct |
81 ms |
95480 KB |
Output is correct |
9 |
Correct |
66 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
67 ms |
96124 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |