Submission #142533

#TimeUsernameProblemLanguageResultExecution timeMemory
142533meatrowSplit the sequence (APIO14_sequence)C++17
100 / 100
585 ms83820 KiB
//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
 
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int K = 205;


struct line {
	ll a;
    ll b;
    int pos;
};
struct CHT {
	int last,top;
	line ln[N];
	inline bool Check(line& a,line& b,line& c) {
		return (ld)(a.b-b.b)*(c.a-b.a)<(ld)(b.b-c.b)*(b.a-a.a);
	}
	inline bool Check2(line& a,line& b,int x) {
		return (ll)x*(b.a-a.a)<a.b-b.b;
	}
	void insert(line u) {
		if(top&&ln[top-1].a==u.a&&ln[top-1].b>=u.b) return;
		while(top>=2&&!Check(ln[top-2],ln[top-1],u)) top--;
		ln[top++]=u;
	}
	line query(int x) {
		last=min(last,top-1);
		while(last+1<top&&!Check2(ln[last],ln[last+1],x))last++;
		return ln[last];
	}
	void clear() {
		last=0; top=0;
	}
}cht;

ll pref[N];
ll dp[2][N];
int way[K][N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> pref[i];
        pref[i] += pref[i - 1];
    }
    for (int i = 1; i <= k; i++) {
        int v = (i - 1) & 1;
        cht.clear();
        cht.insert({ pref[i], dp[v][i] - pref[i] * pref[i], i });
        for (int j = i + 1; j <= n; j++) {
            auto p = cht.query(pref[j]);
            dp[v ^ 1][j] = p.a * pref[j] + p.b;
            way[i][j] = p.pos;
            cht.insert({ pref[j], dp[v][j] - pref[j] * pref[j], j });
        }
    }
    cout << dp[k & 1][n] << '\n';
    int kek = n;
    vector<int> ans;
    for (int i = k; i > 0; i--) {
        ans.push_back(way[i][kek]);
        kek = way[i][kek];
    }
    reverse(ans.begin(), ans.end());
    for (int i : ans) {
        cout << i << ' ';
    }
    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...