Advertisement
_rashed

SRM481-D1-500 BatchSystemRoulette

Feb 7th, 2023
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. class BatchSystemRoulette {
  20. public:
  21.     pair<double,int> arr[50];
  22.     int arr_sz;
  23.  
  24.     double fpow(double base, int p) {
  25.         if(p == 0) {
  26.             return 1.0;
  27.         }
  28.         if(p&1) {
  29.             return base * fpow(base,p-1);
  30.         }
  31.         double ret = fpow(base,p/2);
  32.         return ret*ret;
  33.     }
  34.  
  35.     vector <double> expectedFinishTimes(vector <int> duration, vector <string> user) {
  36.         map<string,vector<pair<double,int>>> tasks;
  37.         map<string,ll> sum;
  38.         vector<pair<ll,string>> v;
  39.         map<ll,int> cnt;
  40.         for(int i = 0; i < user.size(); i++) {
  41.             tasks[user[i]].push_back({duration[i]*1.0,i});
  42.             sum[user[i]] += duration[i];
  43.         }
  44.         for(pair<string,ll> p : sum ) {
  45.             cnt[p.second]++;
  46.             v.push_back({p.second,p.first});
  47.         }
  48.         sort(v.begin(),v.end());
  49.         ll pre = 0;
  50.         vector<double> ans;
  51.         ans.resize(duration.size());
  52.         for(int i = 0; i < v.size(); i++) {
  53.             pair<ll,string> us = v[i];
  54.             arr_sz = 0;
  55.             double eq_wait = 0;
  56.             if(cnt[us.first] > 1) {
  57.                 eq_wait = us.first * fpow(2,cnt[us.first]-1) + (cnt[us.first]-1) * us.first * fpow(2, cnt[us.first]-2);
  58.                 eq_wait /= fpow(2, cnt[us.first]-1);
  59.                 eq_wait -= us.first;
  60.             }
  61.             for(pair<double,int> p : tasks[us.second]) {
  62.                 arr[arr_sz] = p;
  63.                 arr_sz++;
  64.             }
  65.             for(int j = 0; j < arr_sz; j++) {
  66.                 double curr = 0;
  67.                 for(int k = 0; k < arr_sz; k++) {
  68.                     if(k == j)
  69.                         curr += arr[k].first * fpow(2, arr_sz-1);
  70.                     else
  71.                         curr += arr[k].first * fpow(2, arr_sz-2);
  72.                 }
  73.                 ans[arr[j].second] = curr / fpow(2,arr_sz-1) + pre + eq_wait;
  74.             }
  75.             if(i < v.size()-1 && v[i+1].first != us.first)
  76.                 pre += us.first * cnt[us.first];
  77.         }
  78.         return ans;
  79.     }
  80. };
  81.  
  82. int main()
  83. {
  84.     /*ios_base::sync_with_stdio(NULL);
  85.     cin.tie(0);*/
  86.     BatchSystemRoulette bs;
  87.     vector<double> v = bs.expectedFinishTimes({14,13,14,13},{"A", "A", "B", "B"});
  88.     for(int i = 0; i < v.size(); i++) {
  89.         cout << v[i] << " ";
  90.     }
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement