Submission #741979

#TimeUsernameProblemLanguageResultExecution timeMemory
741979MODDI수열 (APIO14_sequence)C++14
100 / 100
388 ms82236 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n, k, where[201][100001];
ll pref[100001]{0}, dp[2][100001], q[100001], l = 1, r = 1;
vi arr;
bool test1(int x, int y, int i){
	return (dp[0][y] - dp[0][x] >= (pref[y] - pref[x]) * (pref[n] - pref[i]));
}
bool test2(int x, int y, int i){
	return ((dp[0][y] - dp[0][x]) * (pref[i] - pref[y]) <=
	        (dp[0][i] - dp[0][y]) * (pref[y] - pref[x]));
}
int main(){
	cin>>n>>k;
	for(int i = 1; i <= n; i++){
		int x;
		cin >> x;
		pref[i] = pref[i - 1] + x;
	}
	fill(dp[0], dp[0]+n+1, 0);
	for(int i = 1; i <= k; i++){
		q[r++] = 0;
		for(int j = 1; j <= n; j++){
			while(r-l > 1 && test1(q[l], q[l+1], j))	l++;
			ll x = q[l];
			dp[1][j] = dp[0][x] + (pref[j] - pref[x]) * (pref[n] - pref[j]);
			where[i][j] = x;
			while(r-l>1 && test2(q[r-2], q[r-1], j)) r--;
			q[r++] = j;
		}
		l = r = 1;
		for(int j = 1; j <= n; j++)	dp[0][j] = dp[1][j];
	}
	ll ans =-1;
	int idx = -1;
	for(int i = 1; i <= n; i++){
		if(dp[0][i] > ans){
			ans = dp[0][i];
			idx = i;
		}
	}
	cout<<ans<<endl;
	for(int i = 0; i < k; i++){
		cout<<idx<<" ";
		idx = where[k-i][idx];
	}
	cout<<endl;
	return 0;
}
#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...