当前位置: 代码迷 >> 综合 >> D. Optimal Partition(dp + 树状数组维护)
  详细解决方案

D. Optimal Partition(dp + 树状数组维护)

热度:111   发布时间:2023-11-22 03:34:55.0

通过树状数组维护时候,记得添加初始元素0,使得维护元素为空的情况(也就是代表前缀和是s[0]) 

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define PII pair<int,int>
using namespace std;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-8;
const int N=5e5+5;
int n,cnt,dp[N],c[N];
ll a[N],pre[N];
set<ll> st;
map<ll,int> id;
void add(int x,int k)
{while(x<=n)c[x]=max(c[x],k),x+=(x&-x);
}
int query(int x)
{int cnt=-inf;while(x>0)cnt=max(cnt,c[x]),x-=(x&-x);return cnt;
}int main()
{std::ios::sync_with_stdio(false);int t;cin >> t;while(t--){cin >> n;cnt = 0;st.clear();id.clear();for(int i = 1 ; i <= n;i++){c[i] = -inf;dp[i] = -inf;}st.insert(0);for(int i = 1 ; i <= n ;i++){cin >> a[i];pre[i] = pre[i - 1] + a[i];st.insert(pre[i]);}for(ll p : st){id[p] = ++cnt;}add(id[0],0);	//代表空元素for(int i = 1 ; i <= n ; i++){if(a[i] == 0) dp[i] = dp[i - 1];else if(a[i] < 0) dp[i] = dp[i - 1] - 1;dp[i] = max(dp[i],query(id[pre[i]] - 1) + i);add(id[pre[i]],dp[i] - i);}cout << dp[n] << endl;}return 0;}

  相关解决方案