Submission #2528246


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Stream;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static int MOD = 1000000007;
	static int[] mh4 = { 0, -1, 1, 0 };
	static int[] mw4 = { -1, 0, 0, 1 };

	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			int n = in.nextInt();
			P[] ps1 = new P[n];
			for (int i = 0; i < n; i++) {
				ps1[i] = new P(i, in.nextLong(), in.nextLong());
			}

			P[] ps2 = Arrays.copyOf(ps1, n);

			Arrays.sort(ps1, (a,b) -> (int)a.x-(int)b.x);
			Arrays.sort(ps2, (a,b) -> (int)a.y-(int)b.y);

			List<E>[] g = new ArrayList[n];
			g = Stream.generate(ArrayList::new).limit(n).toArray(List[]::new);

			for (int i = 0; i < n-1; i++) {
				g[ps1[i].num].add(new E(ps1[i+1].num, Math.abs(ps1[i+1].x-ps1[i].x)));
				g[ps1[i+1].num].add(new E(ps1[i].num, Math.abs(ps1[i+1].x-ps1[i].x)));
			}

			for (int i = 0; i < n-1; i++) {
				g[ps2[i].num].add(new E(ps2[i+1].num, Math.abs(ps2[i+1].y-ps2[i].y)));
				g[ps2[i+1].num].add(new E(ps2[i].num, Math.abs(ps2[i+1].y-ps2[i].y)));
			}

			Set<Integer> mst = new HashSet<>();
			PriorityQueue<E> q = new PriorityQueue<E>((a,b) -> (int)a.c - (int)b.c);

			mst.add(0);
			for (E e : g[0]) {
				q.add(e);
			}

			long ans = 0;
			while (!q.isEmpty()) {
				E edge = q.remove();
				if (mst.contains(edge.t)) {
					continue;
				}
				mst.add(edge.t);
				for (E e : g[edge.t]) {
					q.add(e);
				}
				ans += edge.c;
			}

			out.println(ans);

		}
	}
	static class P{
		int num;
		long x, y;

		public P(int num,long x, long y) {
			super();
			this.num = num;
			this.x = x;
			this.y = y;
		}
	}

	static class E {
		int t;
		long c;

		public E(int t, long c) {
			super();
			this.t = t;
			this.c = c;
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}

	}

}

Submission Info

Submission Time
Task D - Built?
User tutuz
Language Java8 (OpenJDK 1.8.0)
Score 500
Code Size 3607 Byte
Status AC
Exec Time 1216 ms
Memory 96052 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 28
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 1038 ms 94208 KB
02.txt AC 1121 ms 91140 KB
03.txt AC 1077 ms 90640 KB
04.txt AC 1161 ms 90712 KB
05.txt AC 1047 ms 91376 KB
06.txt AC 1092 ms 89300 KB
07.txt AC 1076 ms 88984 KB
08.txt AC 1171 ms 89708 KB
09.txt AC 1151 ms 90852 KB
10.txt AC 1090 ms 91476 KB
11.txt AC 1131 ms 92772 KB
12.txt AC 1216 ms 96052 KB
13.txt AC 936 ms 90932 KB
14.txt AC 927 ms 87844 KB
15.txt AC 990 ms 93124 KB
16.txt AC 974 ms 90112 KB
17.txt AC 984 ms 89648 KB
18.txt AC 925 ms 94068 KB
19.txt AC 1006 ms 91020 KB
20.txt AC 1147 ms 91648 KB
21.txt AC 1106 ms 92876 KB
22.txt AC 1054 ms 89340 KB
23.txt AC 672 ms 94160 KB
24.txt AC 1110 ms 94216 KB
25.txt AC 171 ms 26068 KB
26.txt AC 173 ms 25684 KB
s1.txt AC 163 ms 26196 KB
s2.txt AC 162 ms 26068 KB