結果

問題 No.1170 Never Want to Walk
ユーザー tentententen
提出日時 2021-11-25 09:07:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 3,306 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 74,464 KB
実行使用メモリ 65,752 KB
最終ジャッジ日時 2023-09-10 08:41:36
合計ジャッジ時間 13,671 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,204 KB
testcase_01 AC 43 ms
49,144 KB
testcase_02 AC 42 ms
49,588 KB
testcase_03 AC 43 ms
49,396 KB
testcase_04 AC 42 ms
49,204 KB
testcase_05 AC 42 ms
49,196 KB
testcase_06 AC 43 ms
49,220 KB
testcase_07 AC 43 ms
51,508 KB
testcase_08 AC 41 ms
49,212 KB
testcase_09 AC 44 ms
49,176 KB
testcase_10 AC 42 ms
49,060 KB
testcase_11 AC 42 ms
49,524 KB
testcase_12 AC 54 ms
49,528 KB
testcase_13 AC 55 ms
50,328 KB
testcase_14 AC 55 ms
49,568 KB
testcase_15 AC 54 ms
49,944 KB
testcase_16 AC 55 ms
49,460 KB
testcase_17 AC 54 ms
49,640 KB
testcase_18 AC 54 ms
49,268 KB
testcase_19 AC 56 ms
49,652 KB
testcase_20 AC 54 ms
49,728 KB
testcase_21 AC 54 ms
49,220 KB
testcase_22 AC 54 ms
49,244 KB
testcase_23 AC 55 ms
49,228 KB
testcase_24 AC 56 ms
48,208 KB
testcase_25 AC 55 ms
49,644 KB
testcase_26 AC 54 ms
49,864 KB
testcase_27 AC 373 ms
63,560 KB
testcase_28 AC 375 ms
63,112 KB
testcase_29 AC 366 ms
62,732 KB
testcase_30 AC 367 ms
62,580 KB
testcase_31 AC 367 ms
62,976 KB
testcase_32 AC 366 ms
65,484 KB
testcase_33 AC 376 ms
65,752 KB
testcase_34 AC 385 ms
63,864 KB
testcase_35 AC 374 ms
64,080 KB
testcase_36 AC 379 ms
64,076 KB
testcase_37 AC 370 ms
65,304 KB
testcase_38 AC 376 ms
63,428 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