Advertisement
TwITe

Untitled

Jan 9th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. using ull = unsigned long long;
  5. const ll MAXN = 1e9 + 1;
  6.  
  7. map<ll, ll> h;
  8.  
  9. ll ans = 0;
  10.  
  11. bool check_cur_ans(ll T, ll H, ll x) {
  12.     ll t = 0;
  13.     ll current_h = 0;
  14.     while (current_h < H) {
  15.         current_h += x;
  16.         t++;
  17.         if (t > T) {
  18.             return false;
  19.         }
  20.  
  21.         auto it = h.end()--;
  22.         for (it; it != h.begin(); it--) {
  23.             if (current_h < it->first) {
  24.                 continue;
  25.             }
  26.             current_h -= min(it->second, current_h - it->first + 1);
  27.         }
  28.  
  29.         it = h.begin();
  30.         if (current_h < it->first) {
  31.             continue;
  32.         }
  33.         else {
  34.             current_h -= min(it->second, current_h - it->first + 1);
  35.         }
  36.     }
  37.     return true;
  38. }
  39.  
  40.  
  41. void bin_search (ll left, ll right, ll T, ll H, ll mid = 0) {
  42.     while (right - left > 0) {
  43.         mid = (left + right) >> 1;
  44.  
  45.         if ((check_cur_ans(T, H, mid))) {
  46.             right = mid;
  47.         }
  48.         else {
  49.             left = mid + 1;
  50.         }
  51.     }
  52.  
  53.     ans = right;
  54. }
  55.  
  56. void solve() {
  57.     ios::sync_with_stdio(false);
  58.     cin.tie(NULL);
  59.     ll n, H, T;
  60.     cin >> n >> H >> T;
  61.  
  62.     for (int i = 1; i <= n; i++) {
  63.         ll a, b;
  64.         cin >> a >> b;
  65.         h[a] = b;
  66.     }
  67.  
  68.     ll left = 1;
  69.     ll right = MAXN;
  70.  
  71.     bin_search(left, right, T, H);
  72.  
  73.     cout << ans;
  74. }
  75.  
  76. int main() {
  77.     solve();
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement