Submission #230850

# Submission time Handle Problem Language Result Execution time Memory
230850 2020-05-11T22:34:41 Z Hrithik_Narang Split the sequence (APIO14_sequence) C++14
100 / 100
1323 ms 85368 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
#define pb push_back
#define For(i,s,e) for (ll i=(s); i<(e); i++)
#define Debug_array(a,n) for (ll i=(0); i<(n); i++) cout<<a[i]<<" "
#define Foe(i,s,e) for (ll i=(s); i<=(e); i++)
#define Fod(i,s,e) for (ll i=(s)-1; i>=(e); i--)
#define pii pair<ll,ll>
#define fi first
#define se second
#define endl "\n"
#define mp make_pair
#define big_prime 15486277
#define bigger_prime 179424697
#define biggest_prime 32416188691
//#define random_shuffle(indices.begin(), indices.end());
//std::random_device rd;
//std::mt19937 g(rd());
//std::shuffle(v.begin(), v.end(), g);
using namespace __gnu_pbds; 
using namespace std;
template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
// template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }
 
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
    template <class Arg1, class... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args) {
        const char* sep = strchr(names + 1, ',');
        cerr.write(names, sep - names) << " : " << arg1 << "  ";
        __f(sep + 1, args...);
    }
#else
#define trace(...) 0
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE


typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; 

#define Mod 1000000007
#define MAX 100001

int Next[MAX][201];
ll dp[MAX][2];

//CHT

// struct Line {
// 	mutable ll k, m, p;
// 	bool operator<(const Line& o) const { return k < o.k; }
// 	bool operator<(ll x) const { return p < x; }
// };

// struct LineContainer : multiset<Line, less<>> {
// 	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
// 	const ll inf = LLONG_MAX;
// 
// 	//NOTE: Here 'k' represents slope of the line and 'm' represents the y-intercept
// 	void add(ll k, ll m) {
// 		auto z = insert({k, m, 0}), y = z++, x = y;
// 		while (isect(y, z)) z = erase(z);
// 		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
// 		while ((y = x) != begin() && (--x)->p >= y->p)
// 			isect(x, erase(y));
// 	}
// 	//Querying for the max value of 'x' in all linear functions
// 	ll query(ll x) {
// 		assert(!empty());
// 		auto l = *lower_bound(x);
// 		return l.k * x + l.m;
// 	}
// };
struct Line
{
	ll m,c;
	Line(ll m,ll c) : m(m) , c(c) {}
};

ll DIV(ll a, ll b) { // floored DIVision
	return a / b - ((a ^ b) < 0 && a % b); }

ll get_intersection(Line one,Line two){
	ll num = (two.c-one.c);
	ll den = (one.m-two.m);
	return DIV(num,den);
}

bool can_remove(Line x,Line y,Line z) {
	if(get_intersection(x,z) > get_intersection(y,z)){
		return 1;
	}else return 0;	
}

void add(deque<pair<Line,int> >& s,ll m,ll c,int idx){
	if(s.size()==0){
		s.push_back({Line(m,c),idx});
		return;
	}
	if(s.back().fi.m==m){
		if(s.back().fi.c>c) return;
		else{
			s.pop_back();
			s.push_back({Line(m,c),idx});
			return;
		}
	}
	if(s.size()==1){
		s.push_back({Line(m,c),idx});
		return;
	}
	Line x(m,c);
	while(s.size()>1){
		int y_idx = s.back().se;
		Line y = s.back().fi;s.pop_back();
		Line z = s.back().fi;
		if(can_remove(x,y,z)){
			continue;
		}else{
			s.push_back({y,y_idx});
			break;
		}
	}
	s.push_back({x,idx});
	return;
}

