Submission #48733

#TimeUsernameProblemLanguageResultExecution timeMemory
48733zadrgaSplit the sequence (APIO14_sequence)C++14
71 / 100
2065 ms88572 KiB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 100111
#define maxk 211

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

struct line{
	ll k, n;
	int ind;
};

line env[maxn];
int best_ind, top, bot;

bool bad(int l1, int l2, int l3){
	return (env[l3].n - env[l1].n) * (env[l1].k - env[l2].k) <= (env[l2].n - env[l1].n) * (env[l1].k - env[l3].k);
}

void add(line a){
	if(top - bot > 0 && env[top - 1].k == a.k){
		if(a.n > env[top - 1].n)
			top--;
		else
			return;
	}

	env[top] = a;
	while(top - bot >= 3 && bad(top - 2, top - 1, top)){
		env[top - 1] = env[top];
		top--;
	}
	top++;
}

ll val(int ind, ll x){
	return env[ind].k * x + env[ind].n;
}

pair<ll, int> query(ll x){
	if(best_ind >= top)
		best_ind = top - 1;

	while(best_ind + 1 < top && val(best_ind, x) <= val(best_ind + 1, x))
		best_ind++;

	return mp(val(best_ind, x), env[best_ind].ind);
}


ll dp[maxn][2], pre[maxn], arr[maxn];
int from[maxn][maxk];

ll sum(int i, int j){
	if(i > j)
		return 0LL;

	return pre[j] - pre[i - 1];
}

int main(){
	int n, k;
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; i++){
		scanf("%lld", arr + i);
		pre[i] = pre[i - 1] + arr[i];
	}

	for(int i = 0; i <= n; i++){
		for(int j = 0; j < 2; j++)
			dp[i][j] = -INF;
	}

	for(int i = 1; i <= n - 1; i++)
		dp[i][1] = sum(1, i) * sum(i + 1, n);

	for(int j = 2; j <= k; j++){
		top = bot = 0;
		best_ind = 0;

		int jj = j % 2;
		int prev = 1 - jj;

		for(int i = j; i <= n - 1; i++){
			add({sum(1, i - 1), dp[i - 1][prev] - sum(1, i - 1) * sum(1, n), i - 1});
			
			pair<ll, int> cur = query(sum(1, i));
			dp[i][jj] = cur.fi + sum(i + 1, n) * sum(1, i);
			from[i][j] = cur.se;
		}
	}

	int ans = 0;
	for(int i = 1; i <= n - 1; i++){
		if(dp[i][k % 2] > dp[ans][k % 2])
			ans = i;
	}

	printf("%lld\n", dp[ans][k % 2]);
	for(int i = k; i >= 1; i--){
		printf("%d ", ans);
		ans = from[ans][i];
	}

	return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", arr + i);
   ~~~~~^~~~~~~~~~~~~~~~~
#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...