题目链接:https://codeforces.ml/problemset/problem/1358/D
转自:https://www.cnblogs.com/stelayuri/p/12971546.html
题意:一年有n个月,每个月有d【i】天,每天可以获得的价值为该天是当前月份的第几天,求一段连续天数x使得总价值最大。
思路:可以想到是尺取,如果把每个月份都展开是肯定不行的。 注意到最优的选取方案肯定会选到某个月的最后一天(假如没有选到一个月的最后一天,那么可以将边界向右平移到最后一天,这样比原先一定要优),那么就可以倒序枚举,预处理每个月可获得的价值总和。如果x>0,继续向前枚举。如果x<0,需要用等差数列公式减去多算的部分。
#include <bits/stdc++.h>
using namespace std;
long long d[200005],sum[1000005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for(int i=1; i<=1000000; i++)
sum[i]=sum[i-1]+i;
long long n,x;
cin>>n>>x;
for(int i=1; i<=n; i++)
cin>>d[i];
long long ans=0,tep=0;
int p=n;
for(int i=n; i>0; i--)
{
while(x>0)
{
x-=d[p];
tep+=sum[d[p]];
p--;
if(p==0)
p=n;
}
ans=max(ans,tep-(1-x)*(-x)/2);
tep-=sum[d[i]];
x+=d[i];
}
cout<<ans<<endl;
return 0;
}