#include <algorithm>
#include <cstdio>
using ll = long long;
struct ST_table
{
using func_T = int (*)(int, int);
int n, lg[100005], arr[25][100005];
func_T comp;
ST_table(int _n, func_T _comp) : n(_n), comp(_comp)
{
lg[0] = lg[1] = 0;
for (int i = 2; i <= n; i++)
lg[i] = lg[i >> 1] + 1;
for (int i = 0; i < n; i++)
arr[0][i] = i;
for (int i = 1; i <= lg[n]; i++)
{
for (int j = 0; j + (1 << i) <= n; j++)
arr[i][j] = comp(arr[i - 1][j], arr[i - 1][j + (1 << i - 1)]);
}
}
inline int operator ()(int l, int r)
{
int len = lg[r - l + 1];
return comp(arr[len][l], arr[len][r - (1 << len) + 1]);
}
};
int pos[100005], stk[100005], pre[100005], nxt[100005];
ll arr[100005];
inline int smaller(int x, int y) { return arr[x] < arr[y] ? x : y; }
inline int larger(int x, int y) { return arr[x] > arr[y] ? x : y; }
inline bool check(int n, int st, ll T)
{
for (int i = 0; i < n; i++)
arr[i] = pos[i] - T * i;
ST_table query_min(n, smaller), query_max(n, larger);
auto find_bigger = [&] (int *res)
{
int tp = 0;
stk[tp] = -1;
for (int i = 0; i < n; i++)
{
while (tp && arr[stk[tp]] <= arr[i])
tp--;
res[i] = stk[tp];
stk[++tp] = i;
}
std::reverse(arr, arr + n);
for (int i = 0; i < n; i++)
arr[i] *= -1;
};
find_bigger(pre);
find_bigger(nxt);
std::reverse(nxt, nxt + n);
for (int i = 0; i < n; i++)
nxt[i] = n - nxt[i] - 1;
int l = st, r = st;
while (l || r != n - 1)
{
auto try_l = [&] (int x)
{
if (x < 0 || arr[query_min(x, l - 1)] < arr[r])
return false;
l = x;
return true;
};
auto try_r = [&] (int y)
{
if (y >= n || arr[query_max(r + 1, y)] > arr[l])
return false;
r = y;
return true;
};
if (try_l(pre[l]) || try_r(nxt[r]))
continue;
if (!l)
return arr[query_max(r + 1, n - 1)] <= arr[l];
if (r == n - 1)
return arr[query_min(0, l - 1)] >= arr[r];
int x = query_min(0, l - 1), y = query_max(r + 1, n - 1);
if (arr[x] < arr[r] || arr[y] > arr[l])
return false;
int a = query_max(0, x), b = query_min(y, n - 1);
if (arr[a] >= arr[y])
l = a;
else if (arr[b] <= arr[x])
r = b;
else
return false;
}
return true;
}
int main()
{
// freopen("JOISC2017-D1T3.in", "r", stdin);
int n, st, T;
scanf("%d%d%d", &n, &st, &T);
for (int i = 0; i < n; i++)
scanf("%d", pos + i);
int l = 0, r = 1e9, res = -1;
while (l <= r)
{
int m = l + r >> 1;
if (check(n, st - 1, (ll)m * T * 2))
{
res = m;
r = m - 1;
}
else
l = m + 1;
}
printf("%d\n", res);
return 0;
}
Compilation message
sparklers.cpp: In constructor 'ST_table::ST_table(int, ST_table::func_T)':
sparklers.cpp:19:60: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
arr[i][j] = comp(arr[i - 1][j], arr[i - 1][j + (1 << i - 1)]);
~~^~~
sparklers.cpp: In function 'int main()':
sparklers.cpp:103:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
sparklers.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &st, &T);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sparklers.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", pos + i);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
6 ms |
512 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
5 ms |
512 KB |
Output is correct |
36 |
Correct |
6 ms |
512 KB |
Output is correct |
37 |
Correct |
4 ms |
512 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
512 KB |
Output is correct |
40 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
6 ms |
512 KB |
Output is correct |
30 |
Correct |
6 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
5 ms |
512 KB |
Output is correct |
36 |
Correct |
6 ms |
512 KB |
Output is correct |
37 |
Correct |
4 ms |
512 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
512 KB |
Output is correct |
40 |
Correct |
4 ms |
512 KB |
Output is correct |
41 |
Correct |
193 ms |
11052 KB |
Output is correct |
42 |
Correct |
9 ms |
888 KB |
Output is correct |
43 |
Correct |
46 ms |
3188 KB |
Output is correct |
44 |
Correct |
310 ms |
16824 KB |
Output is correct |
45 |
Correct |
330 ms |
16836 KB |
Output is correct |
46 |
Correct |
327 ms |
16800 KB |
Output is correct |
47 |
Correct |
360 ms |
16888 KB |
Output is correct |
48 |
Correct |
287 ms |
16760 KB |
Output is correct |
49 |
Correct |
335 ms |
16860 KB |
Output is correct |
50 |
Correct |
348 ms |
16888 KB |
Output is correct |
51 |
Correct |
306 ms |
17016 KB |
Output is correct |
52 |
Correct |
328 ms |
16764 KB |
Output is correct |
53 |
Correct |
349 ms |
16888 KB |
Output is correct |
54 |
Correct |
358 ms |
16888 KB |
Output is correct |
55 |
Correct |
339 ms |
16888 KB |
Output is correct |
56 |
Correct |
343 ms |
16760 KB |
Output is correct |
57 |
Correct |
306 ms |
16872 KB |
Output is correct |
58 |
Correct |
277 ms |
16808 KB |
Output is correct |
59 |
Correct |
262 ms |
16120 KB |
Output is correct |
60 |
Correct |
292 ms |
16888 KB |
Output is correct |
61 |
Correct |
301 ms |
16760 KB |
Output is correct |
62 |
Correct |
366 ms |
16760 KB |
Output is correct |
63 |
Correct |
316 ms |
16888 KB |
Output is correct |
64 |
Correct |
330 ms |
16888 KB |
Output is correct |
65 |
Correct |
294 ms |
16840 KB |
Output is correct |
66 |
Correct |
302 ms |
16760 KB |
Output is correct |
67 |
Correct |
281 ms |
16888 KB |
Output is correct |
68 |
Correct |
322 ms |
16888 KB |
Output is correct |
69 |
Correct |
294 ms |
16888 KB |
Output is correct |