Submission #541046

#TimeUsernameProblemLanguageResultExecution timeMemory
541046mansurSplit the sequence (APIO14_sequence)C++17
100 / 100
1006 ms110320 KiB
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
 
#include<bits/stdc++.h>	
 
using namespace std;
 
#define all(a) a.begin(), a.end()                                                   
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
//#define int long long               
#define pb push_back
#define vt vector
#define s second
#define f first
#define nl '\n'
 
using ll = long long;
using pii = pair<ll, ll>;

 
vt<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
 
const int inf = 1e9, N = 1e6 + 5, mod = 1e9 + 7;                    
 
double eps = 1e-6;
 
bool fl = 0;
 
ll ntr(pii a, pii b) {
	ll x = b.f - a.f;
	ll y = a.s - b.s;
	return (x + y - 1) / y;
}
 
void solve() {
	int n, k;
	cin >> n >> k;          	
	ll p[n + 1];
	int pr[n + 1][k + 1];
	bool is[n + 1][k + 1];	
	p[0] = 0;
	for (int i = 1, a; i <= n; i++) {
		cin >> a;
		p[i] = p[i - 1] + a;
	}
	ll dp[2][k + 1];
	for (int s = 1; s <= k; s++) dp[0][s] = -inf;
	dp[0][0] = 0;
	vt<pair<pii, pii>> s;
	s.pb({{0, -1}, {0, 0}});
	for (int j = 0; j <= k; j++) {
		int l = 0;
		vt<pair<pii, pii>> h;
		for (int i = j + 1; i <= n; i++) {
			while (l < sz(s) && s[l].f.s < i && s[l].f.f <= p[i]) l++;
			dp[1][j] = max(dp[0][j], s[l - 1].s.f + s[l - 1].s.s * p[i]);
			if (dp[1][j] == dp[0][j]) {
				pr[i][j] = i - 1;
				is[i][j] = 0;
			}else {
				pr[i][j] = s[l - 1].f.s;
				is[i][j] = 1;
			}
			dp[0][j] = dp[1][j];
			ll a = dp[1][j] - p[i] * p[i], b = p[i];
			ll ps = 0;
			if (sz(h) && b == h.back().s.s) {
				if (a > h.back().s.f) {
				 	h.pop_back();
				 	while (sz(h) && ntr({a, b}, h.back().s) <= h.back().f.f) h.pop_back();
				 	if (sz(h)) ps = ntr({a, b}, h.back().s);
					h.pb({{ps, i}, {a, b}});
				}
				continue;
			}
			while (sz(h) && ntr({a, b}, h.back().s) <= h.back().f.f) h.pop_back();
			if (sz(h)) ps = ntr({a, b}, h.back().s);
			h.pb({{ps, i}, {a, b}});
		}
		swap(s, h);
	}
	cout << dp[0][k] << nl;
	vt<int> ans;
	int i = n, j = k;                                                             
	while (j > 0) {
		if (!is[i][j]) {
			i = pr[i][j];
			continue;
		}
		i = pr[i][j];
		j--;
		ans.pb(i);
	}
	for (int x: ans) cout << x << ' ';
}   
 
main() {                                                         
	//freopen("input.txt", "r", stdin);                                                                                     
	//freopen("output.txt", "w", stdout);                                                                                     
	ios_base::sync_with_stdio(NULL);                                                                                       
	cin.tie(NULL);
	int tp = 1;
	if (fl) cin >> tp;
	while (tp--) solve();
}

Compilation message (stderr)

sequence.cpp: In function 'void solve()':
sequence.cpp:56:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |    while (l < sz(s) && s[l].f.s < i && s[l].f.f <= p[i]) l++;
      |             ^
sequence.cpp: At global scope:
sequence.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   98 | main() {
      | ^~~~
#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...