#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n, m; cin >> n >> m;
vector<pair<int, int>> shifts;
int max_dist = 0;
int start_x = 0;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
if (x > y) y += m;
if (y-x > max_dist) {max_dist = y-x;start_x = x;}
shifts.push_back({x, y});
}
for (int i=0; i < n; i++) {
shifts[i] = {(shifts[i].first-start_x+2*m)%m, (shifts[i].second-start_x+2*m)%m};
if (shifts[i].first > shifts[i].second) shifts[i]={shifts[i].first, shifts[i].second+m};
}
sort(shifts.begin(), shifts.end());
if (n > 5000) {
int time = 0;
int furthest_time = 0;
int c = 0;
for (int i=0; i < n; i++) {
if (shifts[i].first <= time) furthest_time = max(furthest_time, shifts[i].second);
else {
c++;
time = furthest_time;
if (time >= m) {cout << c; return 0;}
if (shifts[i].first > time) {cout << -1; return 0;}
furthest_time = shifts[i].second;
}
}
c++;
time = furthest_time;
if (time >= m) {cout << c; return 0;}
else cout << -1;
}
else {
//brute force
int ans = 10000000;
for (int t = 0; t < n; t++) {
int tmp = shifts[1].first;
for (int i=0; i < n; i++) {
shifts[i] = {(shifts[i].first-tmp+2*m)%m, (shifts[i].second-tmp+2*m)%m};
if (shifts[i].first > shifts[i].second) shifts[i]={shifts[i].first, shifts[i].second+m};
}
sort(shifts.begin(), shifts.end());
int time = 0;
int furthest_time = 0;
int c = 0;
int exit = 0;
for (int i=0; i < n; i++) {
if (shifts[i].first <= time) furthest_time = max(furthest_time, shifts[i].second);
else {
c++;
time = furthest_time;
if (time >= m) {ans = min(ans, c); exit=1; break;}
if (shifts[i].first > time) {exit = 1; break;}
furthest_time = shifts[i].second;
}
}
if (exit == 0) {
c++;
time = furthest_time;
if (time >= m) {ans = min(ans, c);}}
}if (ans == 10000000) cout << -1; else cout << ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
3 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
444 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
37 |
Correct |
2 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
3 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
444 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
37 |
Correct |
2 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
336 KB |
Output is correct |
39 |
Correct |
372 ms |
564 KB |
Output is correct |
40 |
Correct |
404 ms |
664 KB |
Output is correct |
41 |
Correct |
451 ms |
592 KB |
Output is correct |
42 |
Correct |
369 ms |
592 KB |
Output is correct |
43 |
Correct |
418 ms |
592 KB |
Output is correct |
44 |
Correct |
367 ms |
592 KB |
Output is correct |
45 |
Correct |
366 ms |
592 KB |
Output is correct |
46 |
Correct |
366 ms |
592 KB |
Output is correct |
47 |
Correct |
376 ms |
592 KB |
Output is correct |
48 |
Correct |
348 ms |
772 KB |
Output is correct |
49 |
Correct |
352 ms |
600 KB |
Output is correct |
50 |
Correct |
364 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
504 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
3 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
425 ms |
760 KB |
Output is correct |
16 |
Correct |
375 ms |
592 KB |
Output is correct |
17 |
Correct |
398 ms |
592 KB |
Output is correct |
18 |
Correct |
381 ms |
592 KB |
Output is correct |
19 |
Correct |
341 ms |
760 KB |
Output is correct |
20 |
Correct |
42 ms |
6584 KB |
Output is correct |
21 |
Correct |
115 ms |
6564 KB |
Output is correct |
22 |
Correct |
112 ms |
6508 KB |
Output is correct |
23 |
Correct |
92 ms |
6704 KB |
Output is correct |
24 |
Correct |
116 ms |
6584 KB |
Output is correct |
25 |
Incorrect |
104 ms |
6584 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
3 ms |
336 KB |
Output is correct |
12 |
Correct |
428 ms |
772 KB |
Output is correct |
13 |
Correct |
373 ms |
592 KB |
Output is correct |
14 |
Correct |
358 ms |
772 KB |
Output is correct |
15 |
Correct |
41 ms |
6592 KB |
Output is correct |
16 |
Correct |
116 ms |
6584 KB |
Output is correct |
17 |
Correct |
109 ms |
6704 KB |
Output is correct |
18 |
Correct |
113 ms |
6584 KB |
Output is correct |
19 |
Correct |
109 ms |
6564 KB |
Output is correct |
20 |
Correct |
118 ms |
6584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
508 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
2 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
3 ms |
336 KB |
Output is correct |
30 |
Correct |
2 ms |
336 KB |
Output is correct |
31 |
Correct |
2 ms |
336 KB |
Output is correct |
32 |
Correct |
3 ms |
336 KB |
Output is correct |
33 |
Correct |
3 ms |
444 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
2 ms |
336 KB |
Output is correct |
36 |
Correct |
2 ms |
336 KB |
Output is correct |
37 |
Correct |
2 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
336 KB |
Output is correct |
39 |
Correct |
372 ms |
564 KB |
Output is correct |
40 |
Correct |
404 ms |
664 KB |
Output is correct |
41 |
Correct |
451 ms |
592 KB |
Output is correct |
42 |
Correct |
369 ms |
592 KB |
Output is correct |
43 |
Correct |
418 ms |
592 KB |
Output is correct |
44 |
Correct |
367 ms |
592 KB |
Output is correct |
45 |
Correct |
366 ms |
592 KB |
Output is correct |
46 |
Correct |
366 ms |
592 KB |
Output is correct |
47 |
Correct |
376 ms |
592 KB |
Output is correct |
48 |
Correct |
348 ms |
772 KB |
Output is correct |
49 |
Correct |
352 ms |
600 KB |
Output is correct |
50 |
Correct |
364 ms |
452 KB |
Output is correct |
51 |
Correct |
1 ms |
504 KB |
Output is correct |
52 |
Correct |
1 ms |
336 KB |
Output is correct |
53 |
Correct |
1 ms |
336 KB |
Output is correct |
54 |
Correct |
1 ms |
508 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
1 ms |
336 KB |
Output is correct |
57 |
Correct |
1 ms |
336 KB |
Output is correct |
58 |
Correct |
1 ms |
336 KB |
Output is correct |
59 |
Correct |
2 ms |
336 KB |
Output is correct |
60 |
Correct |
2 ms |
336 KB |
Output is correct |
61 |
Correct |
3 ms |
336 KB |
Output is correct |
62 |
Correct |
2 ms |
336 KB |
Output is correct |
63 |
Correct |
2 ms |
336 KB |
Output is correct |
64 |
Correct |
2 ms |
336 KB |
Output is correct |
65 |
Correct |
425 ms |
760 KB |
Output is correct |
66 |
Correct |
375 ms |
592 KB |
Output is correct |
67 |
Correct |
398 ms |
592 KB |
Output is correct |
68 |
Correct |
381 ms |
592 KB |
Output is correct |
69 |
Correct |
341 ms |
760 KB |
Output is correct |
70 |
Correct |
42 ms |
6584 KB |
Output is correct |
71 |
Correct |
115 ms |
6564 KB |
Output is correct |
72 |
Correct |
112 ms |
6508 KB |
Output is correct |
73 |
Correct |
92 ms |
6704 KB |
Output is correct |
74 |
Correct |
116 ms |
6584 KB |
Output is correct |
75 |
Incorrect |
104 ms |
6584 KB |
Output isn't correct |
76 |
Halted |
0 ms |
0 KB |
- |