#include <bits/stdc++.h>
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll long long
#define pll pair<ll,ll>
#define SF(x) scanf("%I64d",&x)
using namespace std;
const long long INF = 2e9;
long long MOD = 1e9+7;
ll n,m;
ll a[2222],b[2222];
ll dp[300005];
vector<ll> adj[30005];
ll pos;
stack<ll> s;
ll DP(ll x){
if(x<0||x>=m)R INF;
if(x==pos)R 0;
ll &ret=dp[x];
if(ret!=-1)R ret;
ret=INF;
ll d;
for(int i=0;i<adj[x].SI;i++){
d=adj[x][i];
for(int j=x+d,q=1;j<m;q++,j+=d){s.P(j);ret=min(ret,DP(j)+q);}
for(int j=x-d,q=1;j>=0;q++,j-=d){s.P(j);ret=min(ret,DP(j)+q);}
}
W(!s.empty()){
dp[s.top()]=-1;
s.pop();
}
for(int i=0;i<adj[x].SI;i++){
d=adj[x][i];
for(int j=x+d,q=1;j<m;q++,j+=d){ret=min(ret,DP(j)+q);}
for(int j=x-d,q=1;j>=0;q++,j-=d){ret=min(ret,DP(j)+q);}
}
R ret;
}
map<ll,bool> M[30005];
int main(){
MEM(dp,-1);
cin>>m>>n;
for(int i=0;i<n;i++){
cin>>a[i]>>b[i];
if(M[a[i]][b[i]])C;
M[a[i]][b[i]]=1;
adj[a[i]].pb(b[i]);
}
pos=a[1];
if(DP(0)==INF)cout<<-1;
else cout<<DP(a[0]);
}
Compilation message
skyscraper.cpp: In function 'long long int DP(long long int)':
skyscraper.cpp:31:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].SI;i++){
^
skyscraper.cpp:40:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].SI;i++){
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Memory limit exceeded |
129 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Memory limit exceeded |
103 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Memory limit exceeded |
119 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Memory limit exceeded |
103 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Memory limit exceeded |
139 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |