이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 3e4;
const int INF = 1e9 + 69;
int n, m;
vector<int> a[N];
bool activated[N];
map<int, int> dis[N];
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
vector<pair<int, int>> dog(m);
for(int i = 0; i<m; ++i){
cin >> dog[i].first >> dog[i].second;
}
pair<int, int> dog0 = dog[0], dog1 = dog[1];
remove_dup(dog);
for(auto i: dog) a[i.first].push_back(i.second);
int o_cnt = 0;
int ans = INF;
deque<pair<int, int>> q;
q.push_back(dog0); dis[dog0.first][dog0.second] = 0;
while(q.size()){
pair<int, int> u = q.front(); q.pop_front();
int d = dis[u.first][u.second];
o_cnt++;
if (o_cnt > 1e7) exit(1);
if (u.first == dog1.first) {
minimize(ans, d);
break;
}
for(int i = -u.second; i <= u.second; i += u.second * 2) {
pair<int, int> v = {u.first + i, u.second};
if (v.first < 0 || v.first >= n) continue;
if (!dis[v.first].count(v.second)) {
dis[v.first][v.second] = d + 1;
q.push_back(v);
}
}
if (!activated[u.first]){
activated[u.first] = true;
for(int j: a[u.first]){
for(int i = -j; i <= j; i += j * 2) {
pair<int, int> v = {u.first + i, j};
if (v.first < 0 || v.first >= n) continue;
if (!dis[v.first].count(v.second)) {
dis[v.first][v.second] = d + 1;
q.push_back(v);
}
}
}
}
}
if (ans == INF) cout << -1 << "\n";
else cout << ans << "\n";
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... |