이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//khodaya khodet komak kon
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimise ("ofast")
#pragma GCC optimise("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000000000000000;
const ll LOG = 25;
int n, k, a[N], ps[N], opt[N][210];
ll dp[N][2];
ll cost (int l, int r){
ll x = ps[r] - ps[l - 1];
return x * x;
}
int solve(int l, int r, int lq, int rq, int x, int f){
if (rq < lq) return 0;
int md = (lq + rq) >> 1;
for (int i = l; i < min(r + 1, md); i++){
if (dp[md][f] > dp[i][f ^ 1] + cost(i + 1, md)){
dp[md][f] = dp[i][f ^ 1] + cost(i + 1, md);
opt[md][x] = i;
}
}
//cout << opt[md][x] << '\n';
if (lq == rq) return 0;
solve(l, opt[md][x], lq, md - 1, x,f);
solve(opt[md][x], r, md + 1, rq, x,f);
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
ps[i] = ps[i - 1] + a[i];
}
bool f = 0;
for (int i = 0; i < N; i++) for (int j = 0; j < 210; j ++) dp[i][j] = INF;
for (int i = 1; i <= n; i++){
dp[i][1] = ps[i] * ps[i];
}
for (int i = 2; i <= k + 1; i++){
solve(1, n, 1, n, i, f);
f ^= 1;
}
cout << (ps[n] * 1ll * ps[n] - dp[n][f ^ 1]) / 2<< '\n';
vector<int> ans;
int Now = n;
for (int i = k + 1; i >= 2; i--){
ans.pb(opt[Now][i]);
Now = opt[Now][i];
}
reverse(all(ans));
for (auto u:ans) cout << u << ' ';
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp:8:0: warning: ignoring #pragma GCC optimise [-Wunknown-pragmas]
#pragma GCC optimise ("ofast")
sequence.cpp:9:0: warning: ignoring #pragma GCC optimise [-Wunknown-pragmas]
#pragma GCC optimise("unroll-loops")
sequence.cpp: In function 'int solve(int, int, int, int, int, int)':
sequence.cpp:43:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
sequence.cpp: In function 'int32_t main()':
sequence.cpp:53:70: warning: iteration 2 invokes undefined behavior [-Waggressive-loop-optimizations]
for (int i = 0; i < N; i++) for (int j = 0; j < 210; j ++) dp[i][j] = INF;
~~~~~~~~~^~~~~
sequence.cpp:53:48: note: within this loop
for (int i = 0; i < N; i++) for (int j = 0; j < 210; j ++) dp[i][j] = INF;
~~^~~~~
# | 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... |