結果
問題 | No.1597 Matrix Sort |
ユーザー |
![]() |
提出日時 | 2021-07-09 22:06:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 405 ms / 1,500 ms |
コード長 | 1,729 bytes |
コンパイル時間 | 2,803 ms |
コンパイル使用メモリ | 145,916 KB |
最終ジャッジ日時 | 2025-01-22 21:51:11 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <tuple> #include <cmath> #include <numeric> #include <functional> #include <cassert> #include <atcoder/convolution> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n, k, p; cin >> n >> k >> p; vector<ll> a(n), b(n); for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); auto sect_elements = [&](int l, int r)->int{ if(r < l) return 0; if(r < 0) return 0; chmax(l, 0); int ans = upper_bound(b.begin(), b.end(), r)-lower_bound(b.begin(), b.end(), l); // cout << l << ',' << r << ' ' << ans << endl; return ans; }; auto calc = [&](int c){ ll ans = 0; for(int i = 0; i < n; i++){ ans += sect_elements(-a[i], c-a[i]); ans += sect_elements(p-a[i], p+c-a[i]); } return ans; }; if(calc(0) >= k){ cout << 0 << endl; return 0; } int l = 0, r = p-1; while(r-l > 1){ int c = (l+r)/2; if(calc(c) < k) l = c; else r = c; } cout << r << endl; }