Submission #624368

# Submission time Handle Problem Language Result Execution time Memory
624368 2022-08-08T04:13:27 Z vovamr Split the sequence (APIO14_sequence) C++17
100 / 100
617 ms 90744 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

struct line {
	ll k, b; int id;
	line() : k(0), b(0), id(0) {}
	line(ll k, ll b, int id) : k(k), b(b), id(id) {}
	inline pll operator()(const ll &x) { return mpp(k * 1ll * x + b, id); }
};
inline bool bad(const line &a, const line &b, const line &c) {
	return (ld)(a.b - c.b) * (b.k - a.k) <= (ld)(c.k - a.k) * (a.b - b.b);
}
const int N = 3e5 + 100;
int sz; line s[N];
inline void add(line c) {
	if (!sz) return void(s[sz++] = c);
	if (s[sz - 1].k == c.k) {
		if (s[sz - 1].b <= c.b) return;
		else --sz;
	}
	while (sz >= 2) {
		const line &a = s[sz - 2];
		const line &b = s[sz - 1];
		if (bad(a, b, c)) --sz;
		else break;
	} s[sz++] = c;
}
int ptr = 0;
inline pll get(const ll x) {
	chmin(ptr, sz - 1);
	while (ptr + 1 < sz && s[ptr + 1](x) < s[ptr](x)) ++ptr;
	return s[ptr](x);
}

inline void solve() {
	int n, k;
	cin >> n >> k;
	ve<int> a(n);
	for (auto &i : a) cin >> i;

	ve<ll> s(n);
	for (int i = 0; i < n; ++i) {
		s[i] = a[i];
		if (i) s[i] += s[i - 1];
	}
	ve<ll> sq(n);
	for (int i = 0; i < n; ++i) {
		sq[i] = a[i] * a[i];
		if (i) sq[i] += sq[i - 1];
	}

	ve<ve<int>> p(k + 2, ve<int>(n));

	ve<ll> dp(n);
	for (int i = 0; i < n; ++i) dp[i] = s[i] * s[i] - sq[i];

	for (int z = 2; z <= k + 1; ++z) {
		sz = ptr = 0; ve<ll> f(n, inf);
		for (int i = z - 2; i < n; ++i) {
			// dp[z][i] =    min  ((s[i] - s[j])^2 - (sq[i] - sq[j]) + dp[j])
			// 		j < i
			// = s[i]^2 - sq[i] + min(-2s[j] * s[i] + s[j]^2 + dp[j] + sq[j])
			if (sz) { pll c = get(s[i]); f[i] = c.fi + s[i] * s[i] - sq[i]; p[z][i] = c.se; };
			if (dp[i] != inf) add(line(-2 * s[i], s[i] * s[i] + dp[i] + sq[i], i));
		}
//		cout << "layer " << z << ":" << '\n';
//		for (int i = 0; i < n; ++i) {
//			if (f[i] > 1e9) cout << -1 << " ";
//			else cout << f[i] << " ";
//		}
//		cout << '\n' << '\n';
		dp.swap(f);
	}
	cout << (s[n - 1] * s[n - 1] - sq[n - 1] - dp[n - 1]) / 2 << '\n';

	ve<int> ans;
	int x = k + 1, y = n - 1;

	while (x > 1) {
		ans.pb(p[x][y] + 1);
		tie(x, y) = mpp(x - 1, p[x][y]);
	}

	reverse(all(ans));
	for (auto i : ans) cout << i << " ";
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
/*
7 3
4 1 3 4 0 2 3
*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB contestant found the optimal answer: 108 == 108
2 Correct 4 ms 7252 KB contestant found the optimal answer: 999 == 999
3 Correct 3 ms 7252 KB contestant found the optimal answer: 0 == 0
4 Correct 3 ms 7252 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 3 ms 7252 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 3 ms 7252 KB contestant found the optimal answer: 1 == 1
7 Correct 3 ms 7252 KB contestant found the optimal answer: 1 == 1
8 Correct 3 ms 7380 KB contestant found the optimal answer: 1 == 1
9 Correct 4 ms 7252 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 3 ms 7252 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 3 ms 7252 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 4 ms 7252 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 3 ms 7252 KB contestant found the optimal answer: 140072 == 140072
14 Correct 4 ms 7252 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 3 ms 7252 KB contestant found the optimal answer: 805 == 805
16 Correct 3 ms 7252 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 3 ms 7252 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7252 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 4 ms 7252 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 4 ms 7380 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 4 ms 7252 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 5 ms 7252 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 3 ms 7252 KB contestant found the optimal answer: 933702 == 933702
7 Correct 3 ms 7380 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 3 ms 7380 KB contestant found the optimal answer: 687136 == 687136
9 Correct 3 ms 7252 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 3 ms 7252 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 3 ms 7380 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 4 ms 7508 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 3 ms 7380 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 3 ms 7380 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 4 ms 7520 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 4 ms 7508 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 3 ms 7380 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 3 ms 7380 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 4 ms 7380 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7432 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 3 ms 7380 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 7 ms 8148 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 3 ms 7380 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 9 ms 8312 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 11 ms 8020 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 10 ms 8148 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 8 ms 8172 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 5 ms 7508 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 7 ms 7636 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7892 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 5 ms 7892 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 42 ms 15572 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 5 ms 7892 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 40 ms 12500 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 38 ms 13268 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 46 ms 13652 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 38 ms 12500 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 35 ms 13320 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 44 ms 14804 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 19 ms 12876 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 19 ms 12884 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 418 ms 90744 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 19 ms 13516 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 617 ms 90432 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 374 ms 66872 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 417 ms 71080 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 282 ms 60068 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 310 ms 67028 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 428 ms 82744 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845