Submission #345632

#TimeUsernameProblemLanguageResultExecution timeMemory
345632maskoffBigger segments (IZhO19_segments)C++14
100 / 100
115 ms24416 KiB
	#include <bits/stdc++.h>
 
#define file ""
 
#define all(x) x.begin(), x.end()
 
#define sc second
#define fr first
 
#define pb push_back
#define mp make_pair
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
 
const ll inf = 1e18 + 5;
const ll mod = 1e9 + 7;
 
const int N = 5e5 + 5;
 
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};

ll p[N], a[N];
ll n;
vector<pair<ll, ll>> dp(N + 5), all;

int get(ll x) {
	int l = 0;
	int r = all.size() - 1;
	int ans = -1;
	while (l <= r) {
	 	int m = l + r >> 1;
	 	if (all[m].fr <= x) l = m + 1, ans = m;
		else r = m - 1;
	}
	if (ans == -1) return 0;
	return all[ans].sc;
}
 	

int main() {  
	ios_base :: sync_with_stdio(false);               
	cin.tie(nullptr);                                               
	srand(time(nullptr));
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	for (int i = 1; i <= n; i++) p[i] += p[i - 1] + a[i];

	for (int i = 1; i <= n; i++) {
	 	int pos = get(p[i]);
	 	//cerr << i << " " << pos << endl;
	 	dp[i].fr = dp[pos].fr + 1;
	 	dp[i].sc = p[i] - p[pos];
		while ((int)all.size() > 0 && all.back().fr >= dp[i].sc + p[i]) 
		 	all.pop_back();
		all.pb({dp[i].sc + p[i], i});
		/*for (auto t : all)
			cout << t.fr << " ";
		cout << endl; */
	}         

	/*for (int i = 1; i <= n; i++) 
	 	cout << i << ": " << dp[i].fr << " " << dp[i].sc << endl;  */
	cout << dp[n].fr;
  return 0;                   
}  

Compilation message (stderr)

segments.cpp: In function 'int get(ll)':
segments.cpp:35:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |    int m = l + r >> 1;
      |            ~~^~~
#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...