The Best Vacation CodeForces - 1358D(贪心+尺取)

You’ve been in love with Coronavirus-chan for a long time, but you didn’t know where she lived until now. And just now you found out that she lives in a faraway place called Naha.

You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly x days and that’s the exact number of days you will spend visiting your friend. You will spend exactly x consecutive (successive) days visiting Coronavirus-chan.

They use a very unusual calendar in Naha: there are n months in a year, i-th month lasts exactly di days. Days in the i-th month are numbered from 1 to di. There are no leap years in Naha.

The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get j hugs if you visit Coronavirus-chan on the j-th day of the month.

You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan).

Please note that your trip should not necessarily begin and end in the same year.

Input
The first line of input contains two integers n and x (1≤n≤2⋅105) — the number of months in the year and the number of days you can spend with your friend.

The second line contains n integers d1,d2,…,dn, di is the number of days in the i-th month (1≤di≤106).

It is guaranteed that 1≤x≤d1+d2+…+dn.

Output
Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.

Examples
Input
3 2
1 3 1
Output
5
Input
3 6
3 3 3
Output
12
Input
5 6
4 2 3 1 3
Output
15
Note
In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) {1,1,2,3,1}. Coronavirus-chan will hug you the most if you come on the third day of the year: 2+3=5 hugs.

In the second test case, the numbers of the days are {1,2,3,1,2,3,1,2,3}. You will get the most hugs if you arrive on the third day of the year: 3+1+2+3+1+2=12 hugs.

In the third test case, the numbers of the days are {1,2,3,4,1,2,1,2,3,1,1,2,3}. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you 2+3+1+2+3+4=15 times.
思路:挺明显的一道尺取的题目,但是呢,又不能全部展开,因为那样必定超时。我们可以发现,如果当前我们选定的范围值[l,r](第l个数到第r个数),那么怎么选择是最优的呢?就是选择这个区间里最后一段长度为x的序列,这样是最优的(可以自行证明)。因为类似于一个环形,那么我们可以在数组后面再增加一个数组,这样就可以线性处理了,例如数组为 a,b,c,d,我们将之变为a,b,c,d,a,b,c,d.这样所有的情况就有了。
代码如下:

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

const int maxx=4e5+100;
int a[maxx];
ll sum[maxx],sum1[maxx];
int n;ll x;

int main()
{
	scanf("%d%lld",&n,&x);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=n+1;i<=2*n;i++) a[i]=a[i-n];
	sum[0]=sum1[0]=0;
	for(int i=1;i<=2*n;i++) sum[i]=sum[i-1]+a[i];
	for(int i=1;i<=2*n;i++) sum1[i]=sum1[i-1]+(1ll+(ll)a[i])*(ll)a[i]/2ll;
	int l=1,r=1;
	ll num=0;
	ll _max=0;
	while(r<=2*n)
	{
		while(num<x&&r<=2*n) num+=a[r],r++;
		if(num<x) break;
		ll ans=0;
		ans+=(sum1[r-1]-sum1[l]);//这样我们就选取的最后一段长度为x的序列,但是这样有可能多,有可能少,分情况讨论。
		if(sum[r-1]-sum[l]>x) 
		{
			ll xx=sum[r-1]-sum[l]-x;
			ans-=(1ll+xx)*xx/2ll;
		}
		else if(sum[r-1]-sum[l]<x)
		{
			ll xx=x-sum[r-1]+sum[l];
			xx=(ll)a[l]-xx+1ll;
			ans+=(xx+(ll)a[l])*((ll)a[l]-xx+1ll)/2ll;
		}
		_max=max(_max,ans);
		num-=a[l];
		l++;
	}
	cout<<_max<<endl;
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值