답안 #159081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
159081 2019-10-20T14:55:06 Z mrboorger Bigger segments (IZhO19_segments) C++17
100 / 100
529 ms 46364 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#include "interactive.h"

#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;

const int maxn = 500005;
const ll inf = 1e18;

vector <pair <int, ll>> dp[maxn];
int a[maxn];
ll sum[maxn];

ll f(int l, int r)
{
    return sum[r] - (l > 0 ? sum[l - 1] : 0);
}

int main()
{
#ifdef LOCAL
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif // LOCAL
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);
//    cout.tie(0);
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i)
    {
        cin >> a[i];
        sum[i] = a[i] + (i > 0 ? sum[i - 1] : 0);
    }
    for(int i = 0; i < n; ++i)
    {
        dp[i].pb({0, inf});
        dp[i].pb({0, inf});
    }
    dp[0][0] = {1, a[0]};
    for(int i = 0; i < n - 1; ++i)
        for(int j = 0; j < 2; ++j)
    {
        if (dp[i][j].S == inf) continue;
        if (dp[i][j].F > dp[i + 1][0].F)
        {
            swap(dp[i + 1][0], dp[i + 1][1]);
            dp[i + 1][0] = {dp[i][j].F, dp[i][j].S + a[i + 1]};
        }
        else
        if (dp[i][j].F == dp[i + 1][0].F && dp[i][j].S + a[i + 1] < dp[i + 1][0].S)
        {
            dp[i + 1][0].S = dp[i][j].S + a[i + 1];
        }
        else
        if (dp[i][j].F > dp[i + 1][1].F)
        {
            dp[i + 1][1] = {dp[i][j].F, dp[i][j].S + a[i + 1]};
        }
        else
        if (dp[i][j].F == dp[i + 1][1].F && dp[i][j].S + a[i + 1] < dp[i + 1][1].S)
        {
            dp[i + 1][1].S = dp[i][j].S + a[i + 1];
        }

        {
            int l = i + 1, r = n - 1;
            int b = n + 1;
            while(l <= r)
            {
                int m = (l + r) / 2;
                if (f(i + 1, m) >= dp[i][j].S)
                {
                    b = min(b, m);
                    r = m - 1;
                }
                else
                {
                    l = m + 1;
                }
            }
            if (b < n + 1)
            {
                if (dp[i][j].F + 1 > dp[b][0].F)
                {
                    swap(dp[b][0], dp[b][1]);
                    dp[b][0] = {dp[i][j].F + 1, f(i + 1, b)};
                }
                else
                if (dp[i][j].F + 1 == dp[b][0].F && f(i + 1, b) < dp[b][0].S)
                {
                    dp[b][0].S = f(i + 1, b);
                }
                else
                if (dp[i][j].F + 1 > dp[b][1].F)
                {
                    dp[b][1] = {dp[i][j].F + 1, f(i + 1, b)};
                }
                else
                if (dp[i][j].F + 1 == dp[b][1].F && f(i + 1, b) < dp[b][1].S)
                {
                    dp[b][1].S = f(i + 1, b);
                }
            }
        }
    }
    cout << max(dp[n - 1][0].F, dp[n - 1][1].F) << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12024 KB Output is correct
3 Correct 12 ms 12124 KB Output is correct
4 Correct 12 ms 12024 KB Output is correct
5 Correct 12 ms 12124 KB Output is correct
6 Correct 15 ms 12024 KB Output is correct
7 Correct 12 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 12 ms 12024 KB Output is correct
10 Correct 12 ms 12024 KB Output is correct
11 Correct 12 ms 12024 KB Output is correct
12 Correct 12 ms 12028 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12024 KB Output is correct
3 Correct 12 ms 12124 KB Output is correct
4 Correct 12 ms 12024 KB Output is correct
5 Correct 12 ms 12124 KB Output is correct
6 Correct 15 ms 12024 KB Output is correct
7 Correct 12 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 12 ms 12024 KB Output is correct
10 Correct 12 ms 12024 KB Output is correct
11 Correct 12 ms 12024 KB Output is correct
12 Correct 12 ms 12028 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12024 KB Output is correct
18 Correct 13 ms 12024 KB Output is correct
19 Correct 12 ms 12024 KB Output is correct
20 Correct 13 ms 12060 KB Output is correct
21 Correct 13 ms 12072 KB Output is correct
22 Correct 12 ms 12024 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 14 ms 12180 KB Output is correct
25 Correct 13 ms 12024 KB Output is correct
26 Correct 13 ms 12152 KB Output is correct
27 Correct 12 ms 12152 KB Output is correct
28 Correct 14 ms 12152 KB Output is correct
29 Correct 13 ms 12152 KB Output is correct
30 Correct 13 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12024 KB Output is correct
3 Correct 12 ms 12124 KB Output is correct
4 Correct 12 ms 12024 KB Output is correct
5 Correct 12 ms 12124 KB Output is correct
6 Correct 15 ms 12024 KB Output is correct
7 Correct 12 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 12 ms 12024 KB Output is correct
10 Correct 12 ms 12024 KB Output is correct
11 Correct 12 ms 12024 KB Output is correct
12 Correct 12 ms 12028 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12024 KB Output is correct
18 Correct 13 ms 12024 KB Output is correct
19 Correct 12 ms 12024 KB Output is correct
20 Correct 13 ms 12060 KB Output is correct
21 Correct 13 ms 12072 KB Output is correct
22 Correct 12 ms 12024 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 14 ms 12180 KB Output is correct
25 Correct 13 ms 12024 KB Output is correct
26 Correct 13 ms 12152 KB Output is correct
27 Correct 12 ms 12152 KB Output is correct
28 Correct 14 ms 12152 KB Output is correct
29 Correct 13 ms 12152 KB Output is correct
30 Correct 13 ms 12152 KB Output is correct
31 Correct 14 ms 12280 KB Output is correct
32 Correct 15 ms 12280 KB Output is correct
33 Correct 16 ms 12284 KB Output is correct
34 Correct 15 ms 12200 KB Output is correct
35 Correct 16 ms 12280 KB Output is correct
36 Correct 16 ms 12280 KB Output is correct
37 Correct 15 ms 12152 KB Output is correct
38 Correct 15 ms 12152 KB Output is correct
39 Correct 14 ms 12280 KB Output is correct
40 Correct 16 ms 12280 KB Output is correct
41 Correct 14 ms 12280 KB Output is correct
42 Correct 14 ms 12280 KB Output is correct
43 Correct 14 ms 12284 KB Output is correct
44 Correct 14 ms 12280 KB Output is correct
45 Correct 14 ms 12280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12024 KB Output is correct
3 Correct 12 ms 12124 KB Output is correct
4 Correct 12 ms 12024 KB Output is correct
5 Correct 12 ms 12124 KB Output is correct
6 Correct 15 ms 12024 KB Output is correct
7 Correct 12 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 12 ms 12024 KB Output is correct
10 Correct 12 ms 12024 KB Output is correct
11 Correct 12 ms 12024 KB Output is correct
12 Correct 12 ms 12028 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12024 KB Output is correct
18 Correct 13 ms 12024 KB Output is correct
19 Correct 12 ms 12024 KB Output is correct
20 Correct 13 ms 12060 KB Output is correct
21 Correct 13 ms 12072 KB Output is correct
22 Correct 12 ms 12024 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 14 ms 12180 KB Output is correct
25 Correct 13 ms 12024 KB Output is correct
26 Correct 13 ms 12152 KB Output is correct
27 Correct 12 ms 12152 KB Output is correct
28 Correct 14 ms 12152 KB Output is correct
29 Correct 13 ms 12152 KB Output is correct
30 Correct 13 ms 12152 KB Output is correct
31 Correct 14 ms 12280 KB Output is correct
32 Correct 15 ms 12280 KB Output is correct
33 Correct 16 ms 12284 KB Output is correct
34 Correct 15 ms 12200 KB Output is correct
35 Correct 16 ms 12280 KB Output is correct
36 Correct 16 ms 12280 KB Output is correct
37 Correct 15 ms 12152 KB Output is correct
38 Correct 15 ms 12152 KB Output is correct
39 Correct 14 ms 12280 KB Output is correct
40 Correct 16 ms 12280 KB Output is correct
41 Correct 14 ms 12280 KB Output is correct
42 Correct 14 ms 12280 KB Output is correct
43 Correct 14 ms 12284 KB Output is correct
44 Correct 14 ms 12280 KB Output is correct
45 Correct 14 ms 12280 KB Output is correct
46 Correct 67 ms 17924 KB Output is correct
47 Correct 86 ms 18424 KB Output is correct
48 Correct 107 ms 18808 KB Output is correct
49 Correct 108 ms 18888 KB Output is correct
50 Correct 111 ms 18880 KB Output is correct
51 Correct 115 ms 18936 KB Output is correct
52 Correct 64 ms 15480 KB Output is correct
53 Correct 43 ms 14200 KB Output is correct
54 Correct 69 ms 18168 KB Output is correct
55 Correct 112 ms 18972 KB Output is correct
56 Correct 80 ms 18396 KB Output is correct
57 Correct 77 ms 18424 KB Output is correct
58 Correct 80 ms 18424 KB Output is correct
59 Correct 86 ms 18552 KB Output is correct
60 Correct 86 ms 18552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 12024 KB Output is correct
2 Correct 12 ms 12024 KB Output is correct
3 Correct 12 ms 12124 KB Output is correct
4 Correct 12 ms 12024 KB Output is correct
5 Correct 12 ms 12124 KB Output is correct
6 Correct 15 ms 12024 KB Output is correct
7 Correct 12 ms 12024 KB Output is correct
8 Correct 13 ms 12152 KB Output is correct
9 Correct 12 ms 12024 KB Output is correct
10 Correct 12 ms 12024 KB Output is correct
11 Correct 12 ms 12024 KB Output is correct
12 Correct 12 ms 12028 KB Output is correct
13 Correct 12 ms 12152 KB Output is correct
14 Correct 12 ms 12152 KB Output is correct
15 Correct 12 ms 12152 KB Output is correct
16 Correct 12 ms 12024 KB Output is correct
17 Correct 12 ms 12024 KB Output is correct
18 Correct 13 ms 12024 KB Output is correct
19 Correct 12 ms 12024 KB Output is correct
20 Correct 13 ms 12060 KB Output is correct
21 Correct 13 ms 12072 KB Output is correct
22 Correct 12 ms 12024 KB Output is correct
23 Correct 12 ms 12024 KB Output is correct
24 Correct 14 ms 12180 KB Output is correct
25 Correct 13 ms 12024 KB Output is correct
26 Correct 13 ms 12152 KB Output is correct
27 Correct 12 ms 12152 KB Output is correct
28 Correct 14 ms 12152 KB Output is correct
29 Correct 13 ms 12152 KB Output is correct
30 Correct 13 ms 12152 KB Output is correct
31 Correct 14 ms 12280 KB Output is correct
32 Correct 15 ms 12280 KB Output is correct
33 Correct 16 ms 12284 KB Output is correct
34 Correct 15 ms 12200 KB Output is correct
35 Correct 16 ms 12280 KB Output is correct
36 Correct 16 ms 12280 KB Output is correct
37 Correct 15 ms 12152 KB Output is correct
38 Correct 15 ms 12152 KB Output is correct
39 Correct 14 ms 12280 KB Output is correct
40 Correct 16 ms 12280 KB Output is correct
41 Correct 14 ms 12280 KB Output is correct
42 Correct 14 ms 12280 KB Output is correct
43 Correct 14 ms 12284 KB Output is correct
44 Correct 14 ms 12280 KB Output is correct
45 Correct 14 ms 12280 KB Output is correct
46 Correct 67 ms 17924 KB Output is correct
47 Correct 86 ms 18424 KB Output is correct
48 Correct 107 ms 18808 KB Output is correct
49 Correct 108 ms 18888 KB Output is correct
50 Correct 111 ms 18880 KB Output is correct
51 Correct 115 ms 18936 KB Output is correct
52 Correct 64 ms 15480 KB Output is correct
53 Correct 43 ms 14200 KB Output is correct
54 Correct 69 ms 18168 KB Output is correct
55 Correct 112 ms 18972 KB Output is correct
56 Correct 80 ms 18396 KB Output is correct
57 Correct 77 ms 18424 KB Output is correct
58 Correct 80 ms 18424 KB Output is correct
59 Correct 86 ms 18552 KB Output is correct
60 Correct 86 ms 18552 KB Output is correct
61 Correct 285 ms 43388 KB Output is correct
62 Correct 312 ms 43416 KB Output is correct
63 Correct 380 ms 43768 KB Output is correct
64 Correct 494 ms 46072 KB Output is correct
65 Correct 511 ms 46268 KB Output is correct
66 Correct 529 ms 46284 KB Output is correct
67 Correct 428 ms 39468 KB Output is correct
68 Correct 217 ms 25976 KB Output is correct
69 Correct 305 ms 42508 KB Output is correct
70 Correct 514 ms 46364 KB Output is correct
71 Correct 323 ms 43464 KB Output is correct
72 Correct 361 ms 43768 KB Output is correct
73 Correct 396 ms 44392 KB Output is correct
74 Correct 420 ms 44668 KB Output is correct
75 Correct 412 ms 44744 KB Output is correct