Advertisement
_rashed

SRM456-D2-1000

Jul 2nd, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-12;
  7.  
  8. class CutSticks {
  9. public:
  10.     int bs(double s, double thold) {
  11.         ll l = 1;
  12.         ll h = 1e9 + 50;
  13.         ll ans = 1;
  14.         while(l <= h) {
  15.             double mid = (l+h)/2;
  16.             double curr = s/mid;
  17.             if(curr+EPS < thold) {
  18.                 h = mid-1;
  19.             }
  20.             else {
  21.                 ans = mid;
  22.                 l = mid+1;
  23.             }
  24.         }
  25.         return ans;
  26.     }
  27.  
  28.     double maxKth(vector<int> sticks, int C, int K) {
  29.         ll n =  sticks.size();
  30.         double ansl = 0;
  31.         double ansh = 1e9;
  32.         double ans = 0;
  33.         for(int btimes = 0; btimes < 128; btimes++) {
  34.             double mid = (ansl+ansh)/2.0;
  35.             bool possible = false;
  36.             for(int i = 0; i < n; i++) {
  37.                 if(mid-EPS > sticks[i]) {
  38.                     continue;
  39.                 }
  40.                 ll cuts = 1;
  41.                 ll mx_pos = 1;
  42.                 ll extra_cuts = 0;
  43.                 for(int k = 0; k < n; k++) {
  44.                     if(k == i || mid-EPS > sticks[k]) {
  45.                         continue;
  46.                     }
  47.                     ll pos_add = bs(sticks[k],mid);
  48.                     mx_pos += pos_add;
  49.                     extra_cuts += pos_add-1;
  50.                 }
  51.                 if(sticks[i]-mid > mid) {
  52.                     ll pos_add = bs(sticks[i]-mid,mid);
  53.                     mx_pos += pos_add;
  54.                     extra_cuts += pos_add-1;
  55.                 }
  56.                 if(cuts+extra_cuts > C) {
  57.                     mx_pos -= (cuts+extra_cuts-C);
  58.                 }
  59.                 //cout << "curr_v is " << curr_v << "\n";
  60.                 if(mx_pos >= K) {
  61.                     //cout << "mx_pos is " << mx_pos << " at i = " << i << " j = " << j << "\n";
  62.                     possible = true;
  63.                     break;
  64.                 }
  65.             }
  66.             if(possible) {
  67.                 ans = mid;
  68.                 ansl = mid;
  69.             }
  70.             else {
  71.                 ansh = mid;
  72.             }
  73.         }
  74.         return ans;
  75.     }
  76.  
  77. };
  78.  
  79.  
  80. int main()
  81. {
  82.     ios_base::sync_with_stdio(false);
  83.     cin.tie(NULL);
  84.     cout.tie(NULL);
  85.     CutSticks cs;
  86.     cout << cs.maxKth({8},50,3) << "\n";
  87.     //cout << cs.bs(8,1) << "\n";
  88.     /*vector<int> v = {76, 594, 17, 6984, 26, 57, 9, 876, 5816, 73, 969, 527, 49};
  89.     sort(v.begin(), v.end());
  90.     for(int i = 0; i < v.size(); i++) {
  91.         cout << v[i] << "\n";
  92.     }*/
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement