include cstdio include iostream include iomanip include cstdio include

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <valarray>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long ll;
#define PATH "D:\\QtProjects\\olymp\\"
map <int, vector<int> > m[2];
const int N = 100001;
pair<int, int> pts[N];
vector<int> g[2][N];
int dist[2][N];
const int INF = 1e9;
/*inline void relax(int i, int v, int d){
if (dist[i][v] > d) dist[i][v] = d;
}*/
int main(){
//freopen(PATH"input.txt", "r", stdin);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
int n; cin >> n;
for (int i = 0; i < n; ++i){
int x, y;
cin >> x >> y;
pts[i] = make_pair(x, y);
m[0][x + y].push_back(i);
m[1][x - y].push_back(i);
}
for (int i = 0; i < 2; ++i){
for (auto& a: m[i]){
auto& vec = a.second;
sort(vec.begin(), vec.end(), [&](const int& l, const int& r){ return pts[l] < pts[r]; } );
for (int j = 0; j + 1 < vec.size(); ++j){
g[i][vec[j]].push_back(vec[j + 1]);
g[i][vec[j + 1]].push_back(vec[j]);
}
}
fill(dist[i], dist[i] + n, INF);
}
dist[0][0] = dist[1][0] = 0;
deque<pair<int, int> > q;
q.emplace_back(0, 0);
q.emplace_back(1, 0);
while (!q.empty()) {
auto v = q.front();
q.pop_front();
if (dist[v.first ^ 1][v.second] > dist[v.first][v.second] + 1) {
dist[v.first ^ 1][v.second] = dist[v.first][v.second] + 1;
q.emplace_back(v.first ^ 1, v.second);
}
int dv = dist[v.first][v.second];
for (int u : g[v.first][v.second]){
if (dist[v.first][u] > dv){
dist[v.first][u] = dv;
q.emplace_front(v.first, u);
}
}
}
int a = min(dist[1][1], dist[0][1]);
cout << ((a == INF)? -1 : a) << endl;
return 0;
}
/*bool doors[100001];
int main(){
freopen(PATH"input.txt", "r", stdin);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
int L, N, M, T;
char c;
cin >> L >> N >> M >> c >> T;
int curv = (c =='L')? -1 : +1;
typedef pair<int, int> TYP;
priority_queue<TYP, vector<TYP>, greater<TYP> > q;
doors[0] = doors[L] = true;
for (int i = 0; i < N; ++i){
int a; cin >> a;
int state; cin >> state;
doors[a] = state == 0;
int p; cin >> p;
for (int j = 0; j < p; ++j ){
int t; cin >> t;
q.emplace(t, a);
}
}
for (int t = 0; t < T; ++t){
while (!q.empty() && q.top().first == t){
int a = q.top().second; q.pop();
doors[a] = !doors[a];
}
if (doors[M]) curv = -curv;
M += curv;
}
cout << M << endl;
return 0;
}
*/