#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 |
71 ms |
95352 KB |
Output is correct |
2 |
Correct |
69 ms |
95348 KB |
Output is correct |
3 |
Correct |
78 ms |
95352 KB |
Output is correct |
4 |
Correct |
88 ms |
95352 KB |
Output is correct |
5 |
Correct |
69 ms |
95324 KB |
Output is correct |
6 |
Correct |
68 ms |
95352 KB |
Output is correct |
7 |
Correct |
68 ms |
95480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
95408 KB |
Output is correct |
2 |
Correct |
103 ms |
95324 KB |
Output is correct |
3 |
Correct |
77 ms |
95280 KB |
Output is correct |
4 |
Correct |
74 ms |
95352 KB |
Output is correct |
5 |
Correct |
68 ms |
95480 KB |
Output is correct |
6 |
Correct |
82 ms |
95352 KB |
Output is correct |
7 |
Correct |
79 ms |
95352 KB |
Output is correct |
8 |
Correct |
70 ms |
95480 KB |
Output is correct |
9 |
Correct |
68 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
67 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 |
65 ms |
95480 KB |
Output is correct |
3 |
Correct |
66 ms |
95356 KB |
Output is correct |
4 |
Correct |
71 ms |
95324 KB |
Output is correct |
5 |
Correct |
68 ms |
95352 KB |
Output is correct |
6 |
Correct |
70 ms |
95480 KB |
Output is correct |
7 |
Correct |
66 ms |
95352 KB |
Output is correct |
8 |
Correct |
66 ms |
95432 KB |
Output is correct |
9 |
Correct |
66 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
67 ms |
96120 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
95352 KB |
Output is correct |
2 |
Correct |
67 ms |
95396 KB |
Output is correct |
3 |
Correct |
73 ms |
95352 KB |
Output is correct |
4 |
Correct |
83 ms |
95320 KB |
Output is correct |
5 |
Correct |
68 ms |
95352 KB |
Output is correct |
6 |
Correct |
74 ms |
95352 KB |
Output is correct |
7 |
Correct |
68 ms |
95356 KB |
Output is correct |
8 |
Correct |
68 ms |
95480 KB |
Output is correct |
9 |
Correct |
91 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
72 ms |
96120 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
95364 KB |
Output is correct |
2 |
Correct |
67 ms |
95352 KB |
Output is correct |
3 |
Correct |
77 ms |
95352 KB |
Output is correct |
4 |
Correct |
81 ms |
95352 KB |
Output is correct |
5 |
Correct |
82 ms |
95352 KB |
Output is correct |
6 |
Correct |
70 ms |
95352 KB |
Output is correct |
7 |
Correct |
68 ms |
95352 KB |
Output is correct |
8 |
Correct |
65 ms |
95484 KB |
Output is correct |
9 |
Correct |
66 ms |
95480 KB |
Output is correct |
10 |
Incorrect |
66 ms |
96120 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |