結果

問題 No.1597 Matrix Sort
ユーザー ocvret_
提出日時 2021-07-11 10:43:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 369 ms / 1,500 ms
コード長 1,007 bytes
コンパイル時間 1,637 ms
コンパイル使用メモリ 171,592 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-02 03:00:49
合計ジャッジ時間 8,675 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007


int main(){
  
  ll n,K,p;
  cin >> n >> K >> p;
  vector<int> a(n),b(n);
  rep(i,n)cin >> a[i],a[i] %= p;
  rep(i,n)cin >> b[i],b[i] %= p;
  sort(ALL(a));
  sort(ALL(b));
  ll l = -1,r = p;
  while(r-l > 1){
    ll mid = (l+r)/2;
    ll cnt = 0;
    rep(i,n){
      int mn = mid-a[i],mx = p + mid - a[i];
      {
        auto iter = lower_bound(ALL(b),mn+1);
        cnt += iter - b.begin();
      }
      {
        auto ml = lower_bound(ALL(b),p-a[i]);
        auto mr = lower_bound(ALL(b),p+mid-a[i]+1);
        cnt += mr-ml;
      }
    }
    if(cnt >= K)r = mid;
    else l = mid;
  }
  cout << r << "\n";
  

  return 0;
}
0