#include <bits/stdc++.h>
using namespace std;
/*
John Watson
https://codeforces.com/profile/quangminh98
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const int maxn = 5e5 + 9;
const int segsize = 8 * maxn;
const ll oo = 1e18;
int n, t;
ll arr[maxn];
class SegmentTree
{
private:
struct Node
{
ll mn, mx;
Node(ll mn = 1e18, ll mx = -1e18) : mn(mn), mx(mx) {}
Node operator + (const Node& other)
{
Node res;
res.mn = min(mn, other.mn);
res.mx = max(mx, other.mx);
return res;
}
};
vector<Node> st;
int n;
void build(int head, int l, int r, ll *arr, int t)
{
if (l == r)
{
int idx = (l % n == 0 ? n : l % n);
int can = (l > n);
st[head] = Node(arr[idx] + can * t, arr[idx] + can * t);
return;
}
int mid = l + r >> 1;
build(2 * head, l, mid, arr, t);
build(2 * head + 1, mid + 1, r, arr, t);
st[head] = st[2 * head] + st[2 * head + 1];
}
Node query(int head, int l, int r, int u, int v)
{
if (l > v || r < u) return Node();
if (u <= l && r <= v) return st[head];
int mid = l + r >> 1;
return query(2 * head, l, mid, u, v) + query(2 * head + 1, mid + 1, r, u, v);
}
public:
SegmentTree(int n, ll *arr, int t) : n(n)
{
st.resize(segsize);
build(1, 1, 2 * n, arr, t);
}
pair<ll, ll> query(int u, int v) // min, max
{
Node ans = query(1, 1, 2 * n, u, v);
return {ans.mn, ans.mx};
}
};
signed main()
{
if (fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
faster();
cin >> n >> t;
for (int i = 1; i <= n; i++)
{
cin >> arr[i];
arr[i] %= t;
}
sort(arr + 1, arr + n + 1);
SegmentTree seg(n, arr, t);
ll res = oo;
for (int i = 1; i <= n + 1; i++)
{
pair<ll, ll> val = seg.query(i, i + n - 1);
ll mid = (val.first + val.second) / 2;
res = min(res, max(mid - val.first, val.second - mid));
}
cout << res << '\n';
return 0;
}
Compilation message
Main.cpp: In member function 'void SegmentTree::build(int, int, int, long long int*, int)':
Main.cpp:52:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'SegmentTree::Node SegmentTree::query(int, int, int, int, int)':
Main.cpp:64:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int main()':
Main.cpp:85:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:86:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
63056 KB |
Output is correct |
2 |
Correct |
22 ms |
63020 KB |
Output is correct |
3 |
Correct |
22 ms |
62852 KB |
Output is correct |
4 |
Correct |
21 ms |
63040 KB |
Output is correct |
5 |
Correct |
23 ms |
62880 KB |
Output is correct |
6 |
Correct |
21 ms |
63068 KB |
Output is correct |
7 |
Correct |
23 ms |
62960 KB |
Output is correct |
8 |
Correct |
22 ms |
63064 KB |
Output is correct |
9 |
Correct |
22 ms |
63276 KB |
Output is correct |
10 |
Correct |
22 ms |
63068 KB |
Output is correct |
11 |
Correct |
22 ms |
63068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
63068 KB |
Output is correct |
2 |
Correct |
22 ms |
63056 KB |
Output is correct |
3 |
Correct |
25 ms |
63068 KB |
Output is correct |
4 |
Correct |
24 ms |
63064 KB |
Output is correct |
5 |
Correct |
26 ms |
63064 KB |
Output is correct |
6 |
Correct |
21 ms |
63068 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
23 ms |
63068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
63068 KB |
Output is correct |
2 |
Correct |
22 ms |
63056 KB |
Output is correct |
3 |
Correct |
25 ms |
63068 KB |
Output is correct |
4 |
Correct |
24 ms |
63064 KB |
Output is correct |
5 |
Correct |
26 ms |
63064 KB |
Output is correct |
6 |
Correct |
21 ms |
63068 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
23 ms |
63068 KB |
Output is correct |
9 |
Correct |
24 ms |
63068 KB |
Output is correct |
10 |
Correct |
24 ms |
63132 KB |
Output is correct |
11 |
Correct |
24 ms |
62840 KB |
Output is correct |
12 |
Correct |
26 ms |
63060 KB |
Output is correct |
13 |
Correct |
24 ms |
63068 KB |
Output is correct |
14 |
Correct |
28 ms |
63068 KB |
Output is correct |
15 |
Correct |
23 ms |
63060 KB |
Output is correct |
16 |
Correct |
22 ms |
63068 KB |
Output is correct |
17 |
Correct |
24 ms |
63040 KB |
Output is correct |
18 |
Correct |
23 ms |
63060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
63068 KB |
Output is correct |
2 |
Correct |
22 ms |
63056 KB |
Output is correct |
3 |
Correct |
25 ms |
63068 KB |
Output is correct |
4 |
Correct |
24 ms |
63064 KB |
Output is correct |
5 |
Correct |
26 ms |
63064 KB |
Output is correct |
6 |
Correct |
21 ms |
63068 KB |
Output is correct |
7 |
Correct |
24 ms |
62964 KB |
Output is correct |
8 |
Correct |
23 ms |
63068 KB |
Output is correct |
9 |
Correct |
24 ms |
63068 KB |
Output is correct |
10 |
Correct |
24 ms |
63132 KB |
Output is correct |
11 |
Correct |
24 ms |
62840 KB |
Output is correct |
12 |
Correct |
26 ms |
63060 KB |
Output is correct |
13 |
Correct |
24 ms |
63068 KB |
Output is correct |
14 |
Correct |
28 ms |
63068 KB |
Output is correct |
15 |
Correct |
23 ms |
63060 KB |
Output is correct |
16 |
Correct |
22 ms |
63068 KB |
Output is correct |
17 |
Correct |
24 ms |
63040 KB |
Output is correct |
18 |
Correct |
23 ms |
63060 KB |
Output is correct |
19 |
Correct |
29 ms |
63028 KB |
Output is correct |
20 |
Correct |
27 ms |
62904 KB |
Output is correct |
21 |
Correct |
33 ms |
63064 KB |
Output is correct |
22 |
Correct |
25 ms |
63060 KB |
Output is correct |
23 |
Correct |
23 ms |
63068 KB |
Output is correct |
24 |
Correct |
24 ms |
63092 KB |
Output is correct |
25 |
Correct |
24 ms |
63068 KB |
Output is correct |
26 |
Correct |
21 ms |
63096 KB |
Output is correct |
27 |
Correct |
23 ms |
63060 KB |
Output is correct |
28 |
Correct |
23 ms |
63064 KB |
Output is correct |
29 |
Correct |
26 ms |
63056 KB |
Output is correct |
30 |
Correct |
27 ms |
62976 KB |
Output is correct |
31 |
Correct |
30 ms |
62996 KB |
Output is correct |
32 |
Correct |
24 ms |
63064 KB |
Output is correct |
33 |
Correct |
26 ms |
63064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
63056 KB |
Output is correct |
2 |
Correct |
22 ms |
63020 KB |
Output is correct |
3 |
Correct |
22 ms |
62852 KB |
Output is correct |
4 |
Correct |
21 ms |
63040 KB |
Output is correct |
5 |
Correct |
23 ms |
62880 KB |
Output is correct |
6 |
Correct |
21 ms |
63068 KB |
Output is correct |
7 |
Correct |
23 ms |
62960 KB |
Output is correct |
8 |
Correct |
22 ms |
63064 KB |
Output is correct |
9 |
Correct |
22 ms |
63276 KB |
Output is correct |
10 |
Correct |
22 ms |
63068 KB |
Output is correct |
11 |
Correct |
22 ms |
63068 KB |
Output is correct |
12 |
Correct |
21 ms |
63068 KB |
Output is correct |
13 |
Correct |
22 ms |
63056 KB |
Output is correct |
14 |
Correct |
25 ms |
63068 KB |
Output is correct |
15 |
Correct |
24 ms |
63064 KB |
Output is correct |
16 |
Correct |
26 ms |
63064 KB |
Output is correct |
17 |
Correct |
21 ms |
63068 KB |
Output is correct |
18 |
Correct |
24 ms |
62964 KB |
Output is correct |
19 |
Correct |
23 ms |
63068 KB |
Output is correct |
20 |
Correct |
24 ms |
63068 KB |
Output is correct |
21 |
Correct |
24 ms |
63132 KB |
Output is correct |
22 |
Correct |
24 ms |
62840 KB |
Output is correct |
23 |
Correct |
26 ms |
63060 KB |
Output is correct |
24 |
Correct |
24 ms |
63068 KB |
Output is correct |
25 |
Correct |
28 ms |
63068 KB |
Output is correct |
26 |
Correct |
23 ms |
63060 KB |
Output is correct |
27 |
Correct |
22 ms |
63068 KB |
Output is correct |
28 |
Correct |
24 ms |
63040 KB |
Output is correct |
29 |
Correct |
23 ms |
63060 KB |
Output is correct |
30 |
Correct |
29 ms |
63028 KB |
Output is correct |
31 |
Correct |
27 ms |
62904 KB |
Output is correct |
32 |
Correct |
33 ms |
63064 KB |
Output is correct |
33 |
Correct |
25 ms |
63060 KB |
Output is correct |
34 |
Correct |
23 ms |
63068 KB |
Output is correct |
35 |
Correct |
24 ms |
63092 KB |
Output is correct |
36 |
Correct |
24 ms |
63068 KB |
Output is correct |
37 |
Correct |
21 ms |
63096 KB |
Output is correct |
38 |
Correct |
23 ms |
63060 KB |
Output is correct |
39 |
Correct |
23 ms |
63064 KB |
Output is correct |
40 |
Correct |
26 ms |
63056 KB |
Output is correct |
41 |
Correct |
27 ms |
62976 KB |
Output is correct |
42 |
Correct |
30 ms |
62996 KB |
Output is correct |
43 |
Correct |
24 ms |
63064 KB |
Output is correct |
44 |
Correct |
26 ms |
63064 KB |
Output is correct |
45 |
Correct |
175 ms |
71624 KB |
Output is correct |
46 |
Correct |
180 ms |
71764 KB |
Output is correct |
47 |
Correct |
175 ms |
71760 KB |
Output is correct |
48 |
Correct |
206 ms |
71776 KB |
Output is correct |
49 |
Correct |
190 ms |
71796 KB |
Output is correct |
50 |
Correct |
173 ms |
71760 KB |
Output is correct |
51 |
Correct |
195 ms |
71764 KB |
Output is correct |
52 |
Correct |
187 ms |
71768 KB |
Output is correct |
53 |
Correct |
184 ms |
71760 KB |
Output is correct |