結果

問題 No.1170 Never Want to Walk
ユーザー tentententen
提出日時 2021-11-25 09:07:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 3,306 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 78,348 KB
実行使用メモリ 55,168 KB
最終ジャッジ日時 2024-06-28 00:08:55
合計ジャッジ時間 11,625 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,792 KB
testcase_01 AC 54 ms
36,856 KB
testcase_02 AC 52 ms
36,556 KB
testcase_03 AC 53 ms
36,896 KB
testcase_04 AC 52 ms
36,608 KB
testcase_05 AC 52 ms
37,020 KB
testcase_06 AC 53 ms
36,664 KB
testcase_07 AC 54 ms
36,964 KB
testcase_08 AC 55 ms
37,092 KB
testcase_09 AC 53 ms
36,760 KB
testcase_10 AC 54 ms
37,028 KB
testcase_11 AC 54 ms
36,864 KB
testcase_12 AC 59 ms
37,140 KB
testcase_13 AC 60 ms
37,080 KB
testcase_14 AC 65 ms
37,112 KB
testcase_15 AC 59 ms
37,396 KB
testcase_16 AC 62 ms
37,312 KB
testcase_17 AC 62 ms
37,092 KB
testcase_18 AC 59 ms
36,684 KB
testcase_19 AC 61 ms
37,088 KB
testcase_20 AC 62 ms
36,788 KB
testcase_21 AC 61 ms
37,396 KB
testcase_22 AC 60 ms
37,136 KB
testcase_23 AC 62 ms
37,080 KB
testcase_24 AC 61 ms
37,124 KB
testcase_25 AC 60 ms
36,892 KB
testcase_26 AC 66 ms
37,464 KB
testcase_27 AC 387 ms
52,972 KB
testcase_28 AC 383 ms
52,648 KB
testcase_29 AC 392 ms
52,500 KB
testcase_30 AC 388 ms
52,488 KB
testcase_31 AC 386 ms
53,052 KB
testcase_32 AC 392 ms
54,856 KB
testcase_33 AC 392 ms
54,724 KB
testcase_34 AC 407 ms
54,104 KB
testcase_35 AC 411 ms
53,700 KB
testcase_36 AC 399 ms
55,168 KB
testcase_37 AC 410 ms
54,896 KB
testcase_38 AC 417 ms
54,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        int[] values = new int[n + 1];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        values[n] = Integer.MAX_VALUE;
        UnionFindTree uft = new UnionFindTree(n);
        int left = 0;
        int right = 0;
        int prevLeft = 0;
        int prevRight = 0;
        for (int i = 0; i < n; i++) {
            while (values[left] - values[i] < a) {
                left++;
            }
            while (values[right] - values[i] <= b) {
                right++;
            }
            if (prevRight > left) {
                uft.unite(i, i - 1);
            }
            for (int j = Math.max(prevRight, left); j < right; j++) {
                uft.unite(i, j);
            }
            prevLeft = left;
            prevRight = right;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(uft.getCount(i)).append("\n");
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        int[] counts;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            counts = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                counts[i] = 1;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return;
            }
            parents[yy] = xx;
            counts[xx] += counts[yy];
        }
        
        public int getCount(int x) {
            return counts[find(x)];
        }
    }
    
    static class Path implements Comparable<Path> {
        int cost;
        int left;
        int right;
        
        public Path(int cost, int left, int right) {
            this.cost = cost;
            this.left = left;
            this.right = right;
        }
        
        public int compareTo(Path another) {
            return cost - another.cost;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0