이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128;
struct line {
ll a, b; int ind;
ll get(ll x) { return a*x+b; }
line() {}
line(ll a, ll b, ll i) : a(a), b(b), ind(i) {}
};
struct cht {
vector<line> l;
int t;
cht() : t(0) {}
void upd(line v) {
while (t+1<l.size() && (l.back().b<=v.b || (i128)(l[l.size()-2].b-v.b)*(l.back().a-l[l.size()-2].a)<=(i128)(l[l.size()-2].b-l.back().b)*(v.a-l[l.size()-2].a))) l.pop_back();
l.emplace_back(v);
}
pair<ll, ll> get(ll x) {
while (t+1<l.size() && l[t].get(x)<l[t+1].get(x)) t++;
return {l[t].get(x), l[t].ind};
}
};
vector<int> rt, ans;
pair<ll, ll> sol(vector<ll> &a, ll s, bool q=false) {
int n=a.size();
vector<ll> d(n), l(n), c(n);
cht st;
st.upd(line(2*a[n-1], 0, -1));
for (int i=0; i<n; i++) {
auto [v, j]=st.get(a[i]);
d[i]=v-2*a[i]*a[i]+s;
l[i]=j;
c[i]=(j<0?0:c[j])+1;
st.upd(line(2*(a[i]+a[n-1]), d[i]-2*a[i]*a[n-1], i));
}
if (q) {
rt.clear();
for (int i=n-1; i!=-1; i=l[i]) rt.emplace_back(i);
reverse(rt.begin(), rt.end());
}
return {d[n-1], c[n-1]};
}
void track(vector<ll> &a, ll s, int p) {
sol(a, -(2*s+1), true);
auto l=rt;
sol(a, -(2*s-1), true);
auto r=rt;
if (l.size()==p) { ans=l; return ; }
if (r.size()==p) { ans=r; return ; }
for (int i=0, j=0; i+1<r.size(); i++) {
while (l[j]<=r[i]) j++;
if (r[i+1]<=l[j]&&i+1+l.size()-j==p) {
for (int k=0; k<=i; k++) ans.emplace_back(r[k]);
for (int k=j; k<l.size(); k++) ans.emplace_back(l[k]);
return ;
}
}
puts("fuck");
assert(0);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin>>n>>k;
k++;
vector<ll> a(n);
for (auto &i:a) cin>>i;
for (int i=1; i<n; i++) a[i]+=a[i-1];
ll s=0, e=1e18;
while (s<e) {
ll m=(s+e)/2;
auto c=sol(a, -(2*m+1)).second;
if (c>k) s=m+1;
else e=m;
}
track(a, s, k);
ll v=0;
for (int i=0; i<k-1; i++) v+=(a[n-1]-a[ans[i]])*(a[ans[i]]-(i?a[ans[i-1]]:0));
cout<<v<<'\n';
for (int i=0; i<k-1; i++) cout<<ans[i]+1<<' ';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'void cht::upd(line)':
sequence.cpp:19:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | while (t+1<l.size() && (l.back().b<=v.b || (i128)(l[l.size()-2].b-v.b)*(l.back().a-l[l.size()-2].a)<=(i128)(l[l.size()-2].b-l.back().b)*(v.a-l[l.size()-2].a))) l.pop_back();
| ~~~^~~~~~~~~
sequence.cpp: In member function 'std::pair<long long int, long long int> cht::get(ll)':
sequence.cpp:23:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | while (t+1<l.size() && l[t].get(x)<l[t+1].get(x)) t++;
| ~~~^~~~~~~~~
sequence.cpp: In function 'void track(std::vector<long long int>&, ll, int)':
sequence.cpp:57:14: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
57 | if (l.size()==p) { ans=l; return ; }
| ~~~~~~~~^~~
sequence.cpp:58:14: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
58 | if (r.size()==p) { ans=r; return ; }
| ~~~~~~~~^~~
sequence.cpp:59:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i=0, j=0; i+1<r.size(); i++) {
| ~~~^~~~~~~~~
sequence.cpp:61:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
61 | if (r[i+1]<=l[j]&&i+1+l.size()-j==p) {
| ~~~~~~~~~~~~~~^~~
sequence.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int k=j; k<l.size(); k++) ans.emplace_back(l[k]);
| ~^~~~~~~~~
# | 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... |