結果

問題 No.1597 Matrix Sort
ユーザー boatmusclesboatmuscles
提出日時 2021-07-09 23:38:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 498 ms / 1,500 ms
コード長 1,212 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 111,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 10:58:30
合計ジャッジ時間 13,933 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 485 ms
4,376 KB
testcase_04 AC 498 ms
4,376 KB
testcase_05 AC 492 ms
4,376 KB
testcase_06 AC 485 ms
4,376 KB
testcase_07 AC 494 ms
4,376 KB
testcase_08 AC 459 ms
4,380 KB
testcase_09 AC 471 ms
4,380 KB
testcase_10 AC 391 ms
4,380 KB
testcase_11 AC 389 ms
4,380 KB
testcase_12 AC 402 ms
4,376 KB
testcase_13 AC 464 ms
4,376 KB
testcase_14 AC 479 ms
4,376 KB
testcase_15 AC 365 ms
4,380 KB
testcase_16 AC 415 ms
4,376 KB
testcase_17 AC 444 ms
4,380 KB
testcase_18 AC 485 ms
4,380 KB
testcase_19 AC 436 ms
4,376 KB
testcase_20 AC 408 ms
4,376 KB
testcase_21 AC 407 ms
4,380 KB
testcase_22 AC 411 ms
4,376 KB
testcase_23 AC 408 ms
4,376 KB
testcase_24 AC 328 ms
4,376 KB
testcase_25 AC 328 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _GLIBCXX_DEBUG
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ull N, K;
unsigned P;
ull Solve(int x, vector<unsigned> A, vector<unsigned> B){
    unsigned a, b, c;
    a = (B[0] <= x) ? distance(A.begin(), upper_bound(A.begin(), A.end(), x - B[0])) : 0;
    b = distance(A.begin(), lower_bound(A.begin(), A.end(), P - B[0]));
    c = distance(A.begin(), upper_bound(A.begin(), A.end(), P + x - B[0]));
    ull ret = a + c - b;
    for (unsigned i = 1; i < N; i++){
        while(a > 0 && A[a-1] + B[i] > x ) a--;
        while(b > 0 && A[b-1] + B[i] >= P) b--;
        while(c > 0 && A[c-1] + B[i] > x + P) c--;
        ret += a + c - b;
    }
    return ret;
}

int main(){
    scanf("%llu %llu %u", &N, &K, &P);
    vector<unsigned> A(N);
    for(int i = 0; i < N; ++i) scanf("%u", &A[i]);
    sort(A.begin(), A.end());
    vector<unsigned> B(N);
    for(int i = 0; i < N; ++i) scanf("%u", &B[i]);
    sort(B.begin(), B.end());
    int ok = P-1, ng = -1, mid;
    while(ok - ng > 1){
        mid = ok + ng >>1;
        if(Solve(mid, A, B) >= K) ok = mid;
        else ng = mid;
    }
    printf("%d\n", ok);
	return 0;
}
0