結果

問題 No.1597 Matrix Sort
ユーザー lavox
提出日時 2021-07-09 22:33:44
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,268 bytes
コンパイル時間 3,436 ms
コンパイル使用メモリ 77,704 KB
実行使用メモリ 70,608 KB
最終ジャッジ日時 2024-07-01 17:16:13
合計ジャッジ時間 33,317 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		long K = Long.parseLong(sc.next());
		int P = Integer.parseInt(sc.next());
		int[] A = new int[N];
		for ( int i = 0 ; i < N ; i++ ) {
			A[i] = Integer.parseInt(sc.next());
		}
		int[] B = new int[N];
		for ( int i = 0 ; i < N ; i++ ) {
			B[i] = Integer.parseInt(sc.next());
		}
		sc.close();

		Arrays.sort(B);
		
		int min = 0;
		int max = P - 1;
		while ( max - min > 1 ) {
			int mid = (min + max) / 2;
			if ( count(A, B, mid, P) < K ) {
				min = mid;
			} else {
				max = mid;
			}
		}
		
		System.out.println(min);
	}
	
	static long count(int[] A, int[] B, int x, int P) {
		long cnt = 0;
		for ( int a : A ) {
			// 0 == x,  P == x + P
			cnt += count(B, x - a);
			cnt += count(B, x + P - a) - count(B, P - a);
		}
		return cnt;
	}
	static long count(int[] B, int x) {
		if ( B[0] >= x ) {
			return 0;
		} else if ( B[B.length - 1] < x ) {
			return B.length;
		}
		int min = 0;
		int max = B.length - 1;
		while ( max - min > 1 ) {
			int mid = (min + max) / 2;
			if ( B[mid] < x ) {
				min = mid;
			} else {
				max = mid;
			}
		}
		return min + 1;
	}
}
0