이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const ll INF = 1e18;
const ll BASE = 137;
const int szBL = 320;
int n, K;
ll a[N];
ll dp[N][2];
int trace[202][N];
ll pre[N];
struct LINE {
ll a, b, id ;
LINE (ll a, ll b, ll id ) : a(a), b(b), id(id) {}
ll operator() (const ll &x) {
return a * x + b;
}
};
struct convex_hull_trick {
vector<LINE> op;
void init () {
vector<LINE> ().swap(op);
}
bool bad (LINE ln1, LINE ln2, LINE ln3) {
return (long double)(ln3.b - ln1.b) / (ln1.a - ln3.a) <= (long double)(ln2.b - ln1.b) / (ln1.a - ln2.a);
}
void add (LINE ln) {
while (SZ(op) >= 2 && bad(op[SZ(op) - 2], op[SZ(op) - 1], ln)) {
op.pop_back();
}
op.push_back(ln);
}
pll get (ll x) {
if (op.empty()) return MP(INF, 0);
ll lf = 0, rt = SZ(op) - 1;
while (lf < rt) {
ll mid = (lf + rt) >> 1;
if (op[mid](x) <= op[mid + 1](x)) rt = mid;
else lf = mid + 1;
}
return MP(op[lf](x), op[lf].id);
}
}cht;
void solution () {
cin >> n >> K;
rep (i, 1, n) cin >> a[i];
rep (i, 1, n) pre[i] = pre[i - 1] + a[i];
rep (i, 1, n) dp[i][1] = 0;
rep (k, 2, K) {
cht.init();
rep (i, 1, n) {
pll cur = cht.get(pre[i]);
dp[i][k & 1] = -cur.fs;
trace[k][i] = cur.se;
// cout << i <<" "<<k<<" "<<cur.fs<<" "<<cur.se <<"\n";
if (dp[i][k & 1] < INF)
cht.add(LINE(-pre[i], -(dp[i][k & 1 ^ 1] - pre[i] * pre[i]), i));
}
}
ll res = 0;
int opt = 0;
rep (i, K + 1, n) {
if (res < dp[i][K & 1] + (pre[n] - pre[i]) * pre[i]) {
res = dp[i][K & 1] + (pre[n] - pre[i]) * pre[i];
opt = i;
}
}
vector<int> Ans;
reb (k, K, 1) {
Ans.push_back(opt);
opt = trace[k][opt];
}
cout << res <<"\n";
iter (&id, Ans) cout << id<<" ";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("TREEV");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9
7 3
4 1 3 4 0 2 3
*/
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void solution()':
sequence.cpp:87:49: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
87 | cht.add(LINE(-pre[i], -(dp[i][k & 1 ^ 1] - pre[i] * pre[i]), i));
| ~~^~~
# | 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... |