Submission #1218558

#TimeUsernameProblemLanguageResultExecution timeMemory
1218558banganRadio Towers (IOI22_towers)C++20
0 / 100
4086 ms1836 KiB
#include "towers.h"
#include <bits/stdc++.h>
using namespace std;

#define chmax(a, b) a = max(a, b)

#define pb push_back

const int maxn = 1e5 + 4;

int n;
int h[maxn];
int dp[maxn];
int pre[maxn];

int local_min(int i) {
	if (1 <= i-1 && h[i] > h[i-1]) return 0;
	if (i+1 <= n && h[i] > h[i+1]) return 0;
	return 1;
}

void init(int N, std::vector<int> H) {
	n = N;
	for (int i=1; i<=n; i++) h[i] = H[i-1];
	for (int i=1; i<=n; i++) pre[i] = pre[i-1] + local_min(i);
}

int max_towers(int L, int R, int D) {
	if (L==R) return 1;
	L++; R++;
	vector<int> stk{L};
	{
		int mx = 0;
		for (int i = 1; i <= R; i++) {
			if (h[i] <= mx-D && h[stk.back()] <= mx-D) stk.pb(i), mx=0;
			else if (h[i] < h[stk.back()]) {
				stk.pop_back();
				stk.pb(i);
				mx=0;
			}
			else chmax(mx, h[i]);
		}
	}
	int ans = 0;
	for (int i : stk) if (i >= L) ans++;
	if (ans == 0) return 1;
	int f = *lower_bound(stk.begin(), stk.end(), L);
	for (int i = f-1, mx=0; i >= L; i--) {
		if (h[i] <= mx+D && h[f] <= mx+D) return ans+1;
		chmax(mx, h[i]);
	}
	return ans;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...