이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;
void solve(){
int n, k; cin >> n >> k;
k++;
vector<int>a(n+1), pref(n+1);
for (int i = 1; i<=n; i++) {
cin >> a[i];
pref[i] = pref[i-1] + a[i];
}
pref.emplace_back(pref.back());
vector dp(n+1, vector<T>(k+1, {-oo, -oo}));
dp[0][0].first = 0;
for (int j = 1; j<=k; j++){
deque<pair<T, int>>q;
q.push_back({{0, 0}, 0});
auto inter = [&](T x, T y){
return (long double)(y.second-x.second)/(long double)(x.first-y.first);
};
auto add = [&](T x, int i){
while ((int)q.size() >= 2 && inter(q[0].first, q[1].first) >= inter(q[1].first, x)) q.pop_front();
q.push_front({x, i});
};
auto query = [&](int x){
auto get = [&](T curr){
return curr.first * x + curr.second;
};
while ((int)q.size() >= 2 && get(q[(int)q.size()-2].first) >= get(q.back().first)) q.pop_back();
return T{get(q.back().first), q.back().second};
};
for (int i = 1; i<=n; i++){
auto [v, id] = query(pref[n]-pref[i]);
v += (pref[n]-pref[i]) * pref[i];
if (v >= dp[i][j].first) {
dp[i][j] = {v, id};
}
add({-pref[i], dp[i][j-1].first}, i);
debug(i, j, dp[i][j].first);
}
}
cout << dp[n][k].first << "\n";
debug(dp[n][k]);
vector<int>ret;
for (int pos = n, ile = k; ile; pos = dp[pos][ile].second, ile--){
ret.emplace_back(pos);
}
reverse(ret.begin(), ret.end());
ret.pop_back();
for (auto x: ret) cout << x << " ";
cout << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# | 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... |