Codeforces 660F:Bear and Bowling 4

博客内容描述了如何在给定一系列保龄球得分的情况下,通过取消一些前缀和后缀来最大化总得分的问题。解决方案涉及到计算前缀和,找到使总得分最大的子序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F. Bear and Bowling 4
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record!

For rolling a ball one gets a score — an integer (maybe negative) number of points. Score for the i-th roll is multiplied by i and scores are summed up. So, for k rolls with scores s1, s2, ..., sk, the total score is . The total score is 0 if there were no rolls.

Limak made n rolls and got score ai for the i-th of them. He wants to maximize his total score and he came up with an interesting idea. He can say that some first rolls were only a warm-up, and that he wasn't focused during the last rolls. More formally, he can cancel any prefix and any suffix of the sequence a1, a2, ..., an. It is allowed to cancel all rolls, or to cancel none of them.

The total score is calculated as if there were only non-canceled rolls. So, the first non-canceled roll has score multiplied by 1, the second one has score multiplied by 2, and so on, till the last non-canceled roll.

What maximum total score can Limak get?

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the total number of rolls made by Limak.

The second line contains n integers a1, a2, ..., an (|ai| ≤ 107) — scores for Limak's rolls.

Output

Print the maximum possible total score after cancelling rolls.

Examples
input
6
5 -1000 1 -3 7 -8
output
16
input
5
1000 1000 1001 1000 1000
output
15003
input
3
-60 -70 -80
output
0
Note

In the first sample test, Limak should cancel the first two rolls, and one last roll. He will be left with rolls 1,  - 3, 7 what gives him the total score 1·1 + 2·( - 3) + 3·7 = 1 - 6 + 21 = 16.


题意是给出一组数。可以去掉部分前缀和后缀,要最终求得的数组,其1*val[1]+2*val[2]+3*val[3]...+n*val[n]值最大。

记pre[i]为val[i]的前缀和。pre2[i]为每一个数值乘以所在位置的值的前缀和。

发现sum(i,j)=pre2[j] - pre2[i-1] - (i-1)*(pre[j] - pre[i-1])。所求的即sum(i,j)的最大值。

然后从小到大枚举右端点j,往前面找i,使得这个i满足,-(i-1)*pre[j]+ (i-1)*pre[i-1]- pre2[i-1]这个值最大。相当于求(-pre[j],1)与(i-1,(i-1)*pre[i-1]-pre2[i-1])的点积。所以可知直接维护前面每一个数据的(i,i*pre[i]-pre2[i])的凸壳。之后二分查找。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef long double ld;

#define INF 0x333f3f3f
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))

const ll mod = 1e9 + 7;
const int maxn = 2e5 + 5;
const double PI = acos(-1.0);

ll n, le, ri;
ll val[maxn], pre[maxn], pre2[maxn], que[maxn];

double sl(int j, int k)
{
	double dy = (j*pre[j] - pre2[j]) - (k*pre[k] - pre2[k]);
	double dx = (j - k);

	return dy / dx;
}

int sear(ll x)
{
	int le2 = le, ri2 = ri;
	int mid;
	while (le2 < ri2 - 1)
	{
		mid = (le2 + ri2) / 2;
		if (sl(que[mid], que[mid - 1])>x)
			le2 = mid;
		else
			ri2 = mid;
	}
	return que[le2];
}

void solve()
{
	ll i, j;
	ll res = 0;
	scanf("%I64d", &n);

	memset(pre, 0, sizeof(pre));
	memset(pre2, 0, sizeof(pre2));

	for (i = 1; i <= n; i++)
	{
		scanf("%I64d", &val[i]);
		pre[i] = pre[i - 1] + val[i];
		pre2[i] = pre2[i - 1] + val[i] * i;
	}
	
	le = 0, que[ri] = 0, ri++;
	
	for (i = 1; i <= n; i++)
	{
		j = sear(pre[i]);
		res = max(res, pre2[i] - pre2[j] - j*(pre[i] - pre[j]));
		while (le + 1 < ri&&sl(que[ri - 1], que[ri - 2]) < sl(i, que[ri - 1]))
			ri--;
		que[ri] = i;
		ri++;
	}
	printf("%I64d\n", res);
}

int main()
{
#ifndef ONLINE_JUDGE  
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif
	
	solve();
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值