#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> All;
All.reserve(2 * n);
vector<array<int, 2>> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i][0] >> A[i][1];
All.push_back(A[i][0]);
All.push_back(A[i][1]);
}
sort(all(All));
map<int, int> new_;
int cnt = 0;
for (int i = 0; i < All.size(); i++) {
if (!i || All[i] != All[i - 1]) {
new_[All[i]] = cnt++;
}
}
int sz = cnt;
for (int i = 0; i < n; i++) {
A[i][0] = new_[A[i][0]], A[i][1] = new_[A[i][1]];
}
int ans = n + 1;
for (int k = 0; k < All.size(); k++) {
if (k && All[k] == All[k - 1]) {
continue;
}
int cur = new_[All[k]];
vector<array<int, 2>> B = A;
vector<vector<int>> Events(sz + 1);
for (int i = 0; i < n; i++) {
if (A[i][0] >= cur) {
A[i][0] -= cur;
}
else {
A[i][0] += sz - cur;
}
if (A[i][1] >= cur) {
A[i][1] -= cur;
}
else {
A[i][1] += sz - cur;
}
if (A[i][0] > A[i][1]) {
A[i][1] = sz;
}
Events[A[i][0]].push_back(i);
Events[A[i][1]].push_back(i);
}
vector<int> Prf(sz + 1, n + 1), Suf(sz + 1, n + 1);
vector<array<int, 2>> Stk;
Prf[0] = 0;
Stk.push_back({0, Prf[0]});
for (int i = 1; i <= sz; i++) {
int mn = i;
for (auto j : Events[i]) {
if (i != A[j][1]) {
continue;
}
mn = min(mn, A[j][0]);
}
if (mn != i) {
array<int, 2> kj = {mn, -1};
array<int, 2> res = *lower_bound(all(Stk), kj);
Prf[i] = min(Prf[i], res[1] + 1);
}
while (!Stk.empty() && Prf[i] <= Stk.back()[1]) {
Stk.pop_back();
}
Stk.push_back({i, Prf[i]});
}
Stk.clear();
Suf[sz] = 0;
Stk.push_back({sz - sz, Suf[sz]});
for (int i = sz; i >= 0; i--) {
int mx = i;
for (auto j : Events[i]) {
if (i != A[j][0]) {
continue;
}
mx = max(mx, A[j][1]);
}
if (mx != i) {
array<int, 2> kj = {sz - mx, -1};
array<int, 2> res = *lower_bound(all(Stk), kj);
Suf[i] = min(Suf[i], res[1] + 1);
}
while (!Stk.empty() && Suf[i] <= Stk.back()[1]) {
Stk.pop_back();
}
Stk.push_back({sz - i, Suf[i]});
}
for (int i = 1; i <= sz; i++) {
Suf[i] = min(Suf[i], Suf[i - 1]);
}
for (int i = sz - 1; i >= 0; i--) {
Prf[i] = min(Prf[i], Prf[i + 1]);
}
for (int i = 0; i <= sz; i++) {
ans = min(ans, Prf[i] + Suf[i]);
}
swap(A, B);
}
if (ans == n + 1) {
ans = -1;
}
cout << ans;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:31:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i = 0; i < All.size(); i++) {
| ~~^~~~~~~~~~~~
Main.cpp:43:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (int k = 0; k < All.size(); k++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
25 ms |
348 KB |
Output is correct |
26 |
Correct |
24 ms |
516 KB |
Output is correct |
27 |
Correct |
10 ms |
348 KB |
Output is correct |
28 |
Correct |
24 ms |
536 KB |
Output is correct |
29 |
Correct |
22 ms |
528 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
10 ms |
348 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
508 KB |
Output is correct |
35 |
Correct |
23 ms |
532 KB |
Output is correct |
36 |
Correct |
23 ms |
348 KB |
Output is correct |
37 |
Correct |
26 ms |
508 KB |
Output is correct |
38 |
Correct |
9 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
25 ms |
348 KB |
Output is correct |
26 |
Correct |
24 ms |
516 KB |
Output is correct |
27 |
Correct |
10 ms |
348 KB |
Output is correct |
28 |
Correct |
24 ms |
536 KB |
Output is correct |
29 |
Correct |
22 ms |
528 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
10 ms |
348 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
508 KB |
Output is correct |
35 |
Correct |
23 ms |
532 KB |
Output is correct |
36 |
Correct |
23 ms |
348 KB |
Output is correct |
37 |
Correct |
26 ms |
508 KB |
Output is correct |
38 |
Correct |
9 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
2070 ms |
1648 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
10 ms |
504 KB |
Output is correct |
10 |
Correct |
21 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
484 KB |
Output is correct |
13 |
Correct |
10 ms |
504 KB |
Output is correct |
14 |
Correct |
24 ms |
348 KB |
Output is correct |
15 |
Execution timed out |
2035 ms |
1644 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
23 ms |
536 KB |
Output is correct |
9 |
Correct |
10 ms |
500 KB |
Output is correct |
10 |
Correct |
22 ms |
524 KB |
Output is correct |
11 |
Correct |
10 ms |
348 KB |
Output is correct |
12 |
Execution timed out |
2093 ms |
1648 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
25 ms |
348 KB |
Output is correct |
26 |
Correct |
24 ms |
516 KB |
Output is correct |
27 |
Correct |
10 ms |
348 KB |
Output is correct |
28 |
Correct |
24 ms |
536 KB |
Output is correct |
29 |
Correct |
22 ms |
528 KB |
Output is correct |
30 |
Correct |
9 ms |
344 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
10 ms |
348 KB |
Output is correct |
33 |
Correct |
10 ms |
344 KB |
Output is correct |
34 |
Correct |
10 ms |
508 KB |
Output is correct |
35 |
Correct |
23 ms |
532 KB |
Output is correct |
36 |
Correct |
23 ms |
348 KB |
Output is correct |
37 |
Correct |
26 ms |
508 KB |
Output is correct |
38 |
Correct |
9 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
2070 ms |
1648 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |