이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 7;
const ll oo = 1e18 + 69;
int n, k, a[maxn], trace[207][maxn];
ll f[maxn], dp[207][maxn];
struct line {
ll m, b;
int id;
};
double inter(line &c, line &d) {
return (double) (d.b - c.b) / (double) (c.m - d.m);
}
struct cht {
line lines[maxn];
int sz = 0;
void init() {
sz = 0;
}
bool bad(line &a, line &b, line &c) {
return inter(a, c) > inter(a, b);
}
ll calc(line a, ll x) {
return a.m * x + a.b;
}
pair<ll, int> query(ll x) {
pair<ll, int> res = {calc(lines[0], x), lines[0].id};
int l = 0, r = sz - 1, mid;
while(l < r) {
mid = l + r >> 1;
ll s1 = calc(lines[mid], x);
ll s2 = calc(lines[mid + 1], x);
if(maxi(res.fi, s1)) res.se = lines[mid].id;
if(maxi(res.fi, s2)) res.se = lines[mid + 1].id;
if(s1 > s2) {
r = mid;
} else {
l = mid + 1;
}
}
return res;
}
void add(line new_line) {
int l = 1, r = sz - 1, mid, res = sz;
while(l <= r) {
mid = l + r >> 1;
if(!bad(lines[mid - 1], lines[mid], new_line)) {
r = mid - 1;
res = mid;
}
else {
l = mid + 1;
}
}
lines[res] = new_line;
sz = res + 1;
}
} cht;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen(TASK".inp", "r", stdin);
// freopen(TASK".out", "w", stdout);
cin>>n>>k;
for(int i = 1; i <= n; i++) {
cin>>a[i];
f[i] = f[i - 1] + a[i];
}
for(int c = 1; c <= k; c++) {
cht.init();
cht.add({f[c - 1], -f[c - 1] * f[n] + dp[c - 1][c - 1], c - 1});
for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= i; j++) {
// if(maxi(dp[c][i], f[i] * f[n] - f[i] * f[i] + f[j - 1] * f[i] - f[j - 1] * f[n] + dp[c - 1][j - 1])) trace[c][i] = j - 1;
// }
if(i < c) continue;
auto tmp = cht.query(f[i]);
dp[c][i] = f[i] * f[n] - f[i] * f[i] + tmp.fi;
trace[c][i] = tmp.se;
cht.add({f[i], -f[i] * f[n] + dp[c - 1][i], i});
}
}
ll ans = -oo;
int pos;
vector<int> res;
for(int i = 1; i <= n; i++) {
if(maxi(ans, dp[k][i])) pos = i;
}
res.pb(pos);
pos = trace[k][pos];
for(int i = k - 1; i >= 1; i--) {
res.pb(pos);
pos = trace[i][pos];
}
reverse(all(res));
cout<<ans<<"\n";
for(auto i:res) cout<<i<<" ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'std::pair<long long int, int> cht::query(long long int)':
sequence.cpp:54:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | mid = l + r >> 1;
| ~~^~~
sequence.cpp: In member function 'void cht::add(line)':
sequence.cpp:71:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | mid = 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... |