結果

問題 No.1597 Matrix Sort
ユーザー saksak
提出日時 2021-07-09 22:07:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 957 ms / 1,500 ms
コード長 2,265 bytes
コンパイル時間 2,109 ms
コンパイル使用メモリ 204,324 KB
実行使用メモリ 267,156 KB
最終ジャッジ日時 2023-09-14 09:14:42
合計ジャッジ時間 14,979 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 957 ms
266,644 KB
testcase_04 AC 916 ms
266,892 KB
testcase_05 AC 715 ms
266,884 KB
testcase_06 AC 652 ms
266,828 KB
testcase_07 AC 439 ms
267,120 KB
testcase_08 AC 896 ms
266,848 KB
testcase_09 AC 609 ms
266,824 KB
testcase_10 AC 224 ms
266,964 KB
testcase_11 AC 233 ms
267,156 KB
testcase_12 AC 242 ms
5,092 KB
testcase_13 AC 514 ms
21,044 KB
testcase_14 AC 772 ms
266,848 KB
testcase_15 AC 142 ms
4,936 KB
testcase_16 AC 360 ms
21,064 KB
testcase_17 AC 552 ms
266,868 KB
testcase_18 AC 754 ms
266,804 KB
testcase_19 AC 662 ms
266,868 KB
testcase_20 AC 582 ms
266,872 KB
testcase_21 AC 371 ms
267,000 KB
testcase_22 AC 280 ms
266,848 KB
testcase_23 AC 236 ms
266,944 KB
testcase_24 AC 31 ms
4,912 KB
testcase_25 AC 30 ms
4,920 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 77 ms
265,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct SegTree {
  ll size;
  vector<ll> pos;
  SegTree(ll N) { size = 1; while(size<N) size<<=1; pos.resize(2*size,0); }
  ll operator[](const ll &x) const { return pos[x+size]; }

  void set(ll x, const ll v) { pos[x+size] = v; }
  void update(ll x, const ll v) { set(x,v); x+=size; while (x>>=1) operate(x); }
  ll query(ll a, ll b) {
    ll L = 0, R = 0;
    for (a+=size, b+=size; a<b; a>>=1, b>>=1) {
      if (a&1) { L = q(L,pos[a]); a++; }
      if (b&1) { b--; R = q(pos[b],R); }
    }
    return q(L,R);
  }
  void operate(ll i) { pos[i] = q(pos[i*2], pos[i*2+1]); }
  ll q(ll x, ll y) { return x + y; }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll N, K, P; cin >> N >> K >> P;
  ll A[N]; rep(i,N) cin >> A[i];
  ll B[N]; rep(i,N) cin >> B[i];

  SegTree st(P); rep(i,N) st.update(B[i],st[B[i]]+1);
  // rep(mid,P) {
  //   ll cnt = 0;
  //   rep(i,N) {
  //     if (A[i]<=mid) cnt += st.query(mid-A[i],P-A[i]);
  //     else cnt += st.query(0,P-A[i]) + st.query(P+mid-A[i],P);
  //   }
  //   cout << mid << ":" << cnt << endl;
  // }

  ll l = 0, r = P-1;
  while (l!=r) {
    ll mid = (l+r+1)/2;
    ll cnt = 0;
    rep(i,N) {
      if (A[i]<=mid) cnt += st.query(mid-A[i],P-A[i]);
      else cnt += st.query(0,P-A[i]) + st.query(P+mid-A[i],P);
    }
    if (cnt>=N*N-K+1) l = mid;
    else r = mid-1;
  }
  ll result = l;
  cout << result << endl;
  return 0;
}
0