Submission #949731

#TimeUsernameProblemLanguageResultExecution timeMemory
949731VinhLuuSplit the sequence (APIO14_sequence)C++17
89 / 100
1429 ms90476 KiB
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const ll oo = -1e12;
const int K = 205;
const int mod = 998244353;
//const int base = 23;

ll n, k, a[N];

namespace sub3{

    ll f[N][210], dp[N][210], s[N];

    int cal(ll i,int j){
        return 1ll * (s[j] - s[i]) * (s[n] - s[j]);
    }

    void solve(int j,int l,int r,int pl,int pr){
        if(l > r) return;
        int mid = (l + r) / 2;
        for(int i = pl; i <= min(mid - 1, pr); i ++){
            ll val = dp[i][j - 1] + cal(i, mid);
            if(val > dp[mid][j]){
                dp[mid][j] = val;
                f[mid][j] = i;
            }
        }
        solve(j, l, mid - 1, pl, f[mid][j]);
        solve(j, mid + 1, r, f[mid][j], pr);
    }

    void sol(){

        for(int i = 1; i <= n; i ++){

            s[i] = s[i - 1] + a[i];
        }
        if(k == 0){
            cout << 0;
            return ;
        }
        for(int j = 0; j <= k; j ++){
            for(int i = 0; i <= n; i ++){
                dp[i][j] = oo;
            }
        }
        for(int i = 1; i <= n; i ++) dp[i][1] = 1ll * s[i] * (s[n] - s[i]), f[i][1] = 0;
        for(int j = 2; j <= k; j ++){
            solve(j, j, n, 1, n);
        }
        ll ans = oo, tmp = 0, cnt = k;
        for(int i = 1; i <= n; i ++){
            if(ans < dp[i][k]){
                ans = dp[i][k];
                tmp = i;
            }
        }
        cout << ans << "\n";

        vector<int> vr;
        vr.pb(tmp);
        while(f[tmp][cnt]){
            vr.pb(f[tmp][cnt]);
            tmp = f[tmp][cnt];
            cnt--;
        }
        reverse(all(vr));
        for(auto j : vr) cout << j << " ";

    }
}
namespace AC{
    ll dp[N][5], s[N], pre, nx;

    int f[N][K];

    ll cal(int i,int j){
        return 1ll * (s[j] - s[i]) * (s[n] - s[j]);
    }

    void solve(int j,int l,int r,int pl,int pr){
        if(l > r) return;
        int mid = (l + r) / 2;
        dp[mid][nx] = oo;
        for(int i = pl; i <= min(mid - 1, pr); i ++){
            ll val = dp[i][pre] + cal(i, mid);
            if(val > dp[mid][nx]){
                dp[mid][nx] = val;
                f[mid][j] = i;
            }
        }
        solve(j, l, mid - 1, pl, f[mid][j]);
        solve(j, mid + 1, r, f[mid][j], pr);
    }

    void sol(){
        for(int i = 1; i <= n; i ++) s[i] = s[i - 1] + a[i];
        if(k == 0){
            cout << 0;
            return ;
        }
        for(int j = 0; j <= 1; j ++){
            for(int i = 0; i <= n; i ++){
                dp[i][j] = oo;
            }
        }

        pre = 0, nx = 1;
        for(int i = 1; i <= n; i ++) dp[i][0] = 1ll * s[i] * (s[n] - s[i]), f[i][0] = 0;
        for(int j = 2; j <= k; j ++){
            solve(j, j, n, 1, n - 1);
            swap(pre, nx);
        }
        ll ans = oo;
        int tmp = 0, cnt = k;
        for(int i = 1; i <= n; i ++){
            if(ans < dp[i][pre]){
                ans = dp[i][pre];
                tmp = i;
            }
        }
        cout << ans << "\n";

        vector<int> vr;
        vr.pb(tmp);
        while(f[tmp][cnt]){
            vr.pb(f[tmp][cnt]);
            tmp = f[tmp][cnt];
            cnt--;
        }
        for(int i = vr.size() - 1; i >= 0; i --) cout << vr[i] << " ";

    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    cin >> n >> k;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
    }
        AC :: sol();
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |         freopen(task ".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...