# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
716863 | vjudge1 | 수열 (APIO14_sequence) | C++17 | 670 ms | 86856 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 1e6 + 100;
const int mod = (int)1e9+7;
int n, k;
int a[maxn];
ll pref[maxn];
ll d[maxn];
ll dp[maxn];
ld inter(int i, int j) {
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];
}
vector< vector<int> > p(n + 1, vector<int>(k+1, 0));
for(int i = 1; i <= n; i++) {
dp[i] = 0;
}
for(int it = 1; it <= k; it++) {
int j = 0;
for(int i = 0; i <= n; i++) {
d[i] = dp[i] - 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] = 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] << "\n";
reverse(cur.begin(), cur.end());
for(int x: cur) cout << x << " ";
}
컴파일 시 표준 에러 (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... |