이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int N = 101234;
int n, k;
ll a[N], sum[N], dp[210][N];
ll cost (int l, int r) {
return (sum[r+1] - sum[l]) * (sum[n] - sum[r+1]);
}
int nxt[210][N];
void solve (int lo, int hi, int opt_beg, int opt_end, int k) {
if (lo > hi) return ;
int mid = (lo + hi) / 2;
int opt = -1;
for (int i = max(mid, opt_beg); i <= opt_end; i++) {
ll tmp = dp[k-1][i+1] + cost(mid, i);
if (dp[k][mid] < tmp) {
dp[k][mid] = tmp;
opt = i;
}
}
nxt[k][mid] = opt;
solve(lo, mid-1, opt_beg, opt, k);
solve(mid+1, hi, opt, opt_end, k);
}
int main (void) {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%lld", a + i);
sum[i+1] = sum[i] + a[i];
}
memset (dp, -1, sizeof dp);
for (int i = 0; i < n; i++) dp[0][i] = 0;
for (int i = 1; i <= k+1; i++)
solve(0, n-1, 0, n-1, i);
cout << dp[k][0] << endl;
vector <int> path;
int x = k, y = 0;
while (x) {
path.pb(nxt[x][y]);
y = nxt[x][y] + 1;
x--;
}
for (auto u : path) cout << u + 1 << " ";
cout << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:39:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", a + i);
~~~~~^~~~~~~~~~~~~~~
# | 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... |