H - Twin Buildings ( 精度损失 )
题意: 有n个长方形的岛,给出长宽。现在想要在岛上建两个大小完全一样的长方形房子,房子的边必须和岛的边平行,问最大能建多么大的。
思路: 按照岛的最长边进行排序,前面的一定可以包括后面的,这样只需要考虑宽就好了。
注意: double存不下long long范围的值,会有精度损失。 处理方式看代码输出部分
Examples Input
2 5 5 3 4
Output
12.5
Input
2 2 5 4 3
Output
8.0
Input
3 10 1 9 8 7 6
Output
42.0
Note
Explanation for the sample input/output #1
Two buildings of 2.5×5
can be built both on the first land.
Explanation for the sample input/output #2
Two buildings of 2×4
can be built each on the first and second lands.
Explanation for the sample input/output #3
Two buildings of 7×6
can be built each on the second and third lands.
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
typedef long long ll;
struct node {
ll xiao,da;
}a[maxn];
ll n,x,y;
bool rule( node a, node b )
{
return a.da>b.da;
}
int main()
{
cin >> n;
for ( int i=0; i<n; i++ ) {
scanf("%lld %lld",&x,&y);
a[i].da = max(x,y);
a[i].xiao = min(x,y);
}
sort(a,a+n,rule);
ll maxx=a[0].xiao, ans=a[0].xiao*a[0].da,now;
for ( int i=1; i<n; i++ ) {
ans = max( ans,a[i].xiao*a[i].da );
now = min(maxx,a[i].xiao);
ans = max( ans, 2*now*a[i].da );
maxx = max(maxx,a[i].xiao);
}
//printf("%.1f\n",(double)(ans/2.0));
if ( ans%2==1 ) {
printf("%lld.5\n",ans/2);
}
else {
printf("%lld.0\n",ans/2);
}
return 0;
}