답안 #978798

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978798 2024-05-09T17:21:46 Z LOLOLO K개의 묶음 (IZhO14_blocks) C++17
100 / 100
848 ms 19724 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 1e5 + 10;
ll f[N], a[N];
ll sp[N][20], n, k;
int lg[N];

void build(int id) {
    for (int i = 0; i <= n; i++) {
        sp[i][0] = f[i];
    }

    for (int j = 1; j < 20; j++) {
        for (int i = 0; i + (1 << j) <= n + 1; i++) {
            sp[i][j] = min(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
        }
    }
}

ll range(int l, int r, int id) {
    if (l > r)
        return 1e16;

    int t = lg[r - l + 1];
    return min(sp[l][t], sp[r - (1 << t) + 1][t]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    for (int i = 2; i < N; i++)
        lg[i] = lg[i / 2] + 1;

    mem(f, 0x3f);
    cin >> n >> k;

    f[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int j = 1; j <= k; j++) {
        build(j - 1);
        stack <int> st;
        vector <ll> pr;

        for (int i = 0; i <= n; i++) {
            while (sz(st) && a[i] >= a[st.top()]) {
                st.pop();
                pr.pop_back();
            }

            int lst = sz(st) ? st.top() : 0;
            ll nxt = range(lst, i - 1, j - 1) + a[i];
            if (sz(pr))
                nxt = min(nxt, pr.back());

            f[i] = nxt;
            st.push(i);
            ll v = sz(pr) ? pr.back() : 1e18;
            pr.pb(min(v, range(lst, i - 1, j - 1) + a[i])); 
        }
    }

    cout << f[n] << '\n';
} 

// 2 2
// 1 2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5464 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 1 ms 5468 KB Output is correct
6 Correct 1 ms 5468 KB Output is correct
7 Correct 1 ms 5468 KB Output is correct
8 Correct 1 ms 5468 KB Output is correct
9 Correct 1 ms 5468 KB Output is correct
10 Correct 1 ms 5468 KB Output is correct
11 Correct 1 ms 5464 KB Output is correct
12 Correct 1 ms 5468 KB Output is correct
13 Correct 1 ms 5468 KB Output is correct
14 Correct 1 ms 5468 KB Output is correct
15 Correct 1 ms 5468 KB Output is correct
16 Correct 1 ms 5468 KB Output is correct
17 Correct 1 ms 5468 KB Output is correct
18 Correct 1 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5468 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5500 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 1 ms 5468 KB Output is correct
6 Correct 1 ms 5468 KB Output is correct
7 Correct 1 ms 5468 KB Output is correct
8 Correct 1 ms 5468 KB Output is correct
9 Correct 1 ms 5468 KB Output is correct
10 Correct 1 ms 5468 KB Output is correct
11 Correct 1 ms 5468 KB Output is correct
12 Correct 1 ms 5468 KB Output is correct
13 Correct 1 ms 5468 KB Output is correct
14 Correct 1 ms 5468 KB Output is correct
15 Correct 1 ms 5464 KB Output is correct
16 Correct 1 ms 5468 KB Output is correct
17 Correct 1 ms 5468 KB Output is correct
18 Correct 1 ms 5468 KB Output is correct
19 Correct 1 ms 5468 KB Output is correct
20 Correct 1 ms 5336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5464 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 1 ms 5468 KB Output is correct
6 Correct 1 ms 5468 KB Output is correct
7 Correct 1 ms 5468 KB Output is correct
8 Correct 1 ms 5468 KB Output is correct
9 Correct 1 ms 5468 KB Output is correct
10 Correct 1 ms 5468 KB Output is correct
11 Correct 1 ms 5464 KB Output is correct
12 Correct 1 ms 5468 KB Output is correct
13 Correct 1 ms 5468 KB Output is correct
14 Correct 1 ms 5468 KB Output is correct
15 Correct 1 ms 5468 KB Output is correct
16 Correct 1 ms 5468 KB Output is correct
17 Correct 1 ms 5468 KB Output is correct
18 Correct 1 ms 5468 KB Output is correct
19 Correct 1 ms 5468 KB Output is correct
20 Correct 1 ms 5468 KB Output is correct
21 Correct 1 ms 5500 KB Output is correct
22 Correct 1 ms 5468 KB Output is correct
23 Correct 1 ms 5468 KB Output is correct
24 Correct 1 ms 5468 KB Output is correct
25 Correct 1 ms 5468 KB Output is correct
26 Correct 1 ms 5468 KB Output is correct
27 Correct 1 ms 5468 KB Output is correct
28 Correct 1 ms 5468 KB Output is correct
29 Correct 1 ms 5468 KB Output is correct
30 Correct 1 ms 5468 KB Output is correct
31 Correct 1 ms 5468 KB Output is correct
32 Correct 1 ms 5468 KB Output is correct
33 Correct 1 ms 5464 KB Output is correct
34 Correct 1 ms 5468 KB Output is correct
35 Correct 1 ms 5468 KB Output is correct
36 Correct 1 ms 5468 KB Output is correct
37 Correct 1 ms 5468 KB Output is correct
38 Correct 1 ms 5336 KB Output is correct
39 Correct 2 ms 5468 KB Output is correct
40 Correct 1 ms 5468 KB Output is correct
41 Correct 1 ms 5468 KB Output is correct
42 Correct 1 ms 5468 KB Output is correct
43 Correct 1 ms 5720 KB Output is correct
44 Correct 1 ms 5464 KB Output is correct
45 Correct 1 ms 5468 KB Output is correct
46 Correct 1 ms 5468 KB Output is correct
47 Correct 1 ms 5468 KB Output is correct
48 Correct 1 ms 5468 KB Output is correct
49 Correct 1 ms 5468 KB Output is correct
50 Correct 1 ms 5468 KB Output is correct
51 Correct 1 ms 5468 KB Output is correct
52 Correct 1 ms 5464 KB Output is correct
53 Correct 1 ms 5468 KB Output is correct
54 Correct 2 ms 5464 KB Output is correct
55 Correct 1 ms 5468 KB Output is correct
56 Correct 2 ms 5720 KB Output is correct
57 Correct 1 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 5464 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 1 ms 5468 KB Output is correct
6 Correct 1 ms 5468 KB Output is correct
7 Correct 1 ms 5468 KB Output is correct
8 Correct 1 ms 5468 KB Output is correct
9 Correct 1 ms 5468 KB Output is correct
10 Correct 1 ms 5468 KB Output is correct
11 Correct 1 ms 5464 KB Output is correct
12 Correct 1 ms 5468 KB Output is correct
13 Correct 1 ms 5468 KB Output is correct
14 Correct 1 ms 5468 KB Output is correct
15 Correct 1 ms 5468 KB Output is correct
16 Correct 1 ms 5468 KB Output is correct
17 Correct 1 ms 5468 KB Output is correct
18 Correct 1 ms 5468 KB Output is correct
19 Correct 1 ms 5468 KB Output is correct
20 Correct 1 ms 5468 KB Output is correct
21 Correct 1 ms 5500 KB Output is correct
22 Correct 1 ms 5468 KB Output is correct
23 Correct 1 ms 5468 KB Output is correct
24 Correct 1 ms 5468 KB Output is correct
25 Correct 1 ms 5468 KB Output is correct
26 Correct 1 ms 5468 KB Output is correct
27 Correct 1 ms 5468 KB Output is correct
28 Correct 1 ms 5468 KB Output is correct
29 Correct 1 ms 5468 KB Output is correct
30 Correct 1 ms 5468 KB Output is correct
31 Correct 1 ms 5468 KB Output is correct
32 Correct 1 ms 5468 KB Output is correct
33 Correct 1 ms 5464 KB Output is correct
34 Correct 1 ms 5468 KB Output is correct
35 Correct 1 ms 5468 KB Output is correct
36 Correct 1 ms 5468 KB Output is correct
37 Correct 1 ms 5468 KB Output is correct
38 Correct 1 ms 5336 KB Output is correct
39 Correct 2 ms 5468 KB Output is correct
40 Correct 1 ms 5468 KB Output is correct
41 Correct 1 ms 5468 KB Output is correct
42 Correct 1 ms 5468 KB Output is correct
43 Correct 1 ms 5720 KB Output is correct
44 Correct 1 ms 5464 KB Output is correct
45 Correct 1 ms 5468 KB Output is correct
46 Correct 1 ms 5468 KB Output is correct
47 Correct 1 ms 5468 KB Output is correct
48 Correct 1 ms 5468 KB Output is correct
49 Correct 1 ms 5468 KB Output is correct
50 Correct 1 ms 5468 KB Output is correct
51 Correct 1 ms 5468 KB Output is correct
52 Correct 1 ms 5464 KB Output is correct
53 Correct 1 ms 5468 KB Output is correct
54 Correct 2 ms 5464 KB Output is correct
55 Correct 1 ms 5468 KB Output is correct
56 Correct 2 ms 5720 KB Output is correct
57 Correct 1 ms 5468 KB Output is correct
58 Correct 12 ms 5468 KB Output is correct
59 Correct 6 ms 11612 KB Output is correct
60 Correct 19 ms 11608 KB Output is correct
61 Correct 151 ms 11612 KB Output is correct
62 Correct 825 ms 18048 KB Output is correct
63 Correct 33 ms 18048 KB Output is correct
64 Correct 397 ms 18260 KB Output is correct
65 Correct 1 ms 5468 KB Output is correct
66 Correct 1 ms 5468 KB Output is correct
67 Correct 1 ms 5468 KB Output is correct
68 Correct 1 ms 5468 KB Output is correct
69 Correct 1 ms 5468 KB Output is correct
70 Correct 1 ms 5468 KB Output is correct
71 Correct 43 ms 5468 KB Output is correct
72 Correct 3 ms 5720 KB Output is correct
73 Correct 12 ms 5464 KB Output is correct
74 Correct 7 ms 11612 KB Output is correct
75 Correct 19 ms 11432 KB Output is correct
76 Correct 162 ms 11608 KB Output is correct
77 Correct 791 ms 18008 KB Output is correct
78 Correct 32 ms 18012 KB Output is correct
79 Correct 388 ms 18044 KB Output is correct
80 Correct 16 ms 18008 KB Output is correct
81 Correct 92 ms 18048 KB Output is correct
82 Correct 848 ms 18048 KB Output is correct
83 Correct 1 ms 5464 KB Output is correct
84 Correct 1 ms 5468 KB Output is correct
85 Correct 40 ms 5724 KB Output is correct
86 Correct 3 ms 5464 KB Output is correct
87 Correct 11 ms 5464 KB Output is correct
88 Correct 7 ms 11868 KB Output is correct
89 Correct 18 ms 11952 KB Output is correct
90 Correct 152 ms 11904 KB Output is correct
91 Correct 793 ms 18856 KB Output is correct
92 Correct 32 ms 19488 KB Output is correct
93 Correct 437 ms 19724 KB Output is correct
94 Correct 3 ms 5468 KB Output is correct
95 Correct 41 ms 5468 KB Output is correct
96 Correct 2 ms 5464 KB Output is correct
97 Correct 1 ms 5468 KB Output is correct
98 Correct 1 ms 5468 KB Output is correct
99 Correct 1 ms 5468 KB Output is correct