結果
問題 | No.1170 Never Want to Walk |
ユーザー | 👑 ygussany |
提出日時 | 2020-08-14 21:46:13 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 65 ms / 2,000 ms |
コード長 | 1,639 bytes |
コンパイル時間 | 245 ms |
コンパイル使用メモリ | 31,232 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-10 14:53:29 |
合計ジャッジ時間 | 2,909 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
#include <stdio.h> #include <stdlib.h> typedef struct { int *par, *size, *height; } UF_forest; void UF_initialize(UF_forest *F, int n) { int i; F->par = (int*)malloc(sizeof(int) * (n + 1)); F->size = (int*)malloc(sizeof(int) * (n + 1)); F->height = (int*)malloc(sizeof(int) * (n + 1)); for (i = 1; i <= n; i++) { F->par[i] = i; F->size[i] = 1; F->height[i] = 1; } } void UF_merge(UF_forest *F, int u, int v) { for (; F->par[u] != u; u = F->par[u]); for (; F->par[v] != v; v = F->par[v]); if (u == v) return; else if (F->height[u] > F->height[v]) { F->par[v] = u; F->size[u] += F->size[v]; } else { F->par[u] = v; F->size[v] += F->size[u]; if (F->height[u] == F->height[v]) F->height[v]++; } } int UF_root(UF_forest *F, int v) { for (; F->par[v] != v; v = F->par[v]); return v; } int UF_size(UF_forest *F, int v) { for (; F->par[v] != v; v = F->par[v]); return F->size[v]; } int main() { int i, N, A, B, x[200002]; scanf("%d %d %d", &N, &A, &B); for (i = 1; i <= N; i++) scanf("%d", &(x[i])); x[0] = -(1 << 30); x[N+1] = x[N] + B + 1; int j, l[2], r[2], m, tmp = 0; UF_forest F; UF_initialize(&F, N + 1); for (i = 1; i <= N; i++) { l[0] = i; r[0] = N + 1; while (l[0] < r[0]) { m = (l[0] + r[0] + 1) / 2; if (x[m] - x[i] > B) r[0] = m - 1; else l[0] = m; } l[1] = i; r[1] = N + 1; while (l[1] < r[1]) { m = (l[1] + r[1]) / 2; if (x[m] - x[i] < A) l[1] = m + 1; else r[1] = m; } for (j = (l[1] < tmp)? tmp: l[1]; j <= r[0]; j++) UF_merge(&F, i, j); tmp = r[0]; } for (i = 1; i <= N; i++) printf("%d\n", UF_size(&F, i)); fflush(stdout); return 0; }