Submission #1218765

#TimeUsernameProblemLanguageResultExecution timeMemory
1218765banganRadio Towers (IOI22_towers)C++20
23 / 100
4069 ms1948 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++;
	int save;
	vector<int> stk{1};
	{
		int mx = 0;
		for (int i = 2; i <= n; 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]);
			if (i == R) save = stk.back();
		}
	}
	int ans = 0;
	for (int i : stk) if (L<=i && i<=R) ans++;
	// if (ans == 0) {
	// 	return 1;
	// 	for (int i=L; i<=R; i++) {
	// 		for (int j=i+1, mx=0; j<=R; j++) {
	// 			if (h[i] <= mx-D && h[j] <= mx-D) return 2;
	// 			chmax(mx, h[j]);
	// 		}
	// 	}
	// 	return 1;
	// }
	save = min_element(h+L, h+R+1) - h;
	int f, l;
	if (ans == 0) f = save, l = save, ans++;
	else f = *lower_bound(stk.begin(), stk.end(), L),
		 l = *prev(upper_bound(stk.begin(), stk.end(), R));
	for (int i = f-1, mx=0; i >= L; i--) {
		if (h[i] <= mx-D && h[f] <= mx-D) {
			ans++;
			break;
		}
		chmax(mx, h[i]);
	}
	for (int i = l+1, mx=0; i <= R; i++) {
		if (h[i] <= mx-D && h[l] <= mx-D) {
			ans++;
			break;
		}
		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...