답안 #503126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503126 2022-01-07T10:11:57 Z stanislavpolyn Bigger segments (IZhO19_segments) C++17
100 / 100
73 ms 15068 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for(int i = (a); i <= (b); ++i)
#define rf(i, a, b) for(int i = (a); i >= (b); --i)
#define fe(x, y) for(auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()

using namespace std;

mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template<typename T>
bool umn(T& a, T b) { return (a > b ? (a = b, 1) : 0); }
template<typename T>
bool umx(T& a, T b) { return (a < b ? (a = b, 1) : 0); }

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T>
using ve = vector<T>;

const int N = 5e5 + 5;

int n;
int a[N];
ll pref[N];
//int dp[N][N];
//pii par[N][N];
//
//int brute1() {
//    fr(i, 0, n) {
//        fr(j, 0, n) {
//            dp[i][j] = -1e9;
//        }
//    }
//
//    fr(i, 1, n) {
//        dp[i][1] = 1;
//
//        rf(j, i, 1) {
//            if(dp[i][j] != -1e9) {
//                fr(nxt, i + 1, n) {
//                    if(pref[nxt] - pref[i] >= pref[i] - pref[j - 1]) {
//                        if(umx(dp[nxt][i + 1], dp[i][j] + 1)) {
//                            par[nxt][i + 1] = {i, j};
//                        }
//                    }
//                }
//                break;
//            }
//        }
//    }
//
//    int mx = 0;
//    pii best;
//    fr(i, 1, n) {
//        fr(j, 1, n) {
//            if(umx(mx, dp[i][j])) {
//                best = {i, j};
//            }
//        }
//    }
//    ve<int> ans;
//    while(1) {
//        ans.pb(best.fi);
//        if(best.se == 1) break;
//        best = par[best.fi][best.se];
//    }
//    reverse(all(ans));
////    cout << "ans: ";
////    fe(x, ans) cout << x << " ";
////    cout << "\n";
//
//    int prev = 0;
//    fe(x, ans) {
////        cout << pref[x] - pref[prev] << " ";
//        prev = x;
//    }
////    cout << "\n";
//
//    return mx;
//}

int solve() {
    ve<int> dp(n + 1), cnt(n + 1);
    fr(i, 0, n) dp[i] = -1e9, cnt[i] = -1e9;

    fr(i, 1, n) {
        if(umx(dp[i], 1)) {
            cnt[i] = 1;
        }
        if(umx(dp[i + 1], dp[i])) {
            cnt[i + 1] = cnt[i];
        }
        int l = i + 1;
        int r = n;
        while(l < r) {
            int mid = l + ((r - l) >> 1);
            if(pref[mid] - pref[i] < pref[i] - pref[dp[i] - 1]) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }

        if(pref[l] - pref[i] >= pref[i] - pref[dp[i] - 1]) {
            if(umx(dp[l], i + 1)) {
                cnt[l] = cnt[i] + 1;
            }
        }

//        ll sum = 0;
//        fr(j, i + 1, n) {
//            sum += a[j];
//            if(sum >= pref[i] - pref[dp[i] - 1]) {
//                break;
//            }
//        }
    }
    return *max_element(all(cnt));
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
#else
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif

    cin >> n;

    fr(i, 1, n) {
        cin >> a[i];
//        a[i] = rng() % int(1e6) + 1;
        pref[i] = a[i] + pref[i - 1];
    }
//    fr(i, 1, n) cout << a[i] << " ";
//    cout << "\n";

//    cout << brute1() << "\n";
//    cout << brute2() << "\n";
    cout << solve() << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 10 ms 2556 KB Output is correct
47 Correct 12 ms 2604 KB Output is correct
48 Correct 15 ms 3012 KB Output is correct
49 Correct 12 ms 3144 KB Output is correct
50 Correct 12 ms 3292 KB Output is correct
51 Correct 15 ms 3252 KB Output is correct
52 Correct 8 ms 1736 KB Output is correct
53 Correct 5 ms 1164 KB Output is correct
54 Correct 10 ms 2440 KB Output is correct
55 Correct 13 ms 3140 KB Output is correct
56 Correct 10 ms 2660 KB Output is correct
57 Correct 11 ms 2588 KB Output is correct
58 Correct 11 ms 2680 KB Output is correct
59 Correct 10 ms 2764 KB Output is correct
60 Correct 10 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 10 ms 2556 KB Output is correct
47 Correct 12 ms 2604 KB Output is correct
48 Correct 15 ms 3012 KB Output is correct
49 Correct 12 ms 3144 KB Output is correct
50 Correct 12 ms 3292 KB Output is correct
51 Correct 15 ms 3252 KB Output is correct
52 Correct 8 ms 1736 KB Output is correct
53 Correct 5 ms 1164 KB Output is correct
54 Correct 10 ms 2440 KB Output is correct
55 Correct 13 ms 3140 KB Output is correct
56 Correct 10 ms 2660 KB Output is correct
57 Correct 11 ms 2588 KB Output is correct
58 Correct 11 ms 2680 KB Output is correct
59 Correct 10 ms 2764 KB Output is correct
60 Correct 10 ms 2764 KB Output is correct
61 Correct 53 ms 11972 KB Output is correct
62 Correct 56 ms 11972 KB Output is correct
63 Correct 63 ms 12356 KB Output is correct
64 Correct 73 ms 14688 KB Output is correct
65 Correct 64 ms 14904 KB Output is correct
66 Correct 72 ms 14936 KB Output is correct
67 Correct 61 ms 11972 KB Output is correct
68 Correct 29 ms 6068 KB Output is correct
69 Correct 49 ms 11260 KB Output is correct
70 Correct 69 ms 15068 KB Output is correct
71 Correct 44 ms 12008 KB Output is correct
72 Correct 51 ms 12308 KB Output is correct
73 Correct 50 ms 12996 KB Output is correct
74 Correct 53 ms 13324 KB Output is correct
75 Correct 53 ms 13384 KB Output is correct