Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <set>
  5. #include <algorithm>
  6. using namespace std;
  7. int main()
  8. {int t;
  9. cin>>t;
  10. for(int j = 0 ; j < t ;j++){
  11.    int n;
  12.    cin >> n;
  13.    pair<long long,long long>building[n];
  14.    for(int i=0; i < n;i++){
  15.     cin >> building[i].first;
  16.    }
  17.    priority_queue<pair<long long ,long long> >q;
  18.    for(int i=0; i < n;i++){
  19.     cin >> building[i].second;
  20.     q.push(building[i]);
  21.    }
  22.    for(int i = n-1 ; i>=0;i--){
  23.     building[i]=q.top();
  24.     q.pop();
  25.    }
  26.    if(n<=1){
  27.     cout <<0;
  28.    }else{
  29.    long long low = 0 ;
  30.    long long high = n-1;
  31.     while(high>low){
  32.             if(high-low<=2){long long maxooo=10e9;
  33.                 for(int z = low ; z <=high;z++ ){long long cost=0;
  34.                     for(int x = 0 ; x <=n-1 ;x++){
  35.                         cost+=(long long)abs(building[x].first-building[z].first)*building[x].second;
  36.                     }if(cost<maxooo){maxooo=cost;}
  37.                 }
  38.                 cout << maxooo;
  39.                 break;
  40.             }
  41.         long long mid1 = ((high-low)/3)+low;
  42.         long long mid2 = high - ((high-low)/3);
  43.         long long costt1=0;
  44.         long long costt2=0;
  45.         for(int i = 0 ;i < n ;i++){
  46.             costt1+=(long long)abs(building[i].first-building[mid1].first)*building[i].second;
  47.             costt2+=(long long)abs(building[i].first-building[mid2].first)*building[i].second;
  48.         }
  49.         if(costt2<=costt1){
  50.             low = mid1+1;
  51.         }else{
  52.         high = mid2-1;}
  53.     }
  54.  
  55.    }
  56.    if(j != t-1){
  57.     cout << endl;
  58.    }
  59.    }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement