Submission #563004

#TimeUsernameProblemLanguageResultExecution timeMemory
563004penguin133Split the sequence (APIO14_sequence)C++14
0 / 100
15 ms5204 KiB

#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[2][100005];
int pre[100005][205];
int P[100005], A[100005];
deque<pair<pair<int, int> , int> > dq;
int f(pair<int, int> x, int y){
	return x.first*y + x.second;
}
long double intersect(int a, int b, int c, int d){
	return (long double)(d-b)/(a-c);
}
void ins(int m , int c, int in){
	while(dq.size() > 1){
		int s = dq.size();
		if(intersect(dq.back().first.first, dq.back().first.second,m,c) <= intersect(dq[s-2].first.first, dq[s-2].first.second, m,c))dq.pop_back();
		else break;
	}
	dq.push_back(make_pair(make_pair(m,c), in));
} 
 
pair<int, int> query(int x){
	while(dq.size() > 1){
		if(f(dq[0].first, x) < f(dq[1].first, x))dq.pop_front();
		else break;
	}
	return {f(dq[0].first, x), dq[0].second};
}
main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int n,k;cin >> n >> k;
	for(int i=1;i<=n;i++)cin >> A[i], P[i] = P[i-1] + A[i];
	for(int i=1;i<=n;i++)dp[1][i] = (P[n] - P[i])*P[i];
	for(int i=2;i<=k;i++){
		int tmp = i %2;
		for(int j=i;j<=n-(k-i)+1;j++){
			ins(P[j-1] + P[n], -P[n] * P[j-1] + dp[1-tmp][j-1], j-1);
			pair<int,int>x = query(P[j]);
			dp[j][tmp] = x.first-  P[j] * P[j];
			pre[i][j] = x.second;
		}
		dq.clear();
	}
	int maxi = -1e9, in;
	for(int i=1;i<=n;i++)if(dp[k%2][i] > maxi)maxi = dp[k%2][i], in = i;
	cout << maxi << '\n';
	cout << in << " ";
	int p = pre[in][k];
	while(p)cout << p << " ", k--, p = pre[p][k];
	/*
	cout << "\n\n";
	for(int i=1;i<=k;i++){
		for(int j=i;j<=n;j++)cout << dp[i][j].first << " " << dp[i][j].second << " ";
		cout << '\n';
	}
	*/
}

Compilation message (stderr)

sequence.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   31 | main(){
      | ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:50:6: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |  int p = pre[in][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...