pair<ll,int> query(deque<pair<Line,int> >& s,ll pt){
	// ll maxi = 0;
	// for(Line l:s){
	// 	maxi = max(maxi,l.m*pt+l.c);
	// }
	// return maxi;

	assert(s.size()!=0);
	while(s.size()>1){
		int x_idx = s.front().se;
		Line x = s.front().fi;s.pop_front();
		Line y = s.front().fi;
		if(x.m*pt+x.c > y.m*pt+y.c){
			s.push_front({x,x_idx});
			break;
		}else{
			continue;
		}
	}
	// ll eval = s.front().fi.m*m+s.fi.front
	// return mp(0,0);
	return {s.front().fi.m*pt+s.front().fi.c,s.front().se};
}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	memset(Next,-1,sizeof(Next));

	int n,K;cin>>n>>K;
	ll a[n],p[n];
	For(i,0,n) {
		cin>>a[i];
		p[i] = a[i]+(i>0?p[i-1]:0);
	}

	vector<pii> to_add(n);
	Fod(i,n-1,0) {
		to_add[i] = {p[i]-p[n-1],p[i]*(p[n-1]-p[i])};
	}

	Foe(k,1,K){
		deque<pair<Line,int> > s;

		Fod(i,n-1,0){
			add(s,to_add[i].fi,to_add[i].se,i);

			ll P = (i>0 ? p[i-1] : 0);
			pair<ll,int> ans = query(s,P);
			dp[i][k%2] = ans.fi;
			Next[i][k] = ans.se;

			to_add[i]={p[i]-p[n-1],dp[i+1][k%2]+p[i]*(p[n-1]-p[i])};
		}
	}

	cout<<dp[0][K%2]<<endl;
	vector<ll> final;
	ll start = 0;
	while(K>0){
		start = Next[start][K]+1;
		final.pb(start);
		K--;
	}
	for(ll x:final){
		cout<<x<<" ";
	}
	cout<<endl;



























	
}
# Verdict Execution time Memory Grader output
1 Correct 44 ms 78968 KB contestant found the optimal answer: 108 == 108
2 Correct 44 ms 78968 KB contestant found the optimal answer: 999 == 999
3 Correct 43 ms 78968 KB contestant found the optimal answer: 0 == 0
4 Correct 44 ms 78968 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 44 ms 78968 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 43 ms 78984 KB contestant found the optimal answer: 1 == 1
7 Correct 43 ms 78976 KB contestant found the optimal answer: 1 == 1
8 Correct 43 ms 78968 KB contestant found the optimal answer: 1 == 1
9 Correct 43 ms 78976 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 43 ms 78968 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 44 ms 78968 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 43 ms 79096 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 44 ms 78976 KB contestant found the optimal answer: 140072 == 140072
14 Correct 43 ms 78968 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 44 ms 78968 KB contestant found the optimal answer: 805 == 805
16 Correct 43 ms 78976 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 45 ms 78968 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 43 ms 78968 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 43 ms 78968 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 44 ms 79104 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 43 ms 78976 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 43 ms 78968 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 45 ms 78968 KB contestant found the optimal answer: 933702 == 933702
7 Correct 44 ms 78972 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 44 ms 79096 KB contestant found the optimal answer: 687136 == 687136
9 Correct 45 ms 78976 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 43 ms 78968 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 43 ms 78968 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 42 ms 78968 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 46 ms 79096 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 43 ms 78968 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 44 ms 78968 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 45 ms 78968 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 44 ms 78968 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 44 ms 78976 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 44 ms 78968 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 45 ms 78972 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 44 ms 79096 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 43 ms 79104 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 55 ms 79096 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 44 ms 79096 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 57 ms 79096 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 54 ms 79096 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 55 ms 79096 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 53 ms 79096 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 45 ms 79096 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 47 ms 79100 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 46 ms 79668 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 46 ms 79608 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 161 ms 79608 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 45 ms 79608 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 109 ms 79608 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 125 ms 79608 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 142 ms 79612 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 118 ms 79584 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 131 ms 79608 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 154 ms 79608 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 67 ms 84856 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 68 ms 84860 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1323 ms 84984 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 77 ms 85112 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1201 ms 84984 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 952 ms 84856 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 1051 ms 85368 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 858 ms 85240 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 958 ms 85112 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 1217 ms 85112 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845