题意:
给定一个数组,将其分成若干不相交的连续子数组,求其中子数组之和大于等于0的子数组的所有元素个数的最大值。n <= 2e5,a[i] <= 1e9。
Solution:
首先,不考虑n的范围直接采用dp进行一个暴力的转移:
dp[i] = max{dp[j] + i - j}(0 < j < i && sum(i + 1 ~ j) >= 0)
区间和可以利用前缀和优化,但复杂度还是O(n^2)
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1];// 可以直接从前一个状态转移
for (int j = 0; j < i; ++j) {
if (pre[i] - pre[j] >= 0) {
dp[i] = max(dp[i], dp[j] + i - j);
}
}
}
此时,可以发现 转移方程可以进行修改
dp[i] = i + max{dp[j] - j}(0 < j < i && sum(i + 1 ~ j) >= 0)
由于i > j, 所以可以考虑用数据结构来记录这个dp[j] + j, 注意有转移条件 pre[i] >= pre[j], 可以考虑用权值线段树(其实就是用线段树维护桶,和普通线段树没太大区别)来维护信息, 每次到一个元素之后我们将先利用当前线段树里面的信息更新dp[i],再将当前dp[i] - i更新到对应的桶里面,易知dp[i] - i越大越好所以桶里面维护的是最大值。其实维护的就是n个以pre[i]为下标, 以dp[i] - i为信息的桶, 然后利用线段树来求区间最大值。每次查询的就是小于等于pre[i]区间内的dp[j] - j的最大值。
一些细节:
注意a[i]范围,然后需要离散化pre数组,还有就是在暴力dp的第二重循环中, j 是从0开始的 此时dp[j] - j = 0, 所以线段树把所有的桶的初始值设为-INF后要注意把pre[0] = 0, dp[0] - 0 = 0的情况更新进线段树, 由于每次最多只有一个pre[i]所以单点修改即可
#include <bits/stdc++.h>
using namespace std;
#define db double
#define il inline
#define fir first
#define sec second
#define eps (1e-10)
#define pb push_back
#define ll long long
#define mkp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define lowbit(a) (a & (-a))
#define SZ(a) ((int)a.size())
#define ull unsigned long long
#define all(a) a.begin(), a.end()
#define split cout << "=========\n";
#define GG { cout << "NO\n"; return; }
#define pll pair<long long, long long>
#define equals(a, b) (fabs((a) - (b)) < eps)
constexpr int ON = 0;
constexpr int CW = -1;
constexpr int CCW = 1;
constexpr int BACK = 2;
constexpr int FRONT = -2;
const db pi = acos(-1.000);
constexpr int maxn = 2e5 + 100;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3f;
constexpr int mod = 1e9 + 7; /* 998244353 */
constexpr int dir[8][2] = {-1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1};
int mx[maxn << 2];
void push_up(int p)
{
mx[p] = max(mx[p << 1], mx[p << 1 | 1]);
}
void update(int p, int x, int nl, int nr, int val)
{
if (nl == x && nr == x) {
mx[p] = max(mx[p], val);
return ;
}
int mid = nl + nr >> 1;
if (mid >= x) update(p << 1, x, nl, mid, val);
else update(p << 1 | 1, x, mid + 1, nr, val);
push_up(p);
}
int query(int p, int l, int r, int nl, int nr)
{
if (nr < l || nl > r || r < l) return -INF;
if (l <= nl && r >= nr) {
return mx[p];
}
int ans = -INF;
int mid = nl + nr >> 1;
ans = max(ans, query(p << 1, l, r, nl, mid));
ans = max(ans, query(p << 1| 1, l, r, mid + 1, nr));
return ans;
}
void solve(int cas)
{
int n; cin >> n;
vector<int> a(n + 1), dp(n + 1, 0);
vector<ll> pre(n + 1, 0), tmp;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
tmp.pb(pre[i]);
}
tmp.pb(0);
sort(all(tmp));
int len = unique(all(tmp)) - tmp.begin();
for (int i = 1; i <= n; ++i)
pre[i] = lower_bound(tmp.begin(), tmp.begin() + len, pre[i]) - tmp.begin() + 1;
for (int i = 1; i <= 4 * n; ++i) mx[i] = -INF;
int k = lower_bound(tmp.begin(), tmp.begin() + len, 0) - tmp.begin() + 1;// 0的下标
update(1, k, 1, len, 0);
for (int i = 1; i <= n; ++i) {
dp[i] = max(dp[i - 1], query(1, 1, pre[i], 1, len) + i);
update(1, pre[i], 1, len, dp[i] - i);
}
cout << dp[n] << '\n';
}
int main(int argc, char *argv[])
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve(1);
return 0;
}
也可以用树状数组维护最大值
struct BIT
{
int MX;
int tr[maxn];
void init(int n)
{
MX = n;
for (int i = 1; i <= MX; ++i) tr[i] = -INF;
}
void update(int x, int val)
{
while (x <= MX) {
tr[x] =max(val, tr[x]);
x += lowbit(x);
}
}
int query(int x)
{
int ans = -INF;
while (x) {
ans = max(ans, tr[x]);
x -= lowbit(x);
}
return ans;
}
} bit;
void solve(int cas)
{
int n; cin >> n;
vector<int> a(n + 1), dp(n + 1, 0);
vector<ll> pre(n + 1, 0), tmp;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
tmp.pb(pre[i]);
}
tmp.pb(0);
sort(all(tmp));
int len = unique(all(tmp)) - tmp.begin();
bit.init(len);
for (int i = 1; i <= n; ++i)
pre[i] = lower_bound(tmp.begin(), tmp.begin() + len, pre[i]) - tmp.begin() + 1;
int k = lower_bound(tmp.begin(), tmp.begin() + len, 0) - tmp.begin() + 1;// 0的下标
bit.update(k, 0);
for (int i = 1; i <= n; ++i) {
dp[i] = max(dp[i - 1], bit.query(pre[i]) + i);
bit.update(pre[i], dp[i] - i);
}
cout << dp[n] << '\n';
}