Submission #392198

# Submission time Handle Problem Language Result Execution time Memory
392198 2021-04-20T15:53:11 Z BorisBarca Split the sequence (APIO14_sequence) C++14
71 / 100
512 ms 17060 KB
/*
$$$$$$$\                      $$\           $$$$$$$\
$$  __$$\                     \__|          $$  __$$\
$$ |  $$ | $$$$$$\   $$$$$$\  $$\  $$$$$$$\ $$ |  $$ | $$$$$$\   $$$$$$\   $$$$$$$\ $$$$$$\
$$$$$$$\ |$$  __$$\ $$  __$$\ $$ |$$  _____|$$$$$$$\ | \____$$\ $$  __$$\ $$  _____|\____$$\
$$  __$$\ $$ /  $$ |$$ |  \__|$$ |\$$$$$$\  $$  __$$\  $$$$$$$ |$$ |  \__|$$ /      $$$$$$$ |
$$ |  $$ |$$ |  $$ |$$ |      $$ | \____$$\ $$ |  $$ |$$  __$$ |$$ |      $$ |     $$  __$$ |
$$$$$$$  |\$$$$$$  |$$ |      $$ |$$$$$$$  |$$$$$$$  |\$$$$$$$ |$$ |      \$$$$$$$\\$$$$$$$ |
\_______/  \______/ \__|      \__|\_______/ \_______/  \_______|\__|       \_______|\_______|
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define PB push_back
#define MP make_pair
#define INS insert
#define LB lower_bound
#define UB upper_bound
#define pii pair <int,int>
#define pll pair <long long, long long>
#define X first
#define Y second
#define _ << " " <<
#define sz(x) (int)x.size()
#define all(a) (a).begin(),(a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i > (b); --i)
#define FORA(i, x) for (auto &i : x)
#define REP(i, n) FOR(i, 0, n)
#define BITS(x) __builtin_popcount(x)
#define SQ(a) (a) * (a)
#define TRACE(x) cout << #x " = " << (x) << '\n';
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define umap unordered_map

typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <pii> vpi;
typedef vector <ll> vll;
typedef vector <pll> vpl;
typedef vector <string> vs;

const int MOD = 1e9 + 7;
const double PI = acos(-1);
const int INF = 1e9 + 10;
const ll INFL = 1e18 + 10;
const int ABC = 30;

inline int sum(int a, int b){
	if (a + b < 0)
		return (a + b + MOD) % MOD;
	return (a + b) % MOD;
}

inline void add(int &a, int b){
	a = sum(a, b);
}

inline int mul(ll a, ll b){
	return ((a % MOD) * ((ll)b % MOD)) % MOD;
}

inline int sub(int a, int b){
	return (a - b + MOD) % MOD;
}

inline int fast_pot(ll pot, ll n){
	ll ret = 1;
	while (n){
		if (n & 1LL)
			ret = (ret * pot) % MOD;
		pot = (pot * pot) % MOD;
		n >>= 1LL;
	}
	return ret;
}

const int N = 2e5 + 10;

ll n, k, a[N], pref[N], dp[2][N], ans[202][10005];

ll get(int l, int r){
	return pref[r] - (l ? pref[l - 1] : 0);
}
/*
ll dp(int i, int j){
	if (i == -1){
		if (j == 0)
			return 0;
		return -INFL;
	}
	if (j == 0)
		return 0;
	ll &ret = memo[i][j];
	if (~ret)
		return ret;
	for (int l = i; l >= 0; --l)
		ret = max(ret, dp(l - 1, j - 1) + get(l, i) * get(0, l - 1));
	return ret;
}

void reconstruct(int i, int j){
	if (i == -1)
		return;
	if (j == 0)
		return;
	for (int l = i; l >= 0; --l){
		if (dp(l - 1, j - 1) + get(l, i) * get(0, l - 1) == dp(i, j)){
			reconstruct(l - 1, j - 1);
			cout << l << ' ';
			return;
		}
	}
}*/


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

struct LineContainer : multiset<Line, less<>> {
	ll div(ll a, ll b) {
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y){
		if (y == end()) return x->p = INFL, 0;
		if (x->k == y->k) x->p = x->m > y->m ? INFL : -INFL;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m, ll pos) {
		auto z = insert({k, m, 0, pos}), y = z++, x = y;
		while (isect(y, z)) y = 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));
	}
	pll query(ll x){
		assert(!empty());
		auto l = *lower_bound(x);
		return {l.k * x + l.m, l.idx};
	}
};



int main () {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;
	FOR(i, 1, n + 1){
		cin >> a[i];
		pref[i] = (i ? pref[i - 1] : 0) + a[i];
	}
	/*memset(memo, -1, sizeof memo);
	cout << dp(n - 1, k) << '\n';	
	reconstruct(n - 1, k);
	cout << '\n';*/

	LineContainer cht;
	
	REP(i, n + 1)
		REP(j, 2)
			dp[j][i] = -INFL;

	dp[0][0] = 0;
	FOR(i, 1, n + 1)
		dp[1][i] = 0;
		
	FOR(j, 2, k + 2){
		int x = j & 1;
		dp[0][0] = -INFL;
		cht.add(pref[j - 1], dp[x ^ 1][j - 1] - SQ(pref[j - 1]), j - 1);
		FOR(i, j, n + 1){
			dp[x][i] = cht.query(pref[i]).X;
			ans[j][i] = cht.query(pref[i]).Y;
			//cout << j _ i _ ans[j][i] << '\n';
			cht.add(pref[i], dp[x ^ 1][i] - SQ(pref[i]), i);
		}
		FOR(i, 0, n + 1)
			dp[x ^ 1][i] = -INFL; 
		cht.clear();
	}

	cout << dp[(k & 1) ^ 1][n] << '\n';
	
	int idx = n;
	stack <int> s;
	for (int i = k + 1; i > 1; --i){
		s.push(ans[i][idx]);
		idx = ans[i][idx];
	}
	while (sz(s)){
		int x = s.top();
		cout << x << ' ';
		s.pop();
	}
	cout << '\n';


	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 332 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 332 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 332 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 332 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 332 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 332 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 332 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 332 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 332 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 332 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 332 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 332 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 332 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 332 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 332 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 332 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 332 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 460 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 332 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 332 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 332 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 460 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 332 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 332 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 332 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 332 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 3 ms 1228 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 332 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 3 ms 972 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 3 ms 1228 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 4 ms 1228 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 2 ms 592 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 460 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 2 ms 588 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 460 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 29 ms 2636 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 460 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 31 ms 2600 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 26 ms 2252 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 31 ms 2648 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 29 ms 2628 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 8 ms 844 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 14 ms 1356 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1484 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 7 ms 1484 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 512 ms 17060 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 10 ms 1612 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 259 ms 10656 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 282 ms 12388 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 321 ms 13104 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 281 ms 10908 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 299 ms 12356 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 380 ms 15348 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Incorrect 99 ms 10588 KB declared answer doesn't correspond to the split scheme: declared = 19795776960, real = 19793779801
2 Halted 0 ms 0 KB -