题目
题意:
给你 n n n个月,每个月 a i a_i ai天,你总共有 x x x天的假期,对于每一个月的 d a y − t h day-th day−th天,如果是在假期中,你可以拥有 d a y day day个拥抱,现在问你最多的拥抱是多少(假期的话是连续的)。
思路:
因为对于每一个月肯定是越到后面拥抱越多,所以对于每一个月,肯定尽量从最后的开始,然后往前,这样的话,就可以 k k k直接加上 a i a_i ai,然后往前计算天数是否可行,我们让 k ≥ x k\geq x k≥x中的 k k k最小,那么 k − x k-x k−x就是多余的天数了,因为假期都是往前的所以多余的部分就是 ( k − x ) ∗ ( k − x + 1 ) / 2 (k-x)*(k-x+1)/2 (k−x)∗(k−x+1)/2。举个例子吧,我们有区间 3 , 4 3,4 3,4,然后我们要选择 5 5 5个那么应该就是 3 , 1 , 2 , 3 , 4 3,1,2,3,4 3,1,2,3,4这五天,那么我们模拟一下,就是此时的 k = a 0 + a 1 k=a_0+a_1 k=a0+a1然后会多出第一个月 1 , 2 1,2 1,2然后我们用 t m p = k − x = 2 tmp=k-x=2 tmp=k−x=2,那么答案就是 { 1 , 2 , 3 , 1 , 2 , 3 , 4 } − { 1 , 2 } \{1,2,3,1,2,3,4\}-\{1,2\} {1,2,3,1,2,3,4}−{1,2},如果此时的 x x x只有 2 2 2的时候,那么当 k = a 0 + a 1 k=a_0+a_1 k=a0+a1的时候,多了一个 a 0 a_0 a0这个区间,这一部分是没有用的,因为 x x x只有两天,那么我们判断 k − a l ≥ x k-a_l\geq x k−al≥x,这样保证了 k k k最小。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1:1;
ret = (c == '-') ? 0:(c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 2e5 + 10;
ll a[maxn << 1];
int main() {
ll n, x;
read(n), read(x);
for (int i = 0; i < n; i++) {
read(a[i]);
a[n + i] = a[i];
}
n <<= 1;
ll Max = 0, l = 0, r = 0, sum = 0, k = 0;
while (r < n) {
k += a[r];
sum += a[r] * (a[r] + 1) / 2;
if (k >= x) {
while (k - a[l] >= x) {
k -= a[l];
sum -= a[l] * (a[l] + 1) / 2;
l++;
}
ll tmp = k - x;
Max = max(Max, sum - (tmp * (tmp + 1) / 2));
}
r++;
}
outl(Max);
return 0;
}