답안 #945228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
945228 2024-03-13T14:43:28 Z fzyzzz_z Bigger segments (IZhO19_segments) C++17
100 / 100
85 ms 17068 KB
#include <bits/stdc++.h>
using namespace std; 

using ll = long long; 

const ll inf = 1e18 + 50;

ll solve(int n, vector<ll> & a) {
    // at i, for j > i s.t. s(i + 1, j) >= v_i upd(at_j, (ans_i + 1, i)) first max ans, then maximize i 
    vector<ll> p(n + 1, 0); 
    for (int i = 0; i < n; ++i) p[i + 1] = p[i] + a[i]; 

    vector<pair<int, int>> best(n, {1, -1}); 

    for (int i = 0; i < n; ++i) {
        if (i > 0) best[i] = max(best[i], best[i - 1]); 

        // cout << "! proc i " << i << ' ' << best[i].first << ' ' << best[i].second << '\n'; 

        ll b = p[i + 1] - p[best[i].second + 1]; 
        // cout << "b " << b << '\n';
        if (p[n] - p[i + 1] >= b) {
            int lo = i + 1, hi = n - 1; 
            while (lo < hi) {
                int md = (hi + lo) / 2; 
                if (p[md + 1] - p[i + 1] >= b) {
                    hi = md; 
                } else {
                    lo = md + 1; 
                }
            }
            // cout << "next " << lo << '\n';

            best[lo] = max(best[lo], {best[i].first + 1, i}); 
        }
    }

    return best[n - 1].first; 
}

ll slow(int n, vector<ll> & a) {
    ll ans = 0; 
    for (int i = 0; i < (1 << (n - 1)); ++i) {
        ll c = 0, last = 1, cnt = 0; 
        for (int j = 0; j < n; ++j) {
            c += a[j]; 
            if ((1 << j) & i) {
                if (c >= last) {
                    last = c; 
                    c = 0; 
                    cnt++; 
                } else {
                    cnt = -n; 
                    break; 
                }
            }
        }
        if (c >= last) cnt++; 
        ans = max(ans, cnt); 
    }
    return ans; 
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n; 
    vector<ll> a(n); 
    for (auto & x: a) cin >> x; 
    cout << solve(n, a) << '\n';
    // int t; 
    // cin >> t; 
    // for (int i = 0; i < t; ++i) {
    //     int n; 
    //     cin >> n; 
    //     vector<ll> a(n); 
    //     for (auto & x: a) cin >> x; 
    //     ll x = solve(n, a), y = slow(n, a); 

    //     cout << x << '\n';
    //     if (x != y) {
    //         cout << "! " << x << ' ' << y << '\n';
    //         for (auto z: a) cout << z << '\n';
    //     }
    // }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 436 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 452 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 436 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 452 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 436 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 452 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 6 ms 3164 KB Output is correct
47 Correct 12 ms 3164 KB Output is correct
48 Correct 14 ms 3640 KB Output is correct
49 Correct 14 ms 3676 KB Output is correct
50 Correct 16 ms 3676 KB Output is correct
51 Correct 16 ms 3676 KB Output is correct
52 Correct 8 ms 2128 KB Output is correct
53 Correct 5 ms 1372 KB Output is correct
54 Correct 16 ms 3032 KB Output is correct
55 Correct 14 ms 3672 KB Output is correct
56 Correct 15 ms 3160 KB Output is correct
57 Correct 13 ms 3160 KB Output is correct
58 Correct 13 ms 3164 KB Output is correct
59 Correct 14 ms 3164 KB Output is correct
60 Correct 13 ms 3416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 436 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 452 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 472 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 6 ms 3164 KB Output is correct
47 Correct 12 ms 3164 KB Output is correct
48 Correct 14 ms 3640 KB Output is correct
49 Correct 14 ms 3676 KB Output is correct
50 Correct 16 ms 3676 KB Output is correct
51 Correct 16 ms 3676 KB Output is correct
52 Correct 8 ms 2128 KB Output is correct
53 Correct 5 ms 1372 KB Output is correct
54 Correct 16 ms 3032 KB Output is correct
55 Correct 14 ms 3672 KB Output is correct
56 Correct 15 ms 3160 KB Output is correct
57 Correct 13 ms 3160 KB Output is correct
58 Correct 13 ms 3164 KB Output is correct
59 Correct 14 ms 3164 KB Output is correct
60 Correct 13 ms 3416 KB Output is correct
61 Correct 28 ms 14088 KB Output is correct
62 Correct 47 ms 13904 KB Output is correct
63 Correct 65 ms 14544 KB Output is correct
64 Correct 77 ms 16628 KB Output is correct
65 Correct 83 ms 16980 KB Output is correct
66 Correct 80 ms 17016 KB Output is correct
67 Correct 64 ms 13652 KB Output is correct
68 Correct 37 ms 6992 KB Output is correct
69 Correct 64 ms 13196 KB Output is correct
70 Correct 75 ms 17068 KB Output is correct
71 Correct 70 ms 13916 KB Output is correct
72 Correct 67 ms 14344 KB Output is correct
73 Correct 71 ms 14916 KB Output is correct
74 Correct 85 ms 15440 KB Output is correct
75 Correct 73 ms 15444 KB Output is correct