As you might already know, space has always been a problem in ICPC Jakarta. To cope with this, ICPC Jakarta is planning to build two new buildings. These buildings should have a shape of a rectangle of the same size. Now, their problem is to find land to build the buildings.
There are NN lands available for sale. The ithith land has a rectangular shape of size Li×WiLi×Wi. For a good feng shui, the building‘s side should be parallel to the land‘s sides.
One way is to build the two buildings on two different lands, one on each land (not necessarily with the same orientation). A building of size A×BA×B can be build on the ithith land if and only if at least one of the following is satisfied:
A≤LiA≤Li and B≤WiB≤Wi, or
A≤WiA≤Wi and B≤LiB≤Li.
Alternatively, it is also possible to build two buildings of A×BA×B on the ithith land with the same orientation. Formally, it is possible to build two buildings of A×BA×B on the ithith land if and only if at least one of the following is satisfied:
A×2≤LiA×2≤Li and B≤WiB≤Wi, or
A×2≤WiA×2≤Wi and B≤LiB≤Li, or
A≤LiA≤Li and B×2≤WiB×2≤Wi, or
A≤WiA≤Wi and B×2≤LiB×2≤Li.
Your task in this problem is to help ICPC Jakarta to figure out the largest possible buildings they can build given NN available lands. Note that ICPC Jakarta has to build two buildings of A×BA×B; output the largest possible for A×BA×B.
Input
Input begins with a line containing an integer: NN (1≤N≤1000001≤N≤100000) representing the number of available lands. The next NN lines each contains two integers: LiLi WiWi (1≤Li,Wi≤1091≤Li,Wi≤109) representing the size of the land.
Output
Output in a line a number representing the largest building that ICPC Jakarta can build with exactly one decimal point (see sample input/output for clarity).
Examples
input
Copy
2
5 5
3 4
output
Copy
12.5
input
Copy
2
2 5
4 3
output
Copy
8.0
input
Copy
3
10 1
9 8
7 6
output
Copy
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.
解题思路:建造两块场地,使得两块场地的面积尽可能地大,有两种情况,第一种:取一块最大的土地,一分为二。第二种:对土地的长(x)进行排序,从大到小,面积取当前x与min(之前y的最大值,当前y的值)乘积作为一块的面积,那在当前x之前的x都大于当前x,所以必定也能放得下这一块面积。有个细节问题是土地的长可以宽,宽也可以是长,所以当宽>长时,需要交换值。注意精度问题,用long long存。
AC代码:
#include <iostream> #include <algorithm> using namespace std; struct Point{ long long x,y; }point[100005]; bool cmp(Point a,Point b) { return a.x*a.y>b.x*b.y; } int main() { int n; while(cin>>n) { long long area=0; for(int i=0;i<n;i++) { cin>>point[i].x; cin>>point[i].y; area=max(point[i].x*point[i].y,area); if(point[i].y>point[i].x) swap(point[i].y,point[i].x); } long long maxy=0; sort(point,point+n,cmp); for(int i=0;i<n;i++) { area=max(area,point[i].x*min(point[i].y,maxy)*2); maxy=max(point[i].y,maxy); } if(area%2) cout<<area/2<<".5"<<endl; else cout<<area/2<<".0"<<endl; } return 0; }
原文地址:https://www.cnblogs.com/lovelcy/p/11824421.html