이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
map<int, bool> vis;
int n, m, goal = -1, dis;
bool vs[maxn];
vector<int> vec[maxn];
vector<pii> q, qu;
void add(int v){
if(0 > v || v >= m || vs[v]) return;
if(v == goal){
cout << dis;
exit(0);
}
for(int x : vec[v]){
vis[(x << 15) | v] = 1;
q.pb(Mp(v, x));
}
return;
}
int main(){
fast_io;
cin >> m >> n;
for(int i = 0; i < n; i++){
int v, x;
if(i == 1) goal = i;
cin >> v >> x;
vec[v].pb(x);
}
add(0);
while(!q.empty()){
dis++;
qu = q;
q.clear();
for(pii i : qu){
int v = i.F, x = i.S;
add(v - x);
add(v + x);
if(v + x < m && !vis[(x << 15) | (v + x)]){
vis[(x << 15) | (v + x)] = 1;
q.pb(Mp(v + x, x));
}
if(0 < v - x && !vis[(x << 15) | (v - x)]){
vis[(x << 15) | (v - x)] = 1;
q.pb(Mp(v - x, x));
}
}
}
cout << -1;
return 0;
}
# | 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... |