제출 #533920

#제출 시각아이디문제언어결과실행 시간메모리
533920hollwo_pelwSplit the sequence (APIO14_sequence)C++17
100 / 100
1424 ms80480 KiB
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
 
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
 
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
 
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(filein.c_str(), "r", stdin);
		freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
 
void Hollwo_Pelw();
 
signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}
 
const int N = 1e5 + 5, K = 202;
 
int n, k, pre[N], tr[K][N];
 
int64_t dp[2][N];
 
inline bool chkmax(int64_t &a, int64_t b) {
	return a < b ? a = b, 1 : 0;
}
 
int64_t calc(int l, int r) {
	return 1ll * (pre[r] - pre[l]) * (pre[n] - pre[r]);
}
 
void solve(int lvl, int l, int r, int opl, int opr) {
	if (l > r) return ;
	int m = l + r >> 1, opm = -1;
	
	dp[lvl & 1][m] = -1e18;
	for (int i = opl; i <= min(m - 1, opr); i++)
		if (chkmax(dp[lvl & 1][m], dp[lvl & 1 ^ 1][i] + calc(i, m))) opm = i;
	
	tr[lvl][m] = opm;

	solve(lvl, l, m - 1, opl, opm);
	solve(lvl, m + 1, r, opm, opr);
}
 
void Hollwo_Pelw() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
		cin >> pre[i], pre[i] += pre[i - 1];
 
 	// if (n <= 200) {
 	// 	for (int i = 1; i <= k; i++) {
 	// 		memset(dp[i & 1], -0x3f, sizeof dp[i & 1]);
 	// 		for (int j = i; j <= n; j++)
 	// 			for (int x = i - 1; x < j; x++)
 	// 				if (chkmax(dp[i & 1][j], dp[i & 1 ^ 1][x] + cost(x, j))) tr[i][j] = x;
 	// 	}
 	// }

	for (int i = 1; i <= k; i++)
		solve(i, i, n - 1, i - 1, n - 1);
 
	int64_t res = -1e18, cur = 0;
	for (int i = k; i < n; i++)
		if (chkmax(res, dp[k & 1][i])) cur = i;
	
	cout << res << '\n';
	if (k == n - 1) {
		for (int i = 1; i < n; i++)
			cout << i << ' ';
	} else {
		for (; k; cur = tr[k --][cur])
			cout << cur << ' ';
	}
}

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'void solve(int, int, int, int, int)':
sequence.cpp:65:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   65 |  int m = l + r >> 1, opm = -1;
      |          ~~^~~
sequence.cpp:69:37: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   69 |   if (chkmax(dp[lvl & 1][m], dp[lvl & 1 ^ 1][i] + calc(i, m))) opm = i;
      |                                 ~~~~^~~
sequence.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
sequence.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...