Submission #343207

# Submission time Handle Problem Language Result Execution time Memory
343207 2021-01-03T14:24:02 Z NhatMinh0208 Split the sequence (APIO14_sequence) C++14
71 / 100
574 ms 131076 KB
/*
	Normie's Template v2.0
*/
 
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
 
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
 
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
 
//Pragmas (Comment out these three lines if you're submitting in szkopul.)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
 
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define endl "\n"
 
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
 
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))

//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
 
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
 
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
 
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
 
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
//---------END-------//
struct node
{
	node *nl=0, *nr=0;
	ll dom=0;
	~node()
	{
		if (nl) delete(nl);
		if (nr) delete(nr);
	}
};
struct dynamic_li_chao
{
	vector<pii(ll)> func={{0,3e18+7}};
	ll calc (ll cur, ll x)
	{
		return (func[cur].fi)*x+(func[cur].se);
	}
	node start;
	void add_line(ll a, ll b, ll l, ll r)
	{
		func.emplace_back(a,b);
		update(-1e9,1e9+1,&start,func.size()-1,l,r);
	}
	void update(ll l, ll r, node* cur, ll diff , ll tl, ll tr)
	{
		if ((l>=tl)and(r-1<=tr))
		{
		if (cur->dom==0)
		{
			cur->dom=diff;
		}
		else if (l+1==r) 
		{
			if (calc(cur->dom,l)>calc(diff,l))
			cur->dom=diff;
		}
		else
		{
			ll mid=(l+r)/2;
			ll doml=(calc(cur->dom,l)>calc(diff,l));
			ll domm=(calc(cur->dom,mid)>calc(diff,mid));
			if (domm)
			{
				ll z=cur->dom;
				cur->dom=diff;
				diff=z;
			}
			if (doml==domm)
			{
				if (!cur->nr) cur->nr=new(node);
				update(mid,r,cur->nr,diff,tl,tr);
			}
			else
			{
				if (!cur->nl) cur->nl=new(node);
				update(l,mid,cur->nl,diff,tl,tr);
			}
		}
		}
		else if ((l>tr)or(r-1<tl)) return;
		else
		{
			ll mid=(l+r)/2;
			if (!cur->nr) cur->nr=new(node);
			update(mid,r,cur->nr,diff,tl,tr);
			if (!cur->nl) cur->nl=new(node);
			update(l,mid,cur->nl,diff,tl,tr);
		}
	}
	pii(ll) get(ll x)
	{
		return getK(-1e9,1e9+1,&start,x);
	}
	pii(ll) getK(ll l, ll r, node* cur, ll tar)
	{
		pii(ll) res1={calc(cur->dom,tar),cur->dom};
		if (l+1==r) return res1;
		pii(ll) res2={3e18+7,0};
		ll mid=(l+r)/2;
		if ((tar<mid)and(cur->nl)) res2=getK(l,mid,cur->nl,tar); 
		else if ((tar>=mid)and(cur->nr)) res2=getK(mid,r,cur->nr,tar);
		if (res1.fi<res2.fi) return res1;
		else return res2;
	}
};

ll n,k,m,i,j,t,t1,u,v,a,b;
ll  dp[100001][201];
int opt[100001][201];
ll pre[100001];
ll arr[100001];
vector<ll> res;
int main()
{
	fio;
	cin>>n>>m; m++;
	for (i=1;i<=n;i++)
	{
		cin>>arr[i];
		pre[i]=pre[i-1]+arr[i];
	}
	for (i=0;i<=n;i++) dp[i][0]=1e18;
	dp[0][0]=0;
	for (j=1;j<=m;j++)
	{
		dynamic_li_chao bruh;
		for (i=j;i<=n;i++)
		{
			bruh.add_line(-2*pre[i-1],dp[i-1][j-1]+pre[i-1]*pre[i-1],0,1e9);
			auto u=bruh.get(pre[i]);
			dp[i][j]=u.fi+pre[i]*pre[i];
			opt[i][j]=u.se+(j-2);
		}
	}
	cout<<(pre[n]*pre[n]-dp[n][m])/2<<endl;
	i=n;
	for (j=m;j>=1;j--)
	{
		i=opt[i][j];
		if (i) res.push_back(i);
	}
	for (i=m-2;i>=0;i--) cout<<res[i]<<' ';
}

Compilation message

sequence.cpp:20: warning: ignoring #pragma comment  [-Wunknown-pragmas]
   20 | #pragma comment(linker, "/stack:200000000")
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 364 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 364 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 364 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 364 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 364 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 364 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 364 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 364 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 364 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 364 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 364 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 364 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 364 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 492 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 492 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 492 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 512 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 492 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 492 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 492 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 492 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 492 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 876 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 876 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 5 ms 876 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 876 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 6 ms 876 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 7 ms 876 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 6 ms 876 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 3 ms 876 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 876 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 3 ms 876 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2796 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 3 ms 2796 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 45 ms 2796 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 3 ms 2796 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 66 ms 2924 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 74 ms 2816 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 48 ms 2796 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 52 ms 2796 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 13 ms 2924 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 22 ms 2796 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 29 ms 24800 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 25 ms 24696 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 574 ms 25000 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 23 ms 24656 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 511 ms 24732 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 574 ms 24732 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 455 ms 24828 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 426 ms 24804 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 428 ms 24844 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 537 ms 24784 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Runtime error 75 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -