이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define K 210
#define N 100010
long long a[N], b[N], pos[N];
double x[N];
int n, k;
long long A[N], s[N];
int pre[K][N];
long long dp[N];
struct Hull {
int start, last;
Hull(int n): start(1), last(0) {}
void init() {
start = 1; last = 0;
}
void add(int k, long long aNew, long long bNew, long long pNew) {
double xNew = -1e18;
while(start <= last) {
if (aNew == a[last]) {
if (bNew >= b[last]) {
b[last] = bNew;
pos[last] = pNew;
}
return;
}
xNew = 1.0 * (b[last] - bNew) / (aNew - a[last]);
if (start == last || xNew >= x[last]) break;
last--;
}
a[++last] = aNew;
b[last] = bNew;
x[last] = xNew;
pos[last] = pNew;
}
long long get(int k, int xQuery) {
if (start > last) return 0;
while (start < last && pos[start + 1] < xQuery && x[start + 1] <= s[xQuery]) start++;
return start;
}
};
void trace(int k, int u) {
if (k != 1) trace(k - 1, pre[k][u]);
printf("%d ", u);
}
int main() {
//freopen("SEQUENCE.inp","r",stdin);
//freopen("SEQUENCE.out","w",stdout);
//ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
//cin>>n>>k;
scanf("%d%d",&n, &k);
for (int i = 1; i<=n; i++) cin>>A[i];
for (int i = 1; i<=n; i++) s[i] = A[i] + s[i - 1];
memset(dp, 0, sizeof dp);
Hull CH(n);
for (int kk = 0; kk<= k - 1; kk++) {
for (int i = 1; i<=n; i++) {
int tmp = CH.get(kk, i);
pre[kk + 1][i] = pos[tmp];
dp[i] = b[tmp] + s[i] * (a[tmp] + s[n] - s[i]);
}
CH.init();
for (int i = 1; i<=n; i++) {
CH.add(kk + 1, s[i], dp[i] - s[n] * s[i], i);
}
}
long long ans = k;
for (int i = k + 1; i<n; i++) if (dp[ans] <= dp[i]) ans = i;
printf("%lld\n",dp[ans]);//<<endl;
trace(k, ans);
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:61:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n, &k);
^
# | 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... |