| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1313310 | wedonttalkanymore | 수열 (APIO14_sequence) | C++20 | 0 ms | 332 KiB |
#include <bits/stdc++.h>
/*
Checklist:
- Check statement:
- Check filename:
- Check test limit:
- Stresstest:
*/
using namespace std;
using ll = long long;
//#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n, k;
int a[N];
ll pfs[N];
struct Convexhull {
vector <pair <pii, int> > hull;
void reset() {
hull.clear();
}
ll cal(pii a, ll x) {
return a.fi * x + a.se;
}
bool bad(pii x, pii y, pii z) {
return (y.se - x.se) * (y.fi - z.fi) >= (z.se - y.se) * (x.fi - y.fi);
}
void add(ll a, ll b, ll id) {
if (hull.size() < 2) {
hull.push_back({{a, b}, id});
return;
}
int sz = hull.size();
while(sz >= 2 && bad(hull[sz - 2].fi, hull[sz - 1].fi, make_pair(a, b))) {
hull.pop_back();
sz--;
}
hull.push_back({{a, b}, id});
}
ll get(ll x) {
int l = 0, r = hull.size() - 1, ans = 0;
// if (l == 0 && r == -1) {
// cout << "gg: " << '\n';
//// return 0;
// }
while(l <= r) {
int mid = (l + r) / 2;
if (mid + 1 == hull.size()) {
ans = mid;
break;
}
if (cal(hull[mid].fi, x) <= cal(hull[mid + 1].fi, x)) {
ans = mid;
l = mid + 1;
}
else {
ans = mid;
r = mid - 1;
}
}
// if (l == 0 && r == -1) {
// cout << "gg: " << '\n';
//// return 0;
// }
// if (x == 9) {
// for (auto tt : hull) cout << "at: " << tt.fi.fi << ' ' << tt.fi.se << '\n';
// }
if (ans < hull.size()) return hull[ans].se;
return 0;
}
} cht;
ll dp[N][2];
int trace[205][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) pfs[i] = pfs[i - 1] + a[i];
for (int i = 1; i <= k; i++) {
cht.reset();
for (int j = 1; j <= n; j++) {
int idx = cht.get(pfs[n] - pfs[j]);
dp[j][1] = dp[idx][0] - pfs[idx] * (pfs[n] - pfs[j]) + pfs[j] * (pfs[n] - pfs[j]);
trace[i][j] = idx;
// cout << i << ' ' << j << ' ' << idx << '\n';
cht.add(-pfs[j], dp[j][0], j);
}
for (int j = 1; j <= n; j++) dp[j][0] = dp[j][1];
// for (int j = 1; j <= n; j++) cout << dp[j][0] << ' '; cout << '\n';
}
// cout << dp[n][0] << '\n';
ll ans = 0, idx = n;
for (int i = 1; i <= n; i++) {
if (ans < dp[i][0]) {
ans = dp[i][0];
idx = i;
}
}
vector <ll> res;
int cur = idx;
for (int i = k; i >= 1; i--) {
res.push_back(cur);
cur = trace[i][cur];
}
reverse(res.begin(), res.end());
cout << ans << '\n';
for (auto x : res) cout << x << ' ';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | 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... | ||||
