Advertisement
Mehulcoder

CureFit2020

Oct 6th, 2020 (edited)
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. /*
  2. Author: Mehul Chaturvedi
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. #define all(x) (x).begin(), (x).end()
  10. #define vll vector<long long>
  11. #define INF 4557430888798830399ll
  12. #define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
  13. /*
  14. * What could be the worst case? That the maximum time in the array is our ans.
  15. * What could be the best? 0 days. Now, say if you are given a day, and I ask you that could this be the answer?
  16. * Simple O(n), just check if >=c disjoint groups can be formed of length m. If yes then, hi=mid-1 (and update ans) else
  17. * lo=mid+1
  18. */
  19. void solve() {
  20.     ll n, c, m; cin >> n >> c >> m;
  21.  
  22.     vll v(n, 0);
  23.     rep(i, n) cin >> v[i];
  24.  
  25.     ll hi = *max_element(all(v));
  26.     ll lo = 0;
  27.  
  28.     ll ans = INF;
  29.     while (lo <= hi) {
  30.         ll mid = (lo + hi) / 2;
  31.         vll temp(n, 0);
  32.         rep(i, n) temp[i] = (v[i] <= mid);
  33.         ll count = 0;
  34.         ll blocks = 0;
  35.         rep(i, n) {
  36.             if (temp[i] == 0 && count != 0) {
  37.                 blocks += count / m;
  38.                 count = 0;
  39.             } else if (temp[i] != 0) {
  40.                 count++;
  41.             }
  42.         }
  43.         blocks += count / m;
  44.  
  45.         if (blocks >= c) {
  46.             ans = min(ans, mid);
  47.             hi = mid - 1;
  48.         } else {
  49.             lo = mid + 1;
  50.         }
  51.     }
  52.  
  53.     cout << ((ans == INF) ? (-1) : (ans)) << '\n';
  54.  
  55.     return;
  56. }
  57.  
  58. int main(int argc , char ** argv) {
  59.     ios_base::sync_with_stdio(false) ;
  60.     cin.tie(NULL) ;
  61.  
  62.     ll t = 1;
  63.     while (t--) {
  64.         solve();
  65.     }
  66.  
  67.     return 0 ;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement