CodeForces - 1252H Twin Buildings
题目:
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
2 5 5 3 4
12.5
2 2 5 4 3
8.0
3 10 1 9 8 7 6
42.0
Note
Explanation for the sample input/output #1
Two buildings of 2.5×52.5×5 can be built both on the first land.
Explanation for the sample input/output #2
Two buildings of 2×42×4 can be built each on the first and second lands.
Explanation for the sample input/output #3
Two buildings of 7×67×6 can be built each on the second and third lands.
题意:
给出n块 L × W 的陆地,你要在这n块陆地上建2个相同的建筑,可以在同一块陆地上建,也可以分开陆地建,求单个建筑的最大面积。
坑点:
因为L和W的范围都是1e9,所以乘起来范围到了1e18,而double的有效数位接近16位,如果用double存可能会出现精度问题。(wa到自闭)
以下截图来自知乎
思路:
首先应该想到,对于任意两块土地,他们的交集面积是他们最小的长度乘以他们最小的宽度,即S = min(L1,L2) * min(W1,W2)
我们按照长度从大到小排序,按顺序枚举最小的长度,此时我们要维护出现过的最大宽度。
设出现过的最大宽度为big,当前枚举的土地长度是x,宽度是y
因为我们是按长度从大到小排序,所以我们当前枚举的长度肯定是之前所有长度中最小的,即最小长度为x
当big<y的时候,说明之前出现的最大宽度都比当前土地的宽度小,即最小宽度为big
当big>=y的时候,说明出现过比y大的宽度,即最小宽度为y
这样就可以算出当最小长度是x的时候,所构成的最大交集面积。
然后再算一下是不是在一块陆地上建的时候为最优情况。
代码:
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 1e5 + 7; struct node { ll x,y; }a[maxn]; int n; bool cmp(node a,node b) { return a.x > b.x; } ll ans1,ans2; int main() { scanf("%d",&n); for (int i=1; i<=n; ++i) { scanf("%lld%lld",&a[i].x,&a[i].y); if(a[i].x < a[i].y) swap(a[i].x,a[i].y); ans1 = max(ans1,a[i].x * a[i].y); } sort(a+1,a+1+n,cmp); ll big = a[1].y; for (int i=2; i<=n; ++i) { if(a[i].y > big) ans2 = max(ans2,a[i].x * big); else ans2 = max(ans2,a[i].x * a[i].y); big = max(big,a[i].y); } ll ans = max(ans1,ans2*2); if(ans&1) printf("%lld.5",ans/2); else printf("%lld.0",ans/2); return 0; }