이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a) for (int i=0;i<(a);++i)
#define FORR(i,a) for (int i=(a)-1;i>=0;i--)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTR(i,a,b) for (int i=(b);i>=(a);--i)
#define trav(i,v) for (auto i : v)
#define fr first
#define sc second
#define x real
#define y imag
typedef long long ll;
typedef long double ld;
typedef complex<ll> point;
const int N = 2e5+6;
const int K = 406;
class conv_hull{
public:
vector<point> lines;
vector<int> ind;
int pos=0;
ll f(point a, ll x) {
return a.x()*x+a.y();
}
ld inter(point a,point b){
ll k=a.x()-b.x();
ll c=b.y()-a.y();
//cout<<k<<endl;
//cout<<c/k<<endl;
return (ld)c/k;
}
void add_line(point nw,int index){
if (!lines.empty() && nw.x()==lines.back().x()){
return;
}
while (lines.size()>1){
//cout<<1<<endl;
point a=lines.back();
point b=lines[lines.size()-2];
//cout<<1<<endl;
if (inter(nw,b)<=inter(b,a)){
lines.pop_back();
ind.pop_back();
}
else break;
}
lines.push_back(nw);
ind.push_back(index);
}
pair<ll,int> get(ll x){
//cout<<pos<<" "<<lines.size()<<endl;
while(pos<lines.size()-1 && inter(lines[pos],lines[pos+1])<=(ld)x){
pos++;
}
//cout<<lines[pos]<<endl;
return {f(lines[pos],x),ind[pos]};
}
void del(){
lines.clear();
ind.clear();
pos=0;
}
};
ll a[N],dp[N],pref[N];
int nax[K][N];
int main(){
ios_base::sync_with_stdio(false);
//cout<<"hi"<<endl;
int n,k;
cin>>n>>k;k++;
FORT (i,1,n){
cin>>a[i];
pref[i]=pref[i-1]+a[i];
}
FORT(i,1,n){
dp[i]=pref[i]*pref[i];
}
conv_hull t;
FORT(i,2,k){
t.add_line({-2ll*pref[i-1],dp[i-1]+pref[i-1]*pref[i-1]},i-1);
//cout<<-pref[i-1]<<" "<<dp[i-1][i-1]+pref[i-1]*pref[i-1]<<endl;
FORT(j,i,n){
ll z=dp[j];
pair<ll,int> tv=t.get(pref[j]);
dp[j]=tv.fr+pref[j]*pref[j];
nax[i][j]=tv.sc;
t.add_line({-2ll*pref[j],z+pref[j]*pref[j]},j);
//cout<<-2ll*pref[j]<<" "<<dp[i-1][j]+pref[j]*pref[j]<<endl;
}
t.del();
//cout<<endl;
}
cout<<(pref[n]*pref[n]-dp[n])/2ll<<endl;
int tv=nax[k][n];
vector<int> v;
FORTR(i,1,k-1){
v.push_back(tv);
tv=nax[i][tv];
}
reverse(v.begin(),v.end());
trav(ans,v)cout<<ans<<" ";
return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In member function 'std::pair<long long int, int> conv_hull::get(ll)':
sequence.cpp:58:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(pos<lines.size()-1 && inter(lines[pos],lines[pos+1])<=(ld)x){
~~~^~~~~~~~~~~~~~~
# | 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... |