Submission #446086

#TimeUsernameProblemLanguageResultExecution timeMemory
446086aryan12Split the sequence (APIO14_sequence)C++17
50 / 100
2090 ms31844 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

void Solve() {
	int n, k;
	cin >> n >> k;
	int a[n + 1];
	a[0] = 0;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] += a[i - 1];
	}
	int dp[n + 1][k + 1];
	int comefrom[n + 1][k + 1];
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j <= k; j++) {
			dp[i][j] = -1;
			comefrom[i][j] = -1;
		}
	}
	dp[0][0] = 0;
	int ans = -1, pos;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= k; j++) {
			if(j > i) {
				continue;
			}
			for(int prev = 0; prev < i; prev++) {
				if(dp[prev][j - 1] != -1 && dp[i][j] < dp[prev][j - 1] + (a[i] - a[prev]) * (a[n] - a[i])) {
					dp[i][j] = max(dp[i][j], dp[prev][j - 1] + (a[i] - a[prev]) * (a[n] - a[i]));
					comefrom[i][j] = prev;
				}
			}
			if(j == k) {
				if(ans < dp[i][k]) {
					pos = i;
				}
				ans = max(ans, dp[i][k]);
			}
		}
	}
	cout << ans << "\n";
	while(k != 0) {
		cout << pos << " ";
		pos = comefrom[pos][k--];
	}
	cout << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'void Solve()':
sequence.cpp:48:7: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   48 |   pos = comefrom[pos][k--];
      |   ~~~~^~~~~~~~~~~~~~~~~~~~
#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...