Submission #1106712

#TimeUsernameProblemLanguageResultExecution timeMemory
1106712luvnaSplit the sequence (APIO14_sequence)C++14
0 / 100
145 ms83024 KiB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e5 + 15;

int n, k;
int pref[N];
ll dp[2][N];
bool past = false, succ = true;

struct line{
    ll slope, c, id;
    line() {}
    line(ll slope, ll c, ll id) : slope(slope), c(c), id(id) {}
    pii f(ll x) {return pii(slope*x + c, id);}
};

struct CHT{
    vector<line> lines;

    void reload(){
        while(!lines.empty()) lines.pop_back(); lines.push_back(line(0,0,0));
    }

    bool dump(line d1, line d2, line d3){
        return (d1.c - d2.c)*(d3.slope - d2.slope) >= (d2.c - d3.c)*(d2.slope - d1.slope);
    }

    void add(line d){
        while(sz(lines) > 1 && dump(d, lines.back(), lines[sz(lines)-2])) lines.pop_back();
        lines.push_back(d);
    }

    pii query(ll x){
        int l = 0, r = sz(lines)-1;
        while(l < r){
            int mid = (l+r) >> 1;
            if(lines[mid].f(x).fi <= lines[mid+1].f(x).fi) l = mid + 1;
            else r = mid;
        }
        return lines[l].f(x);
    }
};

CHT solver;
int trace[N][205];

void solve(){
    cin >> n >> k;

    for(int i = 1; i <= n; i++) cin >> pref[i], pref[i] += pref[i-1];

    for(int j = 1; j <= k; j++){
        solver.reload(); swap(succ, past);
        for(int i = 1; i <= n; i++){
            pii e = solver.query(pref[n] - pref[i]);
            trace[i][j] = e.se;
            dp[succ][i] = e.fi + 1LL*pref[i]*(pref[n] - pref[i]);
            solver.add(line(-pref[i], dp[past][i], i));
        }
    }

    int pos = -1;
    ll ans = -1;

    for(int i = 1; i <= n; i++){
        if(maximize(ans, dp[succ][i])) pos = i;
    }

    vector<int> res;

    for(int i = k; i >= 1; i--) res.push_back(pos), pos = trace[pos][i];

    reverse(all(res));

    cout << ans << endl;

    for(int x : res) cout << x << " ";
}

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

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

sequence.cpp: In member function 'void CHT::reload()':
sequence.cpp:42:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   42 |         while(!lines.empty()) lines.pop_back(); lines.push_back(line(0,0,0));
      |         ^~~~~
sequence.cpp:42:49: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   42 |         while(!lines.empty()) lines.pop_back(); lines.push_back(line(0,0,0));
      |                                                 ^~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         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...