# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
553032 | mars4 | Split the sequence (APIO14_sequence) | C++17 | 1128 ms | 83396 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ff first
#define ss second
#define ll int64_t
#define ld long double
#define nl cout<<"\n"
#define i128 __int128_t
#define all(v) v.begin(),v.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define forn(i,a,b) for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b) for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mod 1'000'000'007
#define mod2 998'244'353
#define inf 1'000'000'000'000'007
#define pi 3.14159265358979323846
template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;
template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p) {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v) {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s) {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m) {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}
void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail>
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//---------------------------------mars4---------------------------------
const ll N=100'000;
const ll K=200;
ll a[N];
ll pre[N+1];
int v[K][N];
vector<ll> dp;
ll sz;
ll sum(ll l,ll r)
{
return pre[r+1]-pre[l];
}
class CHT
{
struct Line
{
ll m;
ll c;
ll ind;
Line(ll m,ll c,ll ind):m(m),c(c),ind(ind) {}
ll eval(ll x)
{
return m*x+c;
}
};
ld intersectX(Line a,Line b)
{
return (ld)(a.c-b.c)/(b.m-a.m);
}
public:
deque<Line> cv_hull;
void update(ll m,ll c,ll ind)
{
Line l=Line(m,c,ind);
ll n=(ll)cv_hull.size();
while(n>=2 and intersectX(l,cv_hull[0])>=intersectX(cv_hull[0],cv_hull[1]))
{
cv_hull.pop_front();
n--;
}
cv_hull.push_front(l);
}
pair<ll,ll> query(ll x,ll ind)
{
ll n=(ll)cv_hull.size();
while(n>=2 and cv_hull[n-1].eval(x)<=cv_hull[n-2].eval(x) and cv_hull[n-2].ind<=ind)
{
cv_hull.pop_back();
n--;
}
return {cv_hull[n-1].eval(x),cv_hull[n-1].ind};
}
};
int main()
{
fastio();
ll z,n,m,t,k,i,j,l,d,h,r;
cin>>n>>k;
sz=n;
dp=vector<ll>(n);
forn(i,0,n)
{
cin>>a[i];
pre[i+1]=pre[i]+a[i];
}
// dp[i][k]=dp[j][k]+(sum(j+1,n-1)-sum(r+1,n-1))*sum(r+1,n-1)
// dp[i][k]=dp[j][k-1]+sum(j+1,n-1)*sum(r+1,n-1)-sum(r+1,n-1)*sum(r+1,n-1);
// y=mx+c
// m=sum(j+1,n-1), x=sum(r+1,n-1)
// m is decreasing, x is decreasing
CHT cht;
cht.update(sum(0,n-1),0,-1);
forn(j,0,k)
{
fill(all(dp),(ll)0);
forn(i,j,n-1)
{
ll rsum=sum(i+1,n-1);
auto [val,ind]=cht.query(rsum,i);
dp[i]=val-rsum*rsum;
v[j][i]=ind;
}
cht=CHT();
forn(i,j+1,n-1)
{
cht.update(sum(i,n-1),dp[i-1],i);
}
}
pair<ll,ll> mval={-1,-1};
forn(i,0,n-1)
{
mval=max(mval,{dp[i],i});
}
vector<ll> ans;
ll ind=mval.ss;
forb(i,k-1,0)
{
ans.push_back(ind);
ind=v[i][ind]-1;
}
reverse(all(ans));
cout<<mval.ff<<"\n";
for(ll i:ans)
{
cout<<i+1<<" ";
}
nl;
cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
return 0;
}
Compilation message (stderr)
# | 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... |