-
Notifications
You must be signed in to change notification settings - Fork 0
/
the_biggest_restaurant.cpp
58 lines (58 loc) · 1.38 KB
/
the_biggest_restaurant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.first > b.first);
}
int main(void){
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long double x[n],h[n];
for(int i=0;i<n;i++)
{
cin>>x[i]>>h[i];
}
sort(h,h+n);
long double sum=0;
long double diff[n-1];
long double temp[n];
vector<pair<long double,long long int>> temp2;
for(int i=1;i<n;i++)
{
diff[i-1]=x[i]-x[i-1];
}
for(int i=0;i<n-2;i++)
{
temp2.push_back(make_pair(diff[i]+diff[i+1],i+1));
}
temp2.push_back(make_pair(diff[0],-1));
temp2.push_back(make_pair(diff[n-2],-2));
sort(temp2.begin(),temp2.end(),sortinrev);
for(int i=0;i<n;i++)
{
if(temp2[i].second==-1)
{
temp[0]=h[n-i-1];
}
else if(temp2[i].second==-2)
{
temp[n-1]=h[n-i-1];
}
else
{
temp[temp2[i].second]=h[n-i-1];
}
}
for(int i=0;i<n-1;i++)
{
sum=sum+(diff[i]*(temp[i]+temp[i+1]))/2;
}
long long int ans= (long long int)(sum*2);
cout<<ans<<"\n";
}
}