Submission #671137

#TimeUsernameProblemLanguageResultExecution timeMemory
671137vuavisaoFeast (NOI19_feast)C++14
100 / 100
126 ms10412 KiB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;

template<typename Lhs, typename Rhs> inline bool Max_self(Lhs &a, Rhs b) { if(b > a) { a = b; return true; } return false; }
template<typename Lhs, typename Rhs> inline bool Min_self(Lhs &a, Rhs b) { if(b < a) { a = b; return true; } return false; }

const ll N = 3e5 + 10;
const ll INF = 1e18;

ll n, k, a[N];

namespace sub6 {
    bool check() {
        return (n <= 2000);
    }

    ll dp[2010][2010];
    ll res;

    void solve() {
        for(ll gr = 1; gr <= k; ++ gr) {
            for(ll i = 1; i <= n; ++ i) {
                Max_self(dp[i][gr], dp[i - 1][gr] + a[i]);
                Max_self(dp[i][gr], dp[i - 1][gr - 1] + a[i]);
            }
            for(ll i = 1; i <= n; ++ i) Max_self(dp[i][gr], dp[i - 1][gr]);
            Max_self(res, dp[n][gr]);
        }
        cout << res;
    }
}

namespace last_sub {

    ll dp[N], cnt[N];

    bool check(ll lamba) {
        ll best[2] = {0, 0};
        for(ll i = 1; i <= n; ++ i) {
            dp[i] = dp[i - 1];
            cnt[i] = cnt[i - 1];
            if(Max_self(dp[i], best[0] + a[i] - lamba)) {
                cnt[i] = cnt[best[1]] + 1;
            }
            if(Max_self(best[0], dp[i] - a[i])) {
                best[1] = i;
            }
        }
        return (cnt[n] <= k);
    }

    void solve() {
//        cout << check(INF) << '\n';
        for(ll i = 1; i <= n; ++ i) a[i] += a[i - 1];
        ll l = 0, r = INF;
        ll lamba = - 1;
        while(l <= r) {
            ll mid = (l + r) >> 1ll;
            if(check(mid)) {
                lamba = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        assert(lamba != - 1);
        check(lamba);
//        cout << dp[n] << ' ' << cnt[n] << '\n';
        cout << dp[n] + lamba * cnt[n];
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("Feast.inp", "r")) {
        freopen("Feast.inp", "r", stdin);
        freopen("Feast.out", "w", stdout);
    }
    cin >> n >> k;
    for(ll i = 1; i <= n; ++ i) cin >> a[i];
//    if(sub6::check()) {
//        sub6::solve();
//        return 0;
//    }
//    assert(false);
    last_sub::solve();
    return 0;
}

/// Code by vuavisao

Compilation message (stderr)

feast.cpp: In function 'int32_t main()':
feast.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen("Feast.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen("Feast.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...