이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100001, K = 201;
ll dp[2][N], p[N];
int par[K][N];
struct CHT{
vector<ll> m, b, id;
int ptr = 0;
bool bad(int l1, int l2, int l3) {
return (b[l3] - b[l1]) * (m[l1] - m[l2]) <= (b[l2] - b[l1]) * (m[l1] - m[l3]);
}
void clear() {
ptr = 0;
m.clear();
b.clear();
id.clear();
}
void add(ll _m, ll _b, ll idx) {
m.push_back(_m);
b.push_back(_b);
id.push_back(idx);
int s = m.size();
while(s >= 3 && bad(s-3, s-2, s-1)) {
s--;
m.erase(m.end()-2);
b.erase(b.end()-2);
id.erase(id.end()-2);
}
}
ll f(int i, ll x) { return m[i]*x + b[i]; }
pair<ll,ll> query(ll x) {
if(ptr >= m.size()) ptr = m.size()-1;
while(ptr < m.size()-1 &&
f(ptr+1, x) >= f(ptr, x)) ptr++;
return {id[ptr], f(ptr, x)};
}
};
void recon(int k, int n) {
if(!k) return;
recon(k-1, par[k][n]);
cout<<par[k][n]<<" ";
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
int n, kk; cin>>n>>kk;
for(int i=1; i <= n; i++) {
cin>>p[i];
p[i] += p[i-1];
}
for(int i=0; i<=1; i++)
for(int j=0; j<=n; j++)
dp[i][j] = -1e18;
for(int i=0; i<=n; i++)
dp[0][i] = 0;
CHT ds;
for(int _k=1; _k <= kk; _k++) {
ds.clear();
ds.add(0, 0, 0);
int k = _k & 1;
for(int i=_k; i<=n; i++) {
auto it = ds.query(p[i]);
dp[k][i] = it.second;
ds.add(p[i], dp[k ^ 1][i] - p[i] * p[i], i);
par[_k][i] = it.first;
}
}
cout<<dp[kk & 1][n]<<endl;
recon(kk, n);
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'std::pair<long long int, long long int> CHT::query(ll)':
sequence.cpp:36:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ptr >= m.size()) ptr = m.size()-1;
^
sequence.cpp:37:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < m.size()-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... |