This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// created by Dinh Manh Hung
// onepunchac168
// THPT CHUYEN HA TINH
// HA TINH, VIET NAM
// Master go go
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops,no-stack-protector")
#pragma GCC target("sse4,avx2,fma")
#define task ""
#define ldb long double
#define pb push_back
#define fi first
#define se second
#define pc pop_back()
#define all(x) begin(x),end(x)
#define uniquev(v) v.resize(unique(all(v))-v.begin())
#define FOR(i,a,b) for (int i=a;i<=b;i++)
#define cntbit(v) __builtin_popcountll(v)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) ((1LL*a*b)/__gcd(a,b))
#define mask(x) (1LL<<(x))
#define readbit(x,i) ((x>>i)&1)
#define ins insert
typedef int ll;
typedef pair <ll,ll> ii;
typedef pair <ll,ii> iii;
typedef pair <ii,ii> iiii;
ll dx[]= {1,-1,0,0,1,-1,1,-1};
ll dy[]= {0,0,-1,1,1,-1,-1,1};
const ldb PI = acos (-1);
//const ll mod=978846151;
//const ll base=29;
const int maxn=1e6+5;
const int mod=1e9+7;
const char nl = '\n';
inline int ReadInt()
{
char co;
for (co = getchar(); co < '0' || co > '9'; co = getchar());
int xo = co - '0';
for (co = getchar(); co >= '0' && co <= '9'; co = getchar())
xo = xo * 10 + co - '0';
return xo;
}
void WriteInt(int xo)
{
if (xo > 9)
WriteInt(xo / 10);
putchar(xo % 10 + '0');
}
/* END OF TEMPLATE*/
// ================= Solution =================//
ll n,m;
ll b[50005];
ll p[50005];
ll dp[50005][255];
bool fee[50005][255];
ll BLOCK=50;
vector <ll> vt[50005];
ll id=0;
priority_queue< iii,vector <iii> ,greater <iii> > qu;
void optmushnpr()
{
cin>>n>>m;
for (int i=0;i<m;i++)
{
cin>>b[i]>>p[i];
if (p[i]<=BLOCK){
fee[b[i]][p[i]]=1;
}
else vt[b[i]].pb(p[i]);
}
id=b[1];
for (int i=0;i<n;i++)
{
for (int j=1;j<=BLOCK+1;j++)
{
dp[i][j]=1e9+5;
}
}
ll aa=b[0];
ll bb=(p[0]>BLOCK)? BLOCK+1 : p[0];
qu.push({0,{aa,bb}});
dp[aa][bb]=0;
ll rr=1e9+5;
while (!qu.empty())
{
ll u=qu.top().se.fi;
ll v=qu.top().se.se;
qu.pop();
if (u==id)
{
rr=dp[u][v];
break;
}
if (v==BLOCK+1)
{
for (auto cnt:vt[u])
{
ll id=u,dem=0;
while (id+cnt<n)
{
id+=cnt;
dem++;
if (dp[id][v]>dp[u][v]+dem)
{
dp[id][v]=dp[u][v]+dem;
qu.push({dp[id][v],{id,v}});
}
}
id=u,dem=0;
while (id-cnt>=0)
{
id-=cnt;
dem++;
if (dp[id][v]>dp[u][v]+dem)
{
dp[id][v]=dp[u][v]+dem;
qu.push({dp[id][v],{id,v}});
}
}
}
for (int i=1;i<=BLOCK;i++)
{
if (fee[u][i]==1)
{
if (u+i<n){
if (dp[u+i][i]>dp[u][v]+1)
{
dp[u+i][i]=dp[u][v]+1;
qu.push({dp[u+i][i],{u+i,i}});
}
}
if (u-i>=0)
{
if (dp[u-i][i]>dp[u][v]+1){
dp[u-i][i]=dp[u][v]+1;
qu.push({dp[u-i][i],{u-i,i}});
}
}
}
}
}
else
{
if (dp[u][BLOCK+1]>dp[u][v])
{
dp[u][BLOCK+1]=dp[u][v];
qu.push({dp[u][BLOCK+1],{u,BLOCK+1}});
}
if (u+v<n)
{
if (dp[u+v][v]>dp[u][v]+1)
{
dp[u+v][v]=dp[u][v]+1;
qu.push({dp[u+v][v],{u+v,v}});
}
}
if (u-v>=0)
{
if (dp[u-v][v]>dp[u][v]+1)
{
dp[u-v][v]=dp[u][v]+1;
qu.push({dp[u-v][v],{u-v,v}});
}
}
}
}
if (rr==1e9+5)
{
cout<<-1;
}
else cout<<rr;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);}
int t;
t=1;
//cin>>t;
while (t--){optmushnpr();}
}
// goodbye see ya
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:192:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
192 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:193:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
193 | freopen(task".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |