이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define mod (long long)(1e9 + 7)
#define eps (long long)(1e-9)
#define vektor vector
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const unsigned Mod = 1e9 + 7;
ll pre[100005], dp[205][100005];
int place[205][100005];
int n, k;
ll cost(int l, int r){
return (pre[r] - pre[l - 1]) * pre[l - 1];
}
bool comp(pair <ll, int>& a, pair <ll, int> b){
if(a.fr != b.fr) return a.fr < b.fr;
return a.sc < b.sc;
}
void dp_do(int l, int r, int optl, int optr, int layer){
if(l > r) return;
pair <ll, int> best(0, 1);
int mid = (l + r) >> 1;
for(int i = optl; i <= min(mid, optr); i ++){
// cout << comp(best, {dp[layer - 1][i - 1] + cost(i, mid), i}) << " " << best.fr << " " << dp[layer - 1][i - 1] + cost(i, mid) << "\n";
if(comp(best, {dp[layer - 1][i - 1] + cost(i, mid), i})) best = {dp[layer - 1][i - 1] + cost(i, mid), i};
}
// cout << layer << " " << mid << " " << best.fr << "\n";
dp[layer][mid] = best.fr;
int opt = best.sc;
place[layer][mid] = best.sc;
//
dp_do(l, mid - 1, optl, opt, layer);
dp_do(mid + 1, r, opt, optr, layer);
}
void solve(){
memset(dp, 0, sizeof(dp));
dp[0][0] = 0;
cin >> n >> k;
for(int i = 1; i <= n; i ++){
ll x;
cin >> x;
pre[i] = pre[i - 1] + x;
}
for(int i = 1; i <= k; i ++) dp_do(1, n, 1, n, i);
cout << dp[k][n] << "\n";
int cur = n;
vector <int> ans;
for(; k >= 1; k --){
ans.pb(place[k][cur] - 1);
cur = place[k][cur] - 1;
}
reverse(all(ans));
for(auto x : ans) cout << x << " ";
}
signed main()
{
// cin.tie(0)->sync_with_stdio(0);
// if(fopen("blank.inp", "r")){
// freopen("blank.inp", "r", stdin);
// freopen("blank.out", "w", stdout);
// }
// // cout << 1; return 0;
ll n = 1;
// cin >> n;
while(n) {
solve();
n --;
cout << "\n";
}
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
// ++++
// ++++-::......:--:::-=+++
// ++++=:............---.....--:..:=++++++
// +++++++::......................::=+++++
// @@ ++++++++++:+++++++++
// @% @*
// %%@@ *****#*@@
// %%%%% @##**##***%
// @@@@@@+-*#*@ #####%+*##*@
// %#+@%%@@@ ##-%:::-#*###@@
// =====:*::: ###-:==::=#*%*
// -====:::::+@@%@@%@#*--=*#%%#@%@ @%@
// @=====%=-@%=:::.:+-::::+::::::::::::%-----%% @@@@@
// @@%#*@++%===+::-================@%@@@@+++++++@@@%%%@#%###%
// @#***#@@ @##=%@===+--:+==+===+%*##@%%%@@@@@@@@@@:= @%@%%%@#%%
// -@ @*#@ #####%:::-====+:::==---**%#@ *%%%%@@@@@@-:
// -=-*% # #***--%%::::::::+=-+#-----=**@*# #+**#@#
// ----=----#+****=----=*#@=::::::=#-----------***%*%
// ------------%*****##=@==::::::+%-----------***%**
// +-----------------*@++%::::::::-----------****@*#
// =------------% @%=+%#+:::::::* #-----*--**** **
// ----%--% *==+++++=+=##*:% ---=--**** **
// %@@@@@@@@@==@+%%%%@ ----**** *#
// @=*+%@@@%@@=++=:%:::% #***@@#
// #+++++++++#==++-:::::: @**** *#
// =+++++++++++==+++::::::: ####@ #%
// +++++++++++=@ @++++::::::@ @#
// @+++++++++=@ =*++=:::::@
// ++++++++@ @++++:::::
// ++++++= =++++:::
// =+++++@ =+++::::
// ++++++-@ =+++:+::
// @::+::::@ =++::-:::
// +::=::::: @+*+:-+++-:
// @::+-:::::# +++::::::@
// ::%=:::::::%@ @%++::::***
// @:+@*:::::::****@ +@+++:-=*:*@
// *:@@%::::::::=**** @%@+++:::#**@
// *%@@@-:::::::%=+==*@ +@%+++:***+*
// #%@@%%::::::::#+=-=# =+@+++++:::*+
// @####+::::::::%+=@#%#=@ @**@@%=++::::@
// @ +:::::::::***#*@ @@###+=+=%@ +**#+=+*+:::-
// =-:::::::%%%%@@ @#=*########%#+*%%+--@*+::%
// +---%@ @@ =-:-**:#
// %%%%% @+--*@
// @%%%%@ @%%%%%
// @%%@@
# | 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... |