Submission #370756

#TimeUsernameProblemLanguageResultExecution timeMemory
370756fhvirusBigger segments (IZhO19_segments)C++17
100 / 100
94 ms10220 KiB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii; typedef pair<ll, ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i = 0; i < (n); ++i)
#define FOO(i,a,b) for(int i = (a); i <= (b); ++i)
#define AI(x) (x).begin(),(x).end()
template<typename I> bool chmax(I &a, I b){ return a < b ? (a = b, true) : false;}
template<typename I> bool chmin(I &a, I b){ return a > b ? (a = b, true) : false;}
#ifdef OWO
#define debug(args...) LKJ("[ " + string(#args) + " ]", args)
void LKJ(){ cerr << endl;}
template<typename I, typename...T> void LKJ(I&&x, T&&...t){ cerr<<x<<", ", LKJ(t...);}
template<typename I> void DE(I a, I b){ while(a < b) cerr<<*a<<" \n"[next(a)==b], ++a;}
#else
#define debug(...) 0
#define DE(...) 0
#endif
#ifndef OWO
#pragma GCC optimize("Ofast")
#endif
const int N = 5e5 + 225;
int n, a[N];
ll s[N];
 
int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n; FOO(i,1,n) cin >> a[i];
	FOO(i,1,n) s[i] = s[i-1] + a[i];

	vector<int> dp(n + 1, 0);
	vector<int> mx(n + 1, 0);
	FOO(i,1,n){
		chmax(mx[i], mx[i-1]);
		dp[i] = dp[mx[i]] + 1;
		int id = lower_bound(s, s + n + 1, 2 * s[i] - s[mx[i]]) - s;
		chmax(mx[id], i);
	}

	cout << dp[n];
 
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...