这是一道人类智慧题。
首先符合条件的 不会大于 ,原因是 符合条件意味着序列中一定能找到一个长度为 的区间。因此考虑从大往小枚举 ,第一个合法的即是答案。
如何判断合不合法呢?我们充分发扬人类智慧,枚举所有长度为 的区间,只要区间中的和与平方和都与恰好 个 ,恰好 个 ,恰好 个 ,,恰好 个 的和与平方和对应就认为这是对的。随便前缀和维护即可,复杂度 。
恰好 个 ,恰好 个 ,恰好 个 ,,恰好 个 的和为 ,平方和为
Code
#include <bits/stdc++.h>
const long long IMX = 1ll << 30;
const long long LMX = 1ll << 60;
const long long MOD1 = 998244353;
const long long MOD2 = 1000000007;
const long long MOD3 = 1000000009;
using ll = long long;
using i128 = __int128;
using ld = long double;
using f128 = __float128;
namespace xvl_ {
#define SP(n, x) std :: setprecision(n) << std :: fixed << x
#define DEBUG(x) std :: cerr << #x << " = " << x << '\n'
#define fst first
#define snd second
template <typename T> T Max(T a, T b) { return a > b ? a : b; } template <typename T, typename... Args> T Max(T a, Args... args) { return a > Max(args...) ? a : Max(args...); }
template <typename T> T Min(T a, T b) { return a < b ? a : b; } template <typename T, typename... Args> T Min(T a, Args... args) { return a < Min(args...) ? a : Min(args...); }
}
using namespace std;
using namespace xvl_;
const int N = 100005;
int T, n, ans;
ll a[N], sum1[N], sum2[N];
bool check(ll x) {
for (int i = 1; i + x * (x + 1) / 2 - 1 <= n; i++) {
if (sum1[i + x * (x + 1) / 2 - 1] - sum1[i - 1] == x * (x + 1) * (2 * x + 1) / 6 and sum2[i + x * (x + 1) / 2 - 1] - sum2[i - 1] == x * x * (x + 1) * (x + 1) / 4) return 1;
}
return 0;
}
int main() {
// freopen("InName.in", "r", stdin);
// freopen("OutName.out", "w", stdout);
ios :: sync_with_stdio(0);
cin.tie(nullptr);
cin >> T;
while (T--) {
ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) { cin >> a[i]; sum1[i] = sum1[i - 1] + a[i]; sum2[i] = sum2[i - 1] + a[i] * a[i]; }
for (int i = sqrt(2 * n); i >= 1; i--) {
if (check(i)) { ans = i; break; }
}
cout << ans << '\n';
}
return 0;
}