Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <set>
- #include <algorithm>
- using namespace std;
- int main()
- {int t;
- cin>>t;
- for(int j = 0 ; j < t ;j++){
- int n;
- cin >> n;
- pair<long long,long long>building[n];
- for(int i=0; i < n;i++){
- cin >> building[i].first;
- }
- priority_queue<pair<long long ,long long> >q;
- for(int i=0; i < n;i++){
- cin >> building[i].second;
- q.push(building[i]);
- }
- for(int i = n-1 ; i>=0;i--){
- building[i]=q.top();
- q.pop();
- }
- if(n<=1){
- cout <<0;
- }else{
- long long low = 0 ;
- long long high = n-1;
- while(high>low){
- if(high-low<=2){long long maxooo=10e9;
- for(int z = low ; z <=high;z++ ){long long cost=0;
- for(int x = 0 ; x <=n-1 ;x++){
- cost+=(long long)abs(building[x].first-building[z].first)*building[x].second;
- }if(cost<maxooo){maxooo=cost;}
- }
- cout << maxooo;
- break;
- }
- long long mid1 = ((high-low)/3)+low;
- long long mid2 = high - ((high-low)/3);
- long long costt1=0;
- long long costt2=0;
- for(int i = 0 ;i < n ;i++){
- costt1+=(long long)abs(building[i].first-building[mid1].first)*building[i].second;
- costt2+=(long long)abs(building[i].first-building[mid2].first)*building[i].second;
- }
- if(costt2<=costt1){
- low = mid1+1;
- }else{
- high = mid2-1;}
- }
- }
- if(j != t-1){
- cout << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement