結果

問題 No.1597 Matrix Sort
ユーザー kobakoba_issakobakoba_issa
提出日時 2021-07-09 23:15:03
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 565 ms / 1,500 ms
コード長 2,222 bytes
コンパイル時間 7,140 ms
コンパイル使用メモリ 134,728 KB
実行使用メモリ 4,816 KB
最終ジャッジ日時 2023-09-14 10:44:55
合計ジャッジ時間 16,375 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 504 ms
4,692 KB
testcase_04 AC 565 ms
4,716 KB
testcase_05 AC 560 ms
4,680 KB
testcase_06 AC 541 ms
4,692 KB
testcase_07 AC 515 ms
4,680 KB
testcase_08 AC 477 ms
4,680 KB
testcase_09 AC 495 ms
4,676 KB
testcase_10 AC 354 ms
4,680 KB
testcase_11 AC 383 ms
4,680 KB
testcase_12 AC 329 ms
4,720 KB
testcase_13 AC 461 ms
4,724 KB
testcase_14 AC 523 ms
4,712 KB
testcase_15 AC 242 ms
4,676 KB
testcase_16 AC 373 ms
4,664 KB
testcase_17 AC 427 ms
4,676 KB
testcase_18 AC 553 ms
4,676 KB
testcase_19 AC 549 ms
4,684 KB
testcase_20 AC 541 ms
4,744 KB
testcase_21 AC 495 ms
4,748 KB
testcase_22 AC 415 ms
4,748 KB
testcase_23 AC 390 ms
4,720 KB
testcase_24 AC 49 ms
4,816 KB
testcase_25 AC 47 ms
4,688 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 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