結果

問題 No.1597 Matrix Sort
ユーザー ks2mks2m
提出日時 2021-07-09 22:15:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,206 ms / 1,500 ms
コード長 1,670 bytes
コンパイル時間 2,124 ms
コンパイル使用メモリ 74,268 KB
実行使用メモリ 68,324 KB
最終ジャッジ日時 2023-09-14 09:28:55
合計ジャッジ時間 24,812 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,556 KB
testcase_01 AC 47 ms
49,536 KB
testcase_02 AC 47 ms
49,500 KB
testcase_03 AC 1,206 ms
67,664 KB
testcase_04 AC 1,155 ms
67,028 KB
testcase_05 AC 945 ms
67,324 KB
testcase_06 AC 886 ms
66,740 KB
testcase_07 AC 877 ms
67,180 KB
testcase_08 AC 1,176 ms
67,260 KB
testcase_09 AC 836 ms
67,140 KB
testcase_10 AC 554 ms
66,720 KB
testcase_11 AC 923 ms
66,992 KB
testcase_12 AC 841 ms
66,460 KB
testcase_13 AC 1,004 ms
67,160 KB
testcase_14 AC 1,019 ms
67,244 KB
testcase_15 AC 703 ms
65,624 KB
testcase_16 AC 916 ms
66,372 KB
testcase_17 AC 969 ms
67,000 KB
testcase_18 AC 1,117 ms
68,148 KB
testcase_19 AC 1,079 ms
68,324 KB
testcase_20 AC 960 ms
67,668 KB
testcase_21 AC 913 ms
66,412 KB
testcase_22 AC 883 ms
67,088 KB
testcase_23 AC 779 ms
67,804 KB
testcase_24 AC 266 ms
66,132 KB
testcase_25 AC 270 ms
65,692 KB
testcase_26 AC 44 ms
49,484 KB
testcase_27 AC 45 ms
49,328 KB
testcase_28 AC 45 ms
49,472 KB
testcase_29 AC 46 ms
49,448 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