Submission #391042

#TimeUsernameProblemLanguageResultExecution timeMemory
391042CSQ31Split the sequence (APIO14_sequence)C++14
50 / 100
2086 ms131076 KiB
#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const ll is_query = -9223372036854775807;
struct line{
	ll m,c,id = 0;//y = mx+c
	line(ll a,ll b,ll d){m = a;c = b;id=d;}
	ll eval(ll x){return m*x+c;}
	ld interx(line l) {return (ld)((l.c - c)/(m - l.m));}
	mutable function<const line*()> succ; //succesor
	bool operator<(const line &r)const{
		if(r.c != is_query)return r.m > m; 
		const line* s = succ();
        if (!s) return 0;
        ll x = r.m;
        return 1.0L * c - s->c < 1.0L *(s->m - m) * x; //is x greater than the intersection point of s and succ?
                                                       //if yes then we move forward ez
	}
};
struct chull{
	set<line>h;
	bool bad(set<line>::iterator y){
		auto z = next(y);
		if(y == h.begin()){  //y is the first line in hull
			if(z == h.end())return 0;
			return y->m == z->m && y->c <= z->c; //are you parallel?
		}
		auto x = prev(y);
		if(z == h.end())return y->m == x->m && x->c <= y->c; //if y is last line also check paralell
		return (x->c - y->c)*1.0L*(z->m - y->m) >= (y->c - z->c)*1.0L*(y->m - x->m); //check if isect(x,y) > isect(y,z)
		                                                                             //if yes,diu diao you
		
	}
	void add(line l){
		auto y = h.insert(l).fi;
		y->succ = [=] { return next(y) == h.end() ? 0 : &*next(y); }; //y's succsesor
		if(bad(y)){h.erase(y);return;}
		while(next(y) != h.end() && bad(next(y)))h.erase(next(y));  //diu diao bu hao der
		while(y != h.begin() && bad(prev(y)))h.erase(prev(y));
	}
	PII query(ll x){
		auto it = *h.lb(line(x,is_query,0));
		return {it.eval(x),it.id};
	}
	
};
int main()
{
	owo
	int n,k;
	cin>>n>>k;
	vector<ll>a(n);
	for(int i=0;i<n;i++)cin>>a[i];
	VII dp(n,vector<ll>(k+1)),par(n,vector<ll>(k+1));
	vector<chull>hulls(k+1);
	hulls[0].add(line(0,0,-1));
	
	vector<ll>p = a;
	for(int i=1;i<n;i++)p[i]+=p[i-1];
	for(int i=0;i<n-1;i++){
		for(int j=k;j>=1;j--){
			if(hulls[j-1].h.empty())continue;
			PII v = hulls[j-1].query(p[n-1]-p[i]);
			dp[i][j] = v.fi + p[i]*(p[n-1] - p[i]);
			par[i][j] = v.se;
			//cout<<i<<" "<<j<<" "<<v.se<<'\n';
			hulls[j].add(line(-p[i],dp[i][j],i));
		}
	}
	ll ans = 0;
	for(int i=0;i<n-1;i++){ans = max(ans,dp[i][k]);}
	//cout<<'\n';
	cout<<ans<<'\n';
	vector<int>seq;
	for(int i=0;i<n;i++){
		if(dp[i][k] == ans){
			int cur = i;
			for(int j=k;j>=1;j--){
				seq.pb(cur);
				cur = par[cur][j];
			}
			break;
		}
	}
	reverse(all(seq));
	for(int x:seq)cout<<x+1<<" ";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...