CF1358D. The Best Vacation

题目链接

思路:尺取+贪心

请看注释!

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>

#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ll long long
#define endl "\n"

using namespace std;

const int N = 4e5+5;

ll d[N]={0}, dd[N]={0};

int main()
{
	ll n, x;
	
	scanf("%lld %lld",&n,&x);
	
	for(ll i=1;i<=n;++i)
	{
		scanf("%lld",&d[i]);//该月份的天数 
		
		dd[i] = d[i]*(1ll+d[i])/2ll;//某一月份全取的次数 
	}
	
	for(int i=1;i<=n;++i)//对出现下一年的情况进行预处理 
	{
		d[n+i] = d[i];
		
		dd[n+i] = dd[i];
	}
	
	ll now = 0, now1 = 0;
	
	int l = n+1, r = n+1;
	
	while(now<=x&&l>=2)//向前进行尺取,看看会到哪 
	{
		if(now+d[l]<=x)//仍旧可以取走一整月 
		{
			now += d[l];//已消耗天数增加 
			
			now1 += dd[l];//已获得 hug数增加 
			
			--l;//考虑再前面一个月份 
		}
		else
		{
			break;//退出循环,已经不能再取走一整月 
		}
	}
	
	ll re = x - now;//残余的空闲时间 
	
	ll mx = now1+(d[l]+d[l]-re+1)*re/2;//计算总共有多少hug 
	//为之前获得的所有hug加上剩余天数取得的hug 
	while(r<n+n)//向后进行尺取 
	{
		now += d[++r];//天数增加 
		
		now1 += dd[r];//拥抱增加 
		
		while(now>x)//当已消耗天数超出预期 
		{
			now -= d[++l];//消除最前面一月的影响 
			
			now1 -= dd[l];
		}
		
		ll re = x - now;
		
		mx = max(mx,now1+(d[l]+d[l]-re+1)*re/2);//更新答案 
	}
	
	cout << mx << endl;
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值