Submission #632631

#TimeUsernameProblemLanguageResultExecution timeMemory
632631phunghaFeast (NOI19_feast)C++14
100 / 100
135 ms9236 KiB
#include <bits/stdc++.h>
using namespace std;

int n, k, cnt[300001];
long long s[300001], dp[300001];

int calc(long long lambda) {
    // dp[i]: tổng lớn nhất của các đoạn với i phần tử đầu tiên, nếu mỗi đoạn khác rỗng được trừ đi lambda
    // cnt[i]: số đoạn tối thiểu
    dp[0] = cnt[0] = 0;
    long long dp_max = 0;
    int cnt_max = 0;
    for (int i = 1; i <= n; i++) {
        // không dùng thêm đoạn
        dp[i] = dp[i - 1];
        cnt[i] = cnt[i - 1];

        // sử dụng thêm đoạn
        if ((dp_max + s[i] - lambda > dp[i]) || (dp_max + s[i] - lambda == dp[i] && cnt[i] > cnt_max + 1)) {
            dp[i] = dp_max + s[i] - lambda;
            cnt[i] = cnt_max + 1;
        }
        if ((dp[i] - s[i] > dp_max) || (dp[i] - s[i] == dp_max && cnt_max > cnt[i])) {
            dp_max = dp[i] - s[i];
            cnt_max = cnt[i];
        }
    }
    return cnt[n];
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("test11.in", "r", stdin);
    //freopen("test01.out", "w", stdout);
    cin >> n >> k;
    s[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i] += s[i - 1];
    }
    long long l = 0, r = 3e14;
    while (l < r) {
        long long c = (l + r) / 2;
        if (calc(c) > k)
            l = c + 1;
        else
            r = c;
    }
    calc(l);
    cout << dp[n] + l * cnt[n] << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...