# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
851667 | iah | 수열 (APIO14_sequence) | C++14 | 104 ms | 131072 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define sz(x) (int32_t) (x.size())
struct line{
ll a, b;
ll calc (ll x) {
return a * x + b;
}
double intersect(line x) {
return (double) (x.b - b) / (double) (a - x.a);
}
};
signed main() {
if (fopen("sequence.in","r")) {
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
}
cin.tie(0)->sync_with_stdio(0);
int n, lim;
cin >> n >> lim;
vector < ll > a(n + 1, 0);
vector < vector < ll > > dp(n + 1, vector < ll > (lim + 2, 0));
vector < vector < int > > trace(n + 1, vector < int > (lim + 2, 0));
// dp[i][j] = max(dp[k][j - 1] - a[k] * a[k] + a[i] * a[k]);
for (int i = 1; i <= n; i ++) {
cin >> a[i];
a[i] += a[i - 1];
}
for (int j = 1; j <= lim + 1; j++) {
deque < pair < line , int > > dq;
dq.push_back({line{a[j - 1], dp[j - 1][j - 1] - a[j - 1] * a[j - 1]}, j - 1});
for (int i = j; i <= n; i ++) {
while (sz(dq) >= 2 && dq[0].fi.intersect(dq[1].fi) <= a[i]) {
dq.pop_front();
}
dp[i][j] = dq[0].fi.calc(a[i]);
trace[i][j] = dq[0].se;
if (j == 1) continue;
line cur_line = {a[i], dp[i][j - 1] - a[i] * a[i]};
while (sz(dq) >= 2 && dq[sz(dq) - 2].fi.intersect(cur_line) <= dq[sz(dq) - 2].fi.intersect(dq[sz(dq) - 1].fi)) {
dq.pop_back();
}
dq.push_back({cur_line, i});
}
}
cout << dp[n][lim + 1] << "\n";
int cur_pos = trace[n][lim + 1], cur_k = lim;
while (cur_k > 0) {
cout << cur_pos << " ";
cur_pos = trace[cur_pos][cur_k];
cur_k --;
}
return 0;
}
컴파일 시 표준 에러 (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... |