# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716859 | vjudge1 | 수열 (APIO14_sequence) | C++17 | 75 ms | 131072 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 1e5 + 100;
const int mod = (int)1e9+7;
int n, k;
int a[maxn];
ll pref[maxn];
ll dp[maxn][202];
int p[maxn][202];
ll d[maxn];
ld inter(int i, int j) {
if(pref[i] == pref[j]) {
cout << "asdf";
exit(0);
}
return (ld)(d[j] - d[i])/(pref[i] - pref[j]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i-1] + a[i];
}
for(int i = 1; i <= n; i++) {
dp[i][0] = 0;
}
for(int it = 1; it <= k; it++) {
int j = 0;
for(int i = 0; i <= n; i++) {
d[i] = dp[i][it-1] - pref[i]*pref[i];
}
vector<int> st;
st.push_back(0);
for(int i = 1; i <= n; i++) {
if(j >= st.size()) {
j = (int)st.size() - 1;
}
while(j + 1 < st.size() && d[st[j]] + pref[i] * pref[st[j]] <= d[st[j+1]] + pref[i] * pref[st[j+1]]) j++;
int t = st[j];
p[i][it] = t;
dp[i][it] = pref[t] * pref[i] + d[t];
int sz = (int)st.size();
while(sz > 0 && pref[i] == pref[st[sz-1]]) {
st.pop_back();
sz--;
}
while(sz > 1 && inter(st[sz - 1], st[sz-2]) > inter(i, st[sz-2])) {
st.pop_back();
sz--;
}
st.push_back(i);
}
}
vector<int> cur;
int last = n;
for(int i = k; i >= 1; i--) {
last = p[last][i];
cur.push_back(last);
}
cout << dp[n][k] << "\n";
reverse(cur.begin(), cur.end());
for(int x: cur) cout << x << " ";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |