Submission #987834

# Submission time Handle Problem Language Result Execution time Memory
987834 2024-05-23T16:31:59 Z Gray Split the sequence (APIO14_sequence) C++17
33 / 100
81 ms 131072 KB
#include <algorithm>
#include <bits/stdc++.h>

#define ll long long
#define ld double
#define ff first
#define ss second
#define ln "\n"

using namespace std;
const ll MOD = 1e9+7;
const unsigned long long INF = 1e19;

struct line{
	ll m, c;
	pair<ll, ll> id;
	line(ll m, ll c, pair<ll, ll> id):m(m),c(c),id(id){}
	ll eval(ll x){
		return m*x+c;
	}
	long double inter(const line &alt){
		return (ld)(c-alt.c)/(ld)(alt.m-m);
	}
};

struct CHT{
	deque<line> lc;
	void add(ll m, ll c, ll i, ll j){
		while (lc.size()>1 and lc.front().inter(line(m, c, {i, j}))>=lc[1].inter(lc.front())) lc.pop_front();
		lc.push_front(line(m, c, {i, j}));
	}
	pair<ll, pair<ll, ll>> query(ll x){
		while (lc.size()>1 and lc.back().eval(x)<=lc[lc.size()-2].eval(x)) lc.pop_back();
		return {lc.back().eval(x), lc.back().id};
	}
};

// dp[i] = dp[j]+(suff[j]-suff[i])*suff[i];
// dp[i] = dp[j]+suff[j]*suff[i]-suff[i]*suff[i];

void solve(){
	ll n, k; cin >> n >> k;
	vector<ll> a(n);
	vector<ll> suff(n+1, 0);
	for (ll i=0; i<n; i++) {
		cin >> a[i];
	}
	for (ll i=n-1; i>=0; i--){
		suff[i]=suff[i+1]+a[i];
	}
	vector<vector<ll>> dp(n, vector<ll>(k+1));
	vector<vector<pair<ll, ll>>> con(n, vector<pair<ll, ll>>(k+1));
	vector<CHT> chts(k+1);
	chts[0].add(suff[0], 0, -1, -1);
	ll mx = -1;
	pair<ll, ll> mxi = {-1, -1};
	for (ll i=1; i<n; i++){
		for (ll j=min(i, k); j>=1; j--){
			auto ret = chts[j-1].query(suff[i]);
			dp[i][j] = ret.ff-suff[i]*suff[i];
			con[i][j] = ret.ss;
			chts[j].add(suff[i], dp[i][j], i, j);
			if (j==k and mx<dp[i][j]){
				mx=dp[i][j];
				mxi = {i, j};
			}
		}
	}
	cout << mx << ln;
	vector<ll> ans;
	while (make_pair(-1LL, -1LL)!=mxi){
		ans.push_back(mxi.ff);
		mxi = con[mxi.ff][mxi.ss];
	}
	for (ll i=k-1; i>=0; i--){
		cout << ans[i] << " ";
	}
	cout << ln;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	ll t=1;
	// cin >> t;
	while (t--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 348 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 348 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 348 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 348 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 0 ms 348 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 348 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 348 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 348 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 348 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 348 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 596 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 348 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 348 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 348 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 348 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 348 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 348 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 348 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 348 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 348 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 600 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 348 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 348 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 348 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 600 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 348 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 2 ms 1624 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 344 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 1 ms 1116 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 2 ms 1372 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 2 ms 1372 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 604 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 604 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 604 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 600 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 7 ms 5512 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 600 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 8 ms 5464 KB contestant found the optimal answer: 679388326 == 679388326
6 Incorrect 8 ms 4952 KB contestant didn't find the optimal answer: 4699030135 < 4699030287
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2908 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 3 ms 3164 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 81 ms 50400 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 4 ms 2908 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 69 ms 32064 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Incorrect 67 ms 36180 KB contestant didn't find the optimal answer: 514786325281 < 514790755404
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 25172 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 30 ms 25436 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Runtime error 68 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -