Submission #366941

#TimeUsernameProblemLanguageResultExecution timeMemory
366941rocks03Split the sequence (APIO14_sequence)C++14
0 / 100
87 ms131076 KiB
//#pragma GCC target("avx2") //#pragma GCC optimization("O3") //#pragma GCC optimization("unroll-loops") #include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define ff first #define ss second #define pb push_back #define SZ(x) ((int)(x).size()) #define all(x) x.begin(), x.end() #define rep(i, a, b) for(int i = (a); i < (b); i++) #define per(i, a, b) for(int i = (a); i >= (b); i--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MAXK = 200+10; const int MAXN = 2e5+100; int N, K, a[MAXN], opt[MAXN][MAXK]; ll pr[MAXN], dp[MAXN][MAXK]; int ck; double slope(int i, int j){ return (double) ((dp[i][ck] - pr[i] * pr[N]) - (dp[j][ck] - pr[j] * pr[N])) / (pr[j] - pr[i]); } void solve(){ cin >> N >> K; rep(i, 1, N + 1){ cin >> a[i]; pr[i] = pr[i - 1] + a[i]; } rep(k, 1, K + 1){ ck = k - 1; deque<int> q; q.push_back(0); rep(i, 1, N + 1){ while(SZ(q) > 1 && slope(q[0], q[1]) <= pr[i]){ q.pop_front(); } int j = q[0]; dp[i][k] = dp[j][k - 1] + (pr[i] - pr[j]) * (pr[N] - pr[i]); opt[i][k] = j; while(SZ(q) > 1 && slope(q[SZ(q) - 2], q.back()) >= slope(q.back(), i)){ q.pop_back(); } q.push_back(i); } } ll ans = 0; int ind = 0; rep(i, 1, N){ if(dp[i][K] >= ans){ ans = dp[i][K], ind = i; } } cout << ans << "\n"; per(k, K, 1){ cout << ind << " "; ind = opt[ind][k]; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); 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...