题目传送门
思路
本题 最大 ,如果暴力枚举无法通过本题,考虑二分答案。
为什么能够二分?
设 表示存在长度至少为 的连续子段。
如果有一个合法子段长度 ,那它的长度自然也 ,所以 成立。
可行长度是从 开始连续一段,满足二分的单调性条件,可以使用二分。
原式为:
我们设 表示前 项的和,即 ,其中 ,原式可变为:
拆开括号得到:
最后移项,得:
我们令 ,原条件则变为 。
尝试二分答案 ,这里我给出一种新的做法————STL 的 std::set,它的时间复杂度通常为 ( 表示当前容器内元素个数),足以 通过本题 的数据。
代码
AC Code
// Problem: P10579 [蓝桥杯 2024 国 A] 最长子段
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P10579
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// Code by wangxy0311
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
template <class T>
T randint(T l, T r) {
uniform_int_distribution<T> dist(l, r);
return dist(rnd);
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef __int128 i128;
typedef unsigned __int128 u128;
typedef long double ld;
#define rep(x, y, z) for (int x = (y); x <= (z); ++x)
#define per(x, y, z) for (int x = (y); x >= (z); --x)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fileIO(s) \
freopen(s ".in", "r", stdin); \
freopen(s ".out", "w", stdout);
#define endl '\n'
#define all(x) begin(x), end(x)
inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <typename T>
void chkmax(T &a, T b) { if (b > a) a = b; }
template <typename T>
void chkmin(T &a, T b) { if (b < a) a = b; }
template <int MOD>
struct Modint {
ll val;
Modint(ll v = 0) { val = (v % MOD + MOD) % MOD; }
Modint operator+(const Modint & x) const { return Modint(val + x.val); }
Modint operator-(const Modint & x) const { return Modint(val - x.val); }
Modint operator*(const Modint & x) const { return Modint((i128)val * x.val % MOD); }
Modint operator^(ll b) const { Modint res = 1, a = *this; for (; b; b >>= 1, a *= a) if (b & 1) res *= a; return res; }
Modint operator^(const Modint & b) const { return *this ^ b.val; }
Modint operator~() const { return *this ^ MOD - 2; }
Modint operator/(const Modint & x) const { return *this * ~x; }
Modint operator-() const { return Modint(-val); }
Modint & operator+=(const Modint & x) { val = (val + x.val) % MOD; return *this; }
Modint & operator-=(const Modint & x) { val = (val - x.val + MOD) % MOD; return *this; }
Modint & operator*=(const Modint & x) { val = (i128)val * x.val % MOD; return *this; }
Modint & operator^=(ll b) { return *this = *this ^ b; }
Modint & operator^=(const Modint & b) { return *this = *this ^ b.val; }
Modint & operator/=(const Modint & x) { return *this = *this / x; }
Modint & operator++() { return *this += Modint(1); }
Modint operator++(int) { Modint tmp = *this; ++*this; return tmp; }
Modint & operator--() { return *this -= Modint(1); }
Modint operator--(int) { Modint tmp = *this; --*this; return tmp; }
bool operator==(const Modint & x) const { return val == x.val; }
bool operator!=(const Modint & x) const { return val != x.val; }
bool operator<(const Modint & x) const { return val < x.val; }
bool operator>(const Modint & x) const { return val > x.val; }
bool operator<=(const Modint & x) const { return val <= x.val; }
bool operator>=(const Modint & x) const { return val >= x.val; }
friend Modint operator+(ll v, const Modint & x) { return Modint(v) + x; }
friend Modint operator-(ll v, const Modint & x) { return Modint(v) - x; }
friend Modint operator*(ll v, const Modint & x) { return Modint(v) * x; }
friend Modint operator/(ll v, const Modint & x) { return Modint(v) / x; }
friend Modint operator^(ll v, const Modint & x) { return Modint(v) ^ x; }
friend ostream & operator<<(ostream & os, const Modint & x) { return os << x.val; }
friend istream & operator>>(istream & is, Modint & x) { ll v; is >> v; x = Modint(v); return is; }
};
const int inf = 1e9;
const ll infll = 1e18;
const double eps = 1e-8;
const ld epsll = 1e-12;
const int N = 3e5 + 5;
ll S[N], X[N], Y[N];
ll n, a, b, c;
bool check(ll mid) {
set<pll> s; // set自动去重 & 排序
rep(R, 1, n) {
if (R >= mid) {
ll L = R - mid + 1;
s.insert({Y[L], L}); // 插入
if (s.lower_bound({X[R], 0}) != s.begin()) { // 满足条件 X[R] > Y[L]
return true;
}
}
}
return false;
}
int main() {
fast_io();
cin >> n >> a >> b >> c;
S[0] = 0; // 初始 S[0] 为 0
rep(i, 1, n) {
ll x;
cin >> x;
S[i] = S[i - 1] + x; // 前缀和
}
rep(R, 1, n) X[R] = S[R] - a * b * R; // 计算对应的X
rep(L, 1, n) Y[L] = S[L - 1] - a * c * L; // 计算对应的Y
ll l = 1, r = n, ans = -1;
while (l <= r) { // 二分
ll mid = l + r >> 1;
if (check(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans;
return 0;
} // 完结撒花时间复杂度分析
set 每次操作平均 , 次操作即 。二分查找也是 ,总的复杂度为 。
注
此方法非本题最优解,仅为正解的一种,若追求最优解考虑其他题解。