Submission #1160467

#TimeUsernameProblemLanguageResultExecution timeMemory
1160467akniet0Bigger segments (IZhO19_segments)C++20
13 / 100
1 ms400 KiB
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define len(F) (int)F.size()
#define ull unsigned long long
#define F first
#define S second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define int long long
#define ld long double
#define pii pair<int,int>
#define mii map<int,int>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

const int N = 5e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const ll inf = 1e9;

int n,a[N],p[N];
vector <pair<int,pii>> dp;

signed main(){
	// freopen("tFt.in", "r", stdin);
	// freopen("tFt.out", "w", stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i=1;i<=n;i++){
		cin >> a[i];
		p[i] = p[i-1] + a[i];
	}
	dp.pb({1, {a[1], 1}});
	for (int i=2;i<=n;i++){
		int l = 0;
		int r = len(dp) - 1;
		while(l<=r){
			int md = (l + r) / 2;
			if (dp[md].S.F <= p[i] - p[dp[md].S.S]){
				l = md + 1;
			}
			else{
				r = md - 1;
			}
		}
		int sum = 0,x = 0;
		if (r < 0){
			if (!dp.empty()) x = dp.back().F;
			if (!dp.empty()) sum = dp.back().S.F + a[i];
		}
		else{
			x = dp[r].F + 1;
			sum = p[i] - p[dp[r].S.S];
		}
		while(!dp.empty() && dp.back().S.F > sum){
			dp.popb();
		}
		dp.pb({x, {sum, i}});
	}
	cout << dp.back().F;
}

//order_of_key(k): Number of items strictly smaller than k .
//find_by_order(k): K-th element in a set (counting from zero).

//S of squares n*(n+1)*(2n+1)/6
//S of cubes [n*(n+1)/2]^2
//S of squares for odds n*(4*n*n-1)/3
//S of cubes for odds n*n*(2*n*n-1)

//a/b%mod = a*(b^(m-2)%mod)
//(a>>F)&1 == 0
//a^b = (a+b)-2(a&b)

//srand(time(0))-always changing

Compilation message (stderr)

segments.cpp:4:30: warning: '-fwhole-program' is not an option that controls warnings [-Wpragmas]
    4 | #pragma GCC diagnostic error "-fwhole-program"
      |                              ^~~~~~~~~~~~~~~~~
segments.cpp:5:30: warning: '-fcse-skip-blocks' is not an option that controls warnings [-Wpragmas]
    5 | #pragma GCC diagnostic error "-fcse-skip-blocks"
      |                              ^~~~~~~~~~~~~~~~~~~
segments.cpp:6:30: warning: '-funsafe-loop-optimizations' is not an option that controls warnings [-Wpragmas]
    6 | #pragma GCC diagnostic error "-funsafe-loop-optimizations"
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...