이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/// adil sultanov | vonat1us
#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1e2;
void fin() {
#ifdef AM
freopen(".in", "r", stdin);
#endif
}
const bool flag = 0;
const int N = 1e5+10;
const int K = 210;
ll p[N], dp[N][K], pr[N][K];
void f(int r, int k) {
if (k == 1) return;
int cut = pr[r][k];
cout << cut+1 << " ";
f(cut, k-1);
}
struct Line {
ll k, b, id;
Line() {}
Line(ll k, ll b, int id): k(k), b(b), id(id) {}
ll get(ll x) {
return x*k + b;
}
ll cross(Line l) {
if (k == l.k) return 0;
return (b-l.b)/(l.k-k);
}
};
vector<Line> v;
void add(Line l) {
while (sz(v) > 1 && v.back().cross(l) <= v[sz(v)-2].cross(v.back())) {
v.pop_back();
}
v.pb(l);
}
ll get(ll x) {
int l = 0;
int r = sz(v)-2;
int ans = sz(v)-1;
while (l <= r) {
int m = l+r >> 1;
if (v[m].cross(v[m+1]) >= x) {
ans = m, r = m-1;
} else {
l = m+1;
}
}
assert(ans != -1);
return ans;//v[ans].get(x);
}
int n;
void solve(int k) {
v.clear();
for (int i = 0; i < n; ++i) {
if (i >= k-1) {
int pos = get(p[i]);
dp[i][k] = v[pos].get(p[i]);
pr[i][k] = v[pos].id;
}
add(Line(p[i], dp[i][k-1]-p[i]*p[i], i));
}
}
void ma1n() {
int k;
cin >> n >> k;
k++;
vector<int> a(n);
for (int& x : a) {
cin >> x;
}
for (int i = 0; i < n; ++i) {
p[i] = (i ? p[i-1] : 0ll)+a[i];
}
memset(pr, -1, sizeof(pr));
for (int l = 2; l <= k; ++l) {
solve(l);
}
cout << dp[n-1][k] << "\n";
f(n-1, k);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr), fin();
int ts = 1;
if (flag) {
cin >> ts;
}
while (ts--) {
ma1n();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'll get(ll)':
sequence.cpp:73:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
73 | int m = l+r >> 1;
| ~^~
# | 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... |