Yet Another Yet Another Task CodeForces - 1359D(单调栈+线段树)

Alice and Bob are playing yet another card game. This time the rules are the following. There are n cards lying in a row in front of them. The i-th card has value ai.

First, Alice chooses a non-empty consecutive segment of cards [l;r] (l≤r). After that Bob removes a single card j from that segment (l≤j≤r). The score of the game is the total value of the remaining cards on the segment (al+al+1+⋯+aj−1+aj+1+⋯+ar−1+ar). In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is 0.

Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.

What segment should Alice choose so that the score is maximum possible? Output the maximum score.

Input
The first line contains a single integer n (1≤n≤105) — the number of cards.

The second line contains n integers a1,a2,…,an (−30≤ai≤30) — the values on the cards.

Output
Print a single integer — the final score of the game.

Examples
Input
5
5 -2 10 -1 4
Output
6
Input
8
5 2 5 3 -30 -30 6 9
Output
10
Input
3
-10 6 -15
Output
0
Note
In the first example Alice chooses a segment [1;5] — the entire row of cards. Bob removes card 3 with the value 10 from the segment. Thus, the final score is 5+(−2)+(−1)+4=6.

In the second example Alice chooses a segment [1;4], so that Bob removes either card 1 or 3 with the value 5, making the answer 5+2+3=10.

In the third example Alice can choose any of the segments of length 1: [1;1], [2;2] or [3;3]. Bob removes the only card, so the score is 0. If Alice chooses some other segment then the answer will be less than 0.
思路:根据题意,我们可以想得到的是Bob肯定设法减去的是Alice选取区间的最大值,这样看来,我们就可以枚举每一个位置的数,看看它在哪一段区间是最大值。这就用到了单调栈。对于第i个数字来说,它的左右区间分别是[L,R].那么我们就求[L,i-1]的最大值和[i+1,R]的最大值(如果有负数就取0),然后相加得到最大值。
线段树中,我们存储的是前缀和与后缀和,这样就需要开两棵线段树。然后更新的时候需要分前缀还是后缀这样讨论。具体看代码吧。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
struct node{
	int l,r,lazy,_max;
}p1[maxx<<2],p2[maxx<<2];
int a[maxx];
int L[maxx],R[maxx],suml[maxx],sumr[maxx];
int n;

inline void pushup(int cur,node p[])
{
	p[cur]._max=max(p[cur<<1]._max,p[cur<<1|1]._max);
}
inline void pushdown(int cur,node p[])
{
	if(p[cur].lazy)
	{
		p[cur<<1]._max+=p[cur].lazy;
		p[cur<<1|1]._max+=p[cur].lazy;
		p[cur<<1].lazy+=p[cur].lazy;
		p[cur<<1|1].lazy+=p[cur].lazy;
		p[cur].lazy=0;
	}
}
inline void build(int l,int r,int cur,node p[])
{
	p[cur].l=l;
	p[cur].r=r;
	p[cur].lazy=p[cur]._max=0;
	if(l==r) return ;
	int mid=l+r>>1;
	build(l,mid,cur<<1,p);
	build(mid+1,r,cur<<1|1,p);
}
inline void update(int l,int r,int cur,node p[],int v)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r)
	{
		p[cur].lazy+=v;
		p[cur]._max+=v;
		return ;
	}
	pushdown(cur,p);
	int mid=L+R>>1;
	if(r<=mid) update(l,r,cur<<1,p,v);
	else if(l>mid) update(l,r,cur<<1|1,p,v);
	else update(l,mid,cur<<1,p,v),update(mid+1,r,cur<<1|1,p,v);
	pushup(cur,p);
}
inline int query(int l,int r,int cur,node p[])
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r) return p[cur]._max;
	pushdown(cur,p);
	int mid=L+R>>1;
	if(r<=mid) return query(l,r,cur<<1,p);
	else if(l>mid) return query(l,r,cur<<1|1,p);
	else return max(query(l,mid,cur<<1,p),query(mid+1,r,cur<<1|1,p));
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	stack<int> s;
	while(s.size()) s.pop();
	for(int i=1;i<=n;i++)
	{
		while(s.size()&&a[s.top()]<=a[i]) s.pop();
		if(s.size()==0) L[i]=1;
		else L[i]=s.top()+1;
		s.push(i);
	}
	while(s.size()) s.pop();
	for(int i=n;i>=1;i--)
	{
		while(s.size()&&a[s.top()]<=a[i]) s.pop();
		if(s.size()==0) R[i]=n;
		else R[i]=s.top()-1;
		s.push(i);
	}
	suml[0]=sumr[n+1]=0;
	for(int i=1;i<=n;i++) suml[i]=suml[i-1]+a[i];
	for(int i=n;i>=1;i--) sumr[i]=sumr[i+1]+a[i];
	build(1,n,1,p1);
	build(1,n,1,p2);
	for(int i=1;i<=n;i++) update(i,i,1,p1,suml[i]);
	for(int i=n;i>=1;i--) update(i,i,1,p2,sumr[i]);
	int l,r,ans1,ans2,ans=0;
	for(int i=1;i<=n;i++)
	{
		l=L[i];
		r=R[i];
		ans1=ans2=0;
		if(i+1<=r)
		{
			update(i+1,r,1,p1,-suml[i]);
			ans1=max(query(i+1,r,1,p1),ans1);
			update(i+1,r,1,p1,suml[i]);
		}
		if(i-1>=l)
		{
			update(l,i-1,1,p2,-sumr[i]);
			ans2=max(query(l,i-1,1,p2),ans2);
			update(l,i-1,1,p2,sumr[i]);
		}
		ans=max(ans,ans1+ans2);
	}
	printf("%d\n",ans);
	return 0;
}

在这里插入图片描述
有什么别的好思路希望路过的大佬指点。
努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值