이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define MAX 105000
typedef long long ftype;
typedef std::pair<ftype,ftype> point;
ftype f(point a, ftype x) {
return a.first*x + a.second;
}
const long long val = 1LL<<30LL;
const int maxn = 4e5;
const int membros = 25 * maxn;
point mem[membros];
int lefta[membros],righta[membros];
int cur=3;
int alocar(void){
int x=cur;
if(x==20*maxn)assert(0);
cur++;
mem[x]={0,-1LL<<62LL};
lefta[x]=righta[x]=0;
return x;
}
int inicio = alocar();
void clear(void){
cur=3;
inicio=alocar();
}
void add_line(point nw, int v = inicio, long long l = -val, long long r = val) {
long long m = (l + r) / 2LL;
bool lef = f(nw, l) > f(mem[v], l);
bool mid = f(nw, m) > f(mem[v], m);
if(mid) {
std::swap(mem[v], nw);
}
if(r-l==1) {
return;
} else if(lef != mid) {
if(!lefta[v])lefta[v]=alocar();
add_line(nw, lefta[v], l, m);
} else {
if(!righta[v])righta[v]=alocar();
add_line(nw,righta[v], m, r);
}
}
typedef std::pair<long long,point> plpoi;
plpoi get(int x, int v = inicio, long long l = -val, long long r = val) {
if(!v)return {-(1LL<<62LL),{-1,-1}};
long long m = (l + r) / 2LL;
if(r-l==1) {
return {f(mem[v], x),mem[v]};
} else if(x < m) {
return std::max({f(mem[v], x),mem[v]}, get(x, lefta[v], l, m));
} else {
return std::max({f(mem[v], x),mem[v]}, get(x, righta[v], m, r));
}
}
long long array[MAX];
long long pref[MAX];
point prevans[MAX];
int backtracking[MAX][205];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
for(auto&x:prevans)x={0,-1LL<<62LL};
int N,K;
std::cin>>N>>K;
for(int i=0;i!=N;++i){
std::cin>>array[i];
if(i){
pref[i]=pref[i-1];
}
pref[i]+=array[i];
}
pref[N]=1LL<<50LL;
long long resp=0;
for(int i=0;i!=K+1;++i){
clear();
add_line({0,0});
std::map<point,int> mapa;
for(int j=0;j!=N;++j){
if(j!=N-1){
add_line(prevans[j]);
mapa[prevans[j]]=j;}
long long tot = pref[j];
long long ans = 0;
if(j){
auto a=get(pref[j]);
ans=a.first;
backtracking[j][i]=mapa[a.second];
}
resp=ans;
prevans[j]={tot,-(pref[j]*tot)+ans};
}
}
std::vector<int> bck;
int cur=N-1;
for(int i=K;i!=0;--i){
int t = backtracking[cur][i];
bck.push_back(t+1);
cur=t;
}
std::reverse(bck.begin(),bck.end());
std::cout<<resp<<"\n";
for(int i=0;i!=bck.size()-1;++i)std::cout<<bck[i]<<" ";
std::cout<<bck.back()<<"\n";
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:106:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i=0;i!=bck.size()-1;++i)std::cout<<bck[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... |