結果

問題 No.1597 Matrix Sort
ユーザー startcppstartcpp
提出日時 2021-07-09 22:06:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 1,500 ms
コード長 1,420 bytes
コンパイル時間 748 ms
コンパイル使用メモリ 85,104 KB
実行使用メモリ 6,012 KB
最終ジャッジ日時 2023-09-14 09:11:50
合計ジャッジ時間 5,953 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 186 ms
6,012 KB
testcase_04 AC 218 ms
5,688 KB
testcase_05 AC 229 ms
5,748 KB
testcase_06 AC 225 ms
5,800 KB
testcase_07 AC 207 ms
5,796 KB
testcase_08 AC 171 ms
5,752 KB
testcase_09 AC 177 ms
5,700 KB
testcase_10 AC 111 ms
5,880 KB
testcase_11 AC 123 ms
5,868 KB
testcase_12 AC 121 ms
5,736 KB
testcase_13 AC 192 ms
5,736 KB
testcase_14 AC 213 ms
5,684 KB
testcase_15 AC 96 ms
5,740 KB
testcase_16 AC 145 ms
5,732 KB
testcase_17 AC 169 ms
5,692 KB
testcase_18 AC 220 ms
5,688 KB
testcase_19 AC 178 ms
5,676 KB
testcase_20 AC 160 ms
5,696 KB
testcase_21 AC 155 ms
5,684 KB
testcase_22 AC 151 ms
6,012 KB
testcase_23 AC 145 ms
5,688 KB
testcase_24 AC 43 ms
5,756 KB
testcase_25 AC 39 ms
5,772 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//やることは分かっていて、a + b < P と a + b >= P の2グループに分けてから、
//X以下の要素は何個?(f(X)個とおく)を解いて、f(X) >= K なる最小のXを2分探索で求めればよい。
//あとは実装を詰められるかどうか、だ!
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n, K, p;
int a[100000];
int b[100000];
int pos[100000];	//pos[i] = a[i] + b[j] >= pを満たす最小のj (無ければn)

//X以下が何個あるか
int f(int X) {
	int ret = 0;
	for (int i = 0; i < n; i++) {
		//b[0, pos[i])の中で数える
		int iter1 = upper_bound(b, b + pos[i], X - a[i]) - b;
		ret += iter1;
		
		//b[pos[i], n)の中で数える
		int iter2 = upper_bound(b + pos[i], b + n, X - a[i] + p) - b;
		iter2 -= pos[i];
		ret += iter2;
	}
	return ret;
}

signed main() {
	int i;
	
	cin >> n >> K >> p;
	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];
	sort(a, a + n);
	sort(b, b + n);
	
	rep(i, n) {
		pos[i] = lower_bound(b, b + n, p - a[i]) - b;
	}
	
	int ng = -1, ok = p, mid;
	while (ok - ng >= 2) {
		mid = (ng + ok) / 2;
		if (f(mid) >= K) ok = mid;
		else ng = mid;
	}
	
	cout << ok << endl;
	return 0;
}
0