結果

問題 No.1597 Matrix Sort
ユーザー ks2mks2m
提出日時 2021-07-09 22:15:28
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 1,149 ms / 1,500 ms
コード長 1,670 bytes
コンパイル時間 1,680 ms
使用メモリ 55,776 KB
最終ジャッジ日時 2023-02-02 00:02:54
合計ジャッジ時間 23,863 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 50 ms
35,816 KB
testcase_01 AC 51 ms
35,752 KB
testcase_02 AC 49 ms
34,480 KB
testcase_03 AC 1,119 ms
53,276 KB
testcase_04 AC 1,149 ms
52,124 KB
testcase_05 AC 935 ms
52,788 KB
testcase_06 AC 891 ms
54,308 KB
testcase_07 AC 878 ms
53,852 KB
testcase_08 AC 1,118 ms
52,420 KB
testcase_09 AC 864 ms
54,372 KB
testcase_10 AC 571 ms
52,704 KB
testcase_11 AC 865 ms
52,552 KB
testcase_12 AC 797 ms
51,700 KB
testcase_13 AC 1,030 ms
53,736 KB
testcase_14 AC 985 ms
53,528 KB
testcase_15 AC 666 ms
52,760 KB
testcase_16 AC 845 ms
55,776 KB
testcase_17 AC 929 ms
52,356 KB
testcase_18 AC 1,072 ms
52,832 KB
testcase_19 AC 1,041 ms
55,140 KB
testcase_20 AC 910 ms
52,964 KB
testcase_21 AC 853 ms
53,032 KB
testcase_22 AC 900 ms
53,048 KB
testcase_23 AC 733 ms
53,272 KB
testcase_24 AC 272 ms
51,876 KB
testcase_25 AC 302 ms
53,588 KB
testcase_26 AC 54 ms
35,616 KB
testcase_27 AC 53 ms
34,120 KB
testcase_28 AC 49 ms
34,348 KB
testcase_29 AC 52 ms
35,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		long k = Long.parseLong(sa[1]);
		int p = Integer.parseInt(sa[2]);
		sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]) % p;
		}
		sa = br.readLine().split(" ");
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			b[i] = Integer.parseInt(sa[i]) % p;
		}
		br.close();

		Arrays.sort(a);
		Arrays.sort(b);

		long k2 = (long) n * n - k + 1;
		int ok = 0;
		int ng = p;
		while (Math.abs(ok - ng) > 1) {
			int mid = (ok + ng) / 2;
			long cnt = 0;
			for (int i = 0; i < n; i++) {
				if (a[i] <= mid) {
					int min = mid - a[i];
					int max = p - a[i];
					int l = binarySearch(b, min);
					int r = binarySearch(b, max);
					cnt += r - l;
				} else {
					int min = 0;
					int max = p - a[i];
					int l = binarySearch(b, min);
					int r = binarySearch(b, max);
					cnt += r - l;

					min = mid + p - a[i];
					max = p;
					l = binarySearch(b, min);
					r = binarySearch(b, max);
					cnt += r - l;
				}
			}
			if (cnt >= k2) {
				ok = mid;
			} else {
				ng = mid;
			}
		}
		System.out.println(ok);
	}

	static int binarySearch(int[] array, int val) {
		int ok = array.length;
		int ng = -1;
		while (Math.abs(ok - ng) > 1) {
			int mid = (ok + ng) / 2;
			if (array[mid] >= val) {
				ok = mid;
			} else {
				ng = mid;
			}
		}
		return ok;
	}
}
0