fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> ii;
  6.  
  7. const int INF = 1e9;
  8. const ll LINF = 1e18;
  9.  
  10. // Nhận xét: với số x càng lớn thì vị trí của x trong mảng sau khi sort càng lớn
  11. // Từ đây ta có thể tìm kiếm nhị phân số x đầu tiên có vị trí >= k
  12. // Vị trí của x = (số lượng số <= x có trong bảng)
  13. ll n, k;
  14.  
  15. bool check(ll x) {
  16. ll pos = 0;
  17. for (int i = 1; i <= n; i++) pos += min(x / i, n);
  18. return (pos >= k);
  19. }
  20.  
  21. int main() {
  22. ios::sync_with_stdio(0); cin.tie(0);
  23. cin >> n;
  24. k = (n * n + 1) / 2;
  25.  
  26. ll l = 1, r = n * n, ans = -1;
  27. while (l <= r) {
  28. ll mid = (l + r) >> 1;
  29.  
  30. if (check(mid)) {
  31. ans = mid;
  32. r = mid - 1;
  33. }
  34. else {
  35. l = mid + 1;
  36. }
  37. }
  38.  
  39. cout << ans << '\n';
  40. }
Success #stdin #stdout 0.35s 5288KB
stdin
999999
stdout
186682420008