[51NOD] 1065 最小正子段和
前缀和加SET维护 O(n + nlog(n))
结果不能为零 白下了一组数据...
set中的查找:
set<#>iterator it = ST.lower_bound(x);
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
const ll INF = 2 * 0x3f3f3f3f;
ll ans = INF;
ll arr[MAXN] = {0}, brr[MAXN] = {0};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
set <ll> ST;
int n;
cin>>n;
ST.insert(0);
for(int i = 1; i <= n; ++i)
{
cin>>arr[i], brr[i] = arr[i] + brr[i - 1];
//cout<<brr[i]<<'\n';
if(arr[i] > 0)
ans = min(ans, arr[i]);
set <ll>::iterator it = ST.lower_bound(brr[i]);
while(it == ST.end() || (brr[i] - *it) <= 0)
{
if(it == ST.begin())
break;
--it;
}
if(*it <= brr[i])
ans = min(ans, brr[i] - *it);
ST.insert(brr[i]);
}
cout<<ans<<'\n';
return 0;
}