#define _GNU_SOURCE
#include <stdio.h>
// Fast I/O using the fastest available character reader
inline int read() {
int x = 0;
int ch = getchar_unlocked();
while (ch < '0' || ch > '9') {
if (ch == EOF) return -1;
ch = getchar_unlocked();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch - '0');
ch = getchar_unlocked();
}
return x;
}
// Global memory allocation (10^7 elements)
#define MAXN 10000005
int X[MAXN], Y[MAXN];
int avl[MAXN];
// Macro for absolute value to avoid function call overhead
#define ABS(x) ((x) < 0 ? -(x) : (x))
int main() {
int n = read();
if (n <= 0) {
if (n == 0) printf("0\n");
return 0;
}
for (int i = 0; i < n; i++) {
X[i] = read();
Y[i] = read();
}
int p = -1; // Stack pointer
for (int i = 0; i < n; i++) {
// Condition: |Xi - Xj| <= Yj - Yi
// This is equivalent to saying Building j protects Building i
if (p >= 0) {
int top_idx = avl[p];
int dx = X[i] - X[top_idx];
if (ABS(dx) <= (Y[top_idx] - Y[i])) {
continue; // i is already protected, skip it
}
}
// Check if current building i protects buildings already in the stack
while (p >= 0) {
int top_idx = avl[p];
int dx = X[i] - X[top_idx];
if (ABS(dx) <= (Y[i] - Y[top_idx])) {
p--; // Building in stack is now redundant
} else {
break;
}
}
// Push current building index to stack
avl[++p] = i;
}
// Output the final count of unprotected buildings
printf("%d\n", p + 1);
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |