Last active
June 14, 2021 15:46
-
-
Save 0xkohe/01db8a05584db938eb8e2e9f52e4e743 to your computer and use it in GitHub Desktop.
blog-binary_search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
const long long INF = 1LL << 60; | |
int main() { | |
int N, M; | |
cin >> N >> M; | |
vector<long long> a(N); | |
vector<long long> b(N); | |
for (int i = 0; i < N; ++i) { | |
cin >> a[i]; | |
} | |
for (int i = 0; i < N; ++i) { | |
cin >> b[i]; | |
} | |
// O(log_N) | |
sort(b.begin(), b.end()); | |
long long left = 0; | |
long long right = INF; | |
// O(log_(MAX(A)*MAX(B))) | |
while (right - left > 1) { | |
long long mid = (right + left) / 2; | |
long long cnt = 0; | |
// O(N) | |
for (int i = 0; i < N; ++i) { | |
// O(log_N) | |
cnt += upper_bound(b.begin(), b.end(), mid / a[i]) - b.begin(); | |
} | |
if (cnt >= M) { | |
right = mid; | |
} else { | |
left = mid; | |
} | |
} | |
cout << right << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment