結果

問題 No.860 買い物
ユーザー tentententen
提出日時 2021-03-10 14:50:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 604 ms / 1,000 ms
コード長 2,741 bytes
コンパイル時間 3,713 ms
コンパイル使用メモリ 75,296 KB
実行使用メモリ 65,444 KB
最終ジャッジ日時 2023-08-02 10:23:07
合計ジャッジ時間 11,088 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
49,748 KB
testcase_01 AC 40 ms
49,408 KB
testcase_02 AC 41 ms
49,692 KB
testcase_03 AC 41 ms
49,676 KB
testcase_04 AC 42 ms
49,380 KB
testcase_05 AC 43 ms
49,512 KB
testcase_06 AC 121 ms
53,824 KB
testcase_07 AC 578 ms
64,604 KB
testcase_08 AC 604 ms
64,600 KB
testcase_09 AC 597 ms
65,444 KB
testcase_10 AC 584 ms
64,616 KB
testcase_11 AC 362 ms
64,464 KB
testcase_12 AC 372 ms
64,440 KB
testcase_13 AC 555 ms
65,156 KB
testcase_14 AC 578 ms
65,044 KB
testcase_15 AC 351 ms
65,360 KB
testcase_16 AC 578 ms
64,728 KB
testcase_17 AC 339 ms
64,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main (String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		ArrayList<Path> paths = new ArrayList<>();
		long total = 0;
		for (int i = 1; i <= n; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int c = Integer.parseInt(line[0]);
		    int d = Integer.parseInt(line[1]);
		    total += c;
		    paths.add(new Path(c, 0, i));
		    if (i > 1) {
		        paths.add(new Path(d, i - 1, i));
		    }
		}
		Collections.sort(paths);
		UnionFindTree uft = new UnionFindTree(n + 1);
		for (Path p : paths) {
		    if (!uft.same(p)) {
		        uft.unite(p);
		        total += p.value;
		    }
		}
		System.out.println(total);
	}
	
	static class UnionFindTree {
	    int[] parents;
	    
	    public UnionFindTree(int size) {
	        parents = new int[size];
	        for (int i = 0; i < size; i++) {
	            parents[i] = i;
	        }
	    }
	    
	    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 boolean same(Path p) {
	        return same(p.left, p.right);
	    }
	    
	    public void unite(int x, int y) {
	        if (!same(x, y)) {
	            parents[find(y)] = find(x);
	        }
	    }
	    
	    public void unite(Path p) {
	        unite(p.left, p.right);
	    }
	}
	
	static class Path implements Comparable<Path> {
	    int value;
	    int left;
	    int right;
	    
	    public Path(int value, int left, int right) {
	        this.value = value;
	        this.left = left;
	        this.right = right;
	    }
	    
	    public int compareTo(Path another) {
	        return value - another.value;
	    }
	}
}

class BinaryIndexedTree {
    int size;
    int[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new int[size];
    }
    
    public void add(int idx, int value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
0