Submission #562989

#TimeUsernameProblemLanguageResultExecution timeMemory
562989penguin133Split the sequence (APIO14_sequence)C++14
71 / 100
298 ms131072 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
pair<int, int> dp[205][100005];
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].first = (P[n] - P[i])*P[i];
	for(int i=2;i<=k;i++){
		for(int j=i;j<=n-(k-i)+1;j++){
			ins(P[j-1] + P[n], -P[n] * P[j-1] + dp[i-1][j-1].first, j-1);
			pair<int,int>x = query(P[j]);
			dp[i][j] = {x.first-  P[j] * P[j], x.second};
		}
		dq.clear();
	}
	int maxi = -1e9, in;
	for(int i=1;i<=n;i++)if(dp[k][i].first > maxi)maxi = dp[k][i].first, in = i;
	cout << maxi << '\n';
	cout << in << " ";
	int p = dp[k][in].second;
	while(p)cout << p << " ", k--, p = dp[k][p].second;
	/*
	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:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   29 | main(){
      | ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:46:6: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |  int p = dp[k][in].second;
      |      ^
#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...