[codeforces 1358D] The Best Vacation 有个数限制的最大连续子段和

Codeforces Round #645 (Div. 2)  参与排名人数14819

[codeforces 1358D]    The Best Vacation   有个数限制的最大连续子段和

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1358/problem/D

ProblemLangVerdictTimeMemory
D - The Best Vacation GNU C++17Accepted78 ms1800 KB

样例模拟如下

3 2
1 3 1
位置1 2 3 4 5 6 7 8 9 10
数值1 1 2 3 1 1 1 2 3 1

位置1开始1+1=2
位置2开始1+2=3
位置3开始2+3=5
位置4开始3+1=4


3 6
3 3 3
位置1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
数值1 2 3 1 2 3 1 2 3  1  2  3  1  2  3  1  2  3

位置1开始1+2+3+1+2+3=12
位置2开始2+3+1+2+3+1=12
位置3开始3+1+2+3+1+2=12
位置4开始1+2+3+1+2+3=12

5 6
4 2 3 1 3
位置1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
数值1 2 3 4 1 2 1 2 3  1  1  2  3  1  2  3  4  1  2  1  2  3  1  1  2  3

位置1开始(1+2+3+4)+(1+2)=13
位置2开始2+3+4+(1+2)+1=13
位置3开始3+4+(1+2)+(1+2)=13
位置4开始4+(1+2)+(1+2+3)=13
位置5开始(1+2)+(1+2+3)+(1)=10
位置6开始2+(1+2+3)+(1)+1=10
位置7开始(1+2+3)+(1)+(1+2)=10
位置8开始2+3+(1)+(1+2+3)=12
位置9开始3+(1)+(1+2+3)+1=11
位置10开始(1)+(1+2+3)+1+2=10
位置11开始(1+2+3)+1+2+3=12
位置12开始2+3+(1+2+3+4)=15
位置13开始3+(1+2+3+4)+1=14
位置14开始(1+2+3+4)+(1+2)=13
位置15开始2+3+4+(1+2)+1=13

AC代码如下

#include <cstdio>
#include <algorithm>
#define maxn 200010
#define LL long long
using namespace std;
LL cnt,sum,ans;
int d[maxn<<1];
LL cal(int l,int r){
	return (LL)(l+r)*(r-l+1)/2;
}
int main(){
	int n,i,l=0;
	LL x;
	scanf("%d%lld",&n,&x);
	for(i=1;i<=n;i++)scanf("%d",&d[i]),d[i+n]=d[i];
	for(i=1;i<=2*n;i++){
		cnt+=d[i],sum+=cal(1,d[i]);
		while(cnt-d[l]>=x)cnt-=d[l],sum-=cal(1,d[l]),l++;//目标x<=cnt&&cnt-d[l]<x
		if(cnt<x)continue;//连续个数还不够x
		ans=max(ans,sum-cal(1,cnt-x));
	}
	printf("%lld\n",ans);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值