| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1311726 | samarthkulkarni | Lightning Rod (NOI18_lightningrod) | C11 | 0 ms | 0 KiB |
#include <stdio.h>
#include <stdlib.h>
// Fast I/O for C11
// Uses getchar_unlocked (standard on Linux judges) for max speed
inline int readInt() {
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'); // x = x * 10 + digit
ch = getchar_unlocked();
}
return x;
}
// Global arrays are stored in the data segment (not stack),
// preventing Stack Overflow for 10^7 elements.
#define MAXN 10000005
int X[MAXN], Y[MAXN];
int avl[MAXN];
// C doesn't have a built-in abs() for ints that is as fast as a macro/inline
#define ABS(x) ((x) < 0 ? -(x) : (x))
int main() {
int n = readInt();
if (n <= 0) {
if (n == 0) printf("0\n");
return 0;
}
for (int i = 0; i < n; i++) {
X[i] = readInt();
Y[i] = readInt();
}
int p = -1; // Stack pointer
for (int i = 0; i < n; i++) {
// Step 1: Check if building 'i' is already covered by the stack top
// Rule: |Xi - Xj| <= Yj - Yi
if (p >= 0) {
int top_idx = avl[p];
if (ABS(X[i] - X[top_idx]) <= (Y[top_idx] - Y[i])) {
continue;
}
}
// Step 2: Check if building 'i' covers buildings already in the stack
// Rule: |Xi - Xj| <= Yi - Yj
while (p >= 0) {
int top_idx = avl[p];
if (ABS(X[i] - X[top_idx]) <= (Y[i] - Y[top_idx])) {
p--;
} else {
break;
}
}
// Step 3: Add current building to the stack
avl[++p] = i;
}
printf("%d\n", p + 1);
return 0;
}
