답안 #754602

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
754602 2023-06-08T06:51:16 Z boris_mihov 송신탑 (IOI22_towers) C++17
44 / 100
4000 ms 10656 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXLOG = 20 + 5;
const int MAXN = 100000 + 10;
const int INF  = 1e9;

int n;
struct Sparse
{
    int sparse[MAXLOG][MAXN];
    int lg[MAXN];

    void build(int _vals[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            sparse[0][i] = _vals[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
            {
                sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
            }
        }
    
        for (int i = 1 ; i <= n ; ++i)
        {
            lg[i] = lg[i - 1];
            if ((1 << lg[i] + 1) < i)
            {
                lg[i]++;
            }
        }
    }

    int findMAX(int l, int r)
    {
        int log = lg[r - l + 1];
        return std::max(sparse[log][l], sparse[log][r - (1 << log) + 1]);
    }
};

int a[MAXN];
int b[MAXN];
int c[MAXN];
int d[MAXN];
int h[MAXN];
Sparse sparse;
std::stack <int> st;
std::vector <int> v;
int isClimb = -1;

void init(int N, std::vector <int> H) 
{
    n = N;
    for (int i = 1 ; i <= n ; ++i)
    {
        h[i] = H[i - 1];
    }

    sparse.build(h);
    st.push(0);

    for (int i = 1 ; i <= n ; ++i)
    {
        while (h[st.top()] > h[i])
        {
            st.pop();
        }

        a[i] = st.top();
        st.push(i);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        if ((i == 1 || h[i - 1] < h[i]) && (i == n || h[i] > h[i + 1]))
        {
            isClimb = i;
            break;
        }
    }

    for (int i = 1 ; i < isClimb ; ++i)
    {
        if (h[i] > h[i + 1])
        {
            isClimb = -1;
            break;
        }
    }

    if (isClimb != -1)
    {
        for (int i = isClimb ; i < n ; ++i)
        {
            if (h[i] < h[i + 1])
            {
                isClimb = -1;
                break;
            }
        }
    }

    while (!st.empty())
    {
        st.pop();
    }

    st.push(n + 1);
    for (int i = n ; i >= 1 ; --i)
    {
        while (h[st.top()] > h[i])
        {
            st.pop();
        }

        c[i] = st.top();
        st.push(i);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        if (a[i] == i - 1)
        {
            b[i] = 0;
        } else
        {
            b[i] = sparse.findMAX(a[i] + 1, i - 1) - h[i];
        }

        if (c[i] == i + 1)
        {
            d[i] = 0;
        } else
        {
            d[i] = sparse.findMAX(i + 1, c[i] - 1) - h[i];
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        int curr = INF; 
        if (a[i] > 0) curr = std::min(curr, b[i]);
        if (c[i] < n + 1) curr = std::min(curr, d[i]);
        v.push_back(curr);
    }

    std::sort(v.begin(), v.end());
}

bool entered;
int max_towers(int L, int R, int D) 
{
    L++; R++;
    if (isClimb != -1)
    {
        if (isClimb < L || R < isClimb)
        {
            return 1;
        }

        if (h[L] + D <= h[isClimb] && h[R] + D <= h[isClimb])
        {
            return 2;
        }

        return 1;
    }

    if (L == R)
    {
        return 1;
    }

    if (L == 1 && R == n)
    {
        int l = -1, r = n, mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (v[mid] < D) l = mid;
            else r = mid;
        }

        return n - r;
    }

    int ans = 0;
    for (int i = L ; i <= R ; ++i)
    {
        if ((a[i] < L || b[i] >= D) && (c[i] > R || d[i] >= D))
        {
            ans++;
        }
    }

    return ans;
}

Compilation message

towers.cpp: In member function 'void Sparse::build(int*)':
towers.cpp:30:93: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   30 |                 sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
      |                                                                                         ~~~~^~~
towers.cpp:37:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   37 |             if ((1 << lg[i] + 1) < i)
      |                       ~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 369 ms 6096 KB Output is correct
2 Correct 815 ms 10392 KB Output is correct
3 Correct 946 ms 10400 KB Output is correct
4 Correct 734 ms 10440 KB Output is correct
5 Correct 737 ms 10564 KB Output is correct
6 Correct 938 ms 10500 KB Output is correct
7 Correct 814 ms 10540 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 1 ms 432 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 1 ms 432 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 20 ms 6564 KB Output is correct
37 Correct 23 ms 10168 KB Output is correct
38 Correct 23 ms 10140 KB Output is correct
39 Correct 23 ms 10180 KB Output is correct
40 Correct 23 ms 10140 KB Output is correct
41 Correct 23 ms 10144 KB Output is correct
42 Correct 23 ms 10124 KB Output is correct
43 Correct 19 ms 10532 KB Output is correct
44 Correct 20 ms 10552 KB Output is correct
45 Correct 19 ms 10308 KB Output is correct
46 Correct 20 ms 10400 KB Output is correct
47 Correct 24 ms 10140 KB Output is correct
48 Correct 23 ms 10148 KB Output is correct
49 Correct 23 ms 10144 KB Output is correct
50 Correct 24 ms 10592 KB Output is correct
51 Correct 19 ms 10400 KB Output is correct
52 Correct 23 ms 10188 KB Output is correct
53 Correct 24 ms 10128 KB Output is correct
54 Correct 23 ms 10140 KB Output is correct
55 Correct 19 ms 10572 KB Output is correct
56 Correct 25 ms 10400 KB Output is correct
57 Correct 25 ms 9892 KB Output is correct
58 Correct 27 ms 10204 KB Output is correct
59 Correct 26 ms 10144 KB Output is correct
60 Correct 25 ms 10188 KB Output is correct
61 Correct 23 ms 10144 KB Output is correct
62 Correct 23 ms 10160 KB Output is correct
63 Correct 29 ms 10196 KB Output is correct
64 Correct 19 ms 10440 KB Output is correct
65 Correct 18 ms 10564 KB Output is correct
66 Correct 18 ms 10276 KB Output is correct
67 Correct 19 ms 10528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4040 ms 10140 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 292 ms 2640 KB Output is correct
2 Correct 891 ms 10160 KB Output is correct
3 Correct 803 ms 10188 KB Output is correct
4 Correct 846 ms 10180 KB Output is correct
5 Correct 900 ms 10180 KB Output is correct
6 Correct 884 ms 10188 KB Output is correct
7 Correct 566 ms 10144 KB Output is correct
8 Correct 675 ms 10472 KB Output is correct
9 Correct 895 ms 10656 KB Output is correct
10 Correct 600 ms 10436 KB Output is correct
11 Correct 829 ms 10428 KB Output is correct
12 Correct 23 ms 10144 KB Output is correct
13 Correct 22 ms 10284 KB Output is correct
14 Correct 22 ms 10180 KB Output is correct
15 Correct 18 ms 10552 KB Output is correct
16 Correct 18 ms 10404 KB Output is correct
17 Correct 29 ms 9892 KB Output is correct
18 Correct 23 ms 10132 KB Output is correct
19 Correct 24 ms 10156 KB Output is correct
20 Correct 24 ms 10180 KB Output is correct
21 Correct 22 ms 10164 KB Output is correct
22 Correct 32 ms 10140 KB Output is correct
23 Correct 23 ms 10184 KB Output is correct
24 Correct 19 ms 10532 KB Output is correct
25 Correct 19 ms 10548 KB Output is correct
26 Correct 20 ms 10380 KB Output is correct
27 Correct 18 ms 10532 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 1 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 1 ms 464 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 464 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 464 KB Output is correct
8 Correct 1 ms 464 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 1 ms 432 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 464 KB Output is correct
36 Correct 20 ms 6564 KB Output is correct
37 Correct 23 ms 10168 KB Output is correct
38 Correct 23 ms 10140 KB Output is correct
39 Correct 23 ms 10180 KB Output is correct
40 Correct 23 ms 10140 KB Output is correct
41 Correct 23 ms 10144 KB Output is correct
42 Correct 23 ms 10124 KB Output is correct
43 Correct 19 ms 10532 KB Output is correct
44 Correct 20 ms 10552 KB Output is correct
45 Correct 19 ms 10308 KB Output is correct
46 Correct 20 ms 10400 KB Output is correct
47 Correct 24 ms 10140 KB Output is correct
48 Correct 23 ms 10148 KB Output is correct
49 Correct 23 ms 10144 KB Output is correct
50 Correct 24 ms 10592 KB Output is correct
51 Correct 19 ms 10400 KB Output is correct
52 Correct 23 ms 10188 KB Output is correct
53 Correct 24 ms 10128 KB Output is correct
54 Correct 23 ms 10140 KB Output is correct
55 Correct 19 ms 10572 KB Output is correct
56 Correct 25 ms 10400 KB Output is correct
57 Correct 25 ms 9892 KB Output is correct
58 Correct 27 ms 10204 KB Output is correct
59 Correct 26 ms 10144 KB Output is correct
60 Correct 25 ms 10188 KB Output is correct
61 Correct 23 ms 10144 KB Output is correct
62 Correct 23 ms 10160 KB Output is correct
63 Correct 29 ms 10196 KB Output is correct
64 Correct 19 ms 10440 KB Output is correct
65 Correct 18 ms 10564 KB Output is correct
66 Correct 18 ms 10276 KB Output is correct
67 Correct 19 ms 10528 KB Output is correct
68 Execution timed out 4040 ms 10140 KB Time limit exceeded
69 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 369 ms 6096 KB Output is correct
2 Correct 815 ms 10392 KB Output is correct
3 Correct 946 ms 10400 KB Output is correct
4 Correct 734 ms 10440 KB Output is correct
5 Correct 737 ms 10564 KB Output is correct
6 Correct 938 ms 10500 KB Output is correct
7 Correct 814 ms 10540 KB Output is correct
8 Correct 0 ms 336 KB Output is correct
9 Correct 1 ms 464 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 1 ms 464 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 464 KB Output is correct
15 Correct 1 ms 464 KB Output is correct
16 Correct 1 ms 464 KB Output is correct
17 Correct 1 ms 464 KB Output is correct
18 Correct 1 ms 464 KB Output is correct
19 Correct 1 ms 464 KB Output is correct
20 Correct 1 ms 464 KB Output is correct
21 Correct 1 ms 464 KB Output is correct
22 Correct 0 ms 336 KB Output is correct
23 Correct 1 ms 432 KB Output is correct
24 Correct 1 ms 464 KB Output is correct
25 Correct 1 ms 464 KB Output is correct
26 Correct 1 ms 464 KB Output is correct
27 Correct 1 ms 464 KB Output is correct
28 Correct 1 ms 464 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 464 KB Output is correct
31 Correct 1 ms 464 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 464 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 464 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 464 KB Output is correct
39 Correct 1 ms 464 KB Output is correct
40 Correct 1 ms 464 KB Output is correct
41 Correct 1 ms 464 KB Output is correct
42 Correct 1 ms 464 KB Output is correct
43 Correct 1 ms 464 KB Output is correct
44 Correct 1 ms 464 KB Output is correct
45 Correct 1 ms 464 KB Output is correct
46 Correct 20 ms 6564 KB Output is correct
47 Correct 23 ms 10168 KB Output is correct
48 Correct 23 ms 10140 KB Output is correct
49 Correct 23 ms 10180 KB Output is correct
50 Correct 23 ms 10140 KB Output is correct
51 Correct 23 ms 10144 KB Output is correct
52 Correct 23 ms 10124 KB Output is correct
53 Correct 19 ms 10532 KB Output is correct
54 Correct 20 ms 10552 KB Output is correct
55 Correct 19 ms 10308 KB Output is correct
56 Correct 20 ms 10400 KB Output is correct
57 Correct 24 ms 10140 KB Output is correct
58 Correct 23 ms 10148 KB Output is correct
59 Correct 23 ms 10144 KB Output is correct
60 Correct 24 ms 10592 KB Output is correct
61 Correct 19 ms 10400 KB Output is correct
62 Correct 23 ms 10188 KB Output is correct
63 Correct 24 ms 10128 KB Output is correct
64 Correct 23 ms 10140 KB Output is correct
65 Correct 19 ms 10572 KB Output is correct
66 Correct 25 ms 10400 KB Output is correct
67 Correct 25 ms 9892 KB Output is correct
68 Correct 27 ms 10204 KB Output is correct
69 Correct 26 ms 10144 KB Output is correct
70 Correct 25 ms 10188 KB Output is correct
71 Correct 23 ms 10144 KB Output is correct
72 Correct 23 ms 10160 KB Output is correct
73 Correct 29 ms 10196 KB Output is correct
74 Correct 19 ms 10440 KB Output is correct
75 Correct 18 ms 10564 KB Output is correct
76 Correct 18 ms 10276 KB Output is correct
77 Correct 19 ms 10528 KB Output is correct
78 Execution timed out 4040 ms 10140 KB Time limit exceeded
79 Halted 0 ms 0 KB -