結果

問題 No.1597 Matrix Sort
ユーザー kobakoba_issakobakoba_issa
提出日時 2021-07-09 23:15:03
言語 C++17(clang Beta)
(clang 13.0.0 + boost 1.78.0)
結果
AC  
実行時間 561 ms / 1,500 ms
コード長 2,222 bytes
コンパイル時間 3,785 ms
使用メモリ 4,736 KB
最終ジャッジ日時 2023-02-02 01:12:38
合計ジャッジ時間 12,340 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
3,380 KB
testcase_01 AC 2 ms
3,372 KB
testcase_02 AC 1 ms
3,428 KB
testcase_03 AC 500 ms
4,636 KB
testcase_04 AC 561 ms
4,640 KB
testcase_05 AC 556 ms
4,624 KB
testcase_06 AC 533 ms
4,624 KB
testcase_07 AC 507 ms
4,736 KB
testcase_08 AC 471 ms
4,552 KB
testcase_09 AC 484 ms
4,684 KB
testcase_10 AC 349 ms
4,588 KB
testcase_11 AC 379 ms
4,588 KB
testcase_12 AC 326 ms
4,636 KB
testcase_13 AC 459 ms
4,596 KB
testcase_14 AC 521 ms
4,548 KB
testcase_15 AC 239 ms
4,644 KB
testcase_16 AC 371 ms
4,644 KB
testcase_17 AC 428 ms
4,632 KB
testcase_18 AC 547 ms
4,640 KB
testcase_19 AC 544 ms
4,604 KB
testcase_20 AC 537 ms
4,620 KB
testcase_21 AC 496 ms
4,608 KB
testcase_22 AC 412 ms
4,584 KB
testcase_23 AC 384 ms
4,644 KB
testcase_24 AC 46 ms
4,684 KB
testcase_25 AC 43 ms
4,644 KB
testcase_26 AC 2 ms
3,392 KB
testcase_27 AC 1 ms
3,472 KB
testcase_28 AC 1 ms
3,312 KB
testcase_29 AC 2 ms
3,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sz(a) (int)(a.size())

template<class T> 
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}

template<class T> 
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}

#define MAX (ll)1e6+1    //nCrのnの最大値+1

ll fac[MAX], inv[MAX], finv[MAX];

void COMinit(ll mod) {
    fac[0] = 1, finv[0] = 1;
    fac[1] = 1, inv[1] = 1, finv[1] = 1;
    for (int i = 2; i < MAX; i++) {
        fac[i] = fac[i-1] * i % mod;
        inv[i] = mod - (inv[mod % i] * (mod/i) % mod) % mod;
        finv[i] = finv[i-1] * inv[i] % mod;
    }
}

ll COM(ll n, ll k, ll mod) {
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * finv[k] % mod * finv[n-k] % mod;
}


//コーナーケースに気をつけろ!
int main() {
    ll n, k, p; cin >> n >> k >> p;
    vector<ll> a(n), b(n);
    rep(i, n) {
        cin >> a[i];
        a[i] %= p;
    }
    rep(i, n) {
        cin >> b[i];
        b[i] %= p;
    }
    sort(b.begin(), b.end());
    ll l = -1, r = p-1;
    while (l + 1 < r) {
        ll x = (l + r) / 2, cnt = 0;
        //cout << "x = " << x << endl;
        rep(i, n) {
            //cout << a[i] << " ";
            cnt += upper_bound(b.begin(), b.end(), x-a[i]) - b.begin();
            //cout << a[i] - x << " 以下 " << upper_bound(b.begin(), b.end(), x-a[i]) - b.begin() << " ";
            if (max(0LL, max(p-a[i], x-a[i]+1)) <= x + p - a[i])  {
                cnt += upper_bound(b.begin(), b.end(), x+p-a[i]) - lower_bound(b.begin(), b.end(), max(0LL, max(p-a[i], x-a[i]+1)));
                //cout << max(a[i]-p, x-a[i]+1) << "以上" << x+p-a[i] <<  "以下 " <<  upper_bound(b.begin(), b.end(), x+p-a[i]) - lower_bound(b.begin(), b.end(), max(a[i]-p, x-a[i]+1));
            }
            //cout << endl;
        }
        //cout << cnt << endl;
        if (cnt >= k)
            r = x;
        else 
            l = x;
    }
    cout << r << endl;
    return 0;   
}

//小数点精度
//cout << fixed << std::setprecision(15) << y << endl;
0