Description
A large part of the world economy depends on oil, which is why research into new methods for finding and extracting oil is still active. Profits of oil companies depend in part on how efficiently they can drill for oil. The International Crude Petroleum Consortium (ICPC) hopes that extensive computer simulations will make it easier to determine how to drill oil wells in the best possible way. Drilling oil wells optimally is getting harder each day – the newly discovered oil deposits often do not form a single body, but are split into many parts. The ICPC is currently concerned with stratified deposits, as illustrated in Figure G.1.
Figure G.1: Oil layers buried in the earth. This figure corresponds to Sample Input 1.To simplify its analysis, the ICPC considers only the 2-dimensional case, where oil deposits are modeled as horizontal line segments parallel to the earth’s surface. The ICPC wants to know how to place a single oil well to extract the maximum amount of oil. The oil well is drilled from the surface along a straight line and can extract oil from all deposits that it intersects on its way down, even if the intersection is at an endpoint of a deposit. One such well is shown as a dashed line in Figure G.1, hitting three deposits. In this simple model the amount of oil contained in a deposit is equal to the width of the deposit. Can you help the ICPC determine the maximum amount of oil that can be extracted by a single well?
Input
The first line of input contains a single integer n (1 ≤ n ≤ 2 000), which is the number of oil deposits.This is followed by n lines, each describing a single deposit. These lines contain three integers x0,x1, and y giving the deposit’s position as the line segment with endpoints (x0, y) and (x1, y). These numbers satisfy |x0|, |x1| ≤ 106 and 1 ≤ y ≤ 106. No two deposits will intersect, not even at a point.
Output
Display the maximum amount of oil that can be extracted by a single oil well.
Solution
首先油井不能横着打(在WF原样例中可以体现),然后可以发现最优的油井挖掘中的一定存在一个方案,经过了其中一块油田的边缘。那么直接以每一块的油田边缘为原点进行极角排序扫描答案。用 xy 作为斜率可以规避很多问题。
#include<stdio.h>
#include<algorithm>
int n,ans,y[2002],Y,an;
double x0[2002],x1[2002],X;
bool f[2002];
struct poi
{
double v;int p;
friend bool operator <(const poi &a,const poi &b){return a.v<b.v;}
}K[4004];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%lf%lf%d",x0+i,x1+i,y+i);
if (x1[i]<x0[i]) std::swap(x1[i],x0[i]);
x0[i]-=0.0000001;x1[i]+=0.0000001;
}
for (int i=1;i<=n;i++) for (int now=0;now<2;now++)
{
int tot=0;ans=std::max(ans,an=x1[i]-x0[i]);
Y=y[i];X=(now)?x0[i]:x1[i];
for (int j=1;j<=n;j++) if (Y!=y[j])
{
K[tot++]={(X-x0[j])/(Y-y[j]),j};
K[tot++]={(X-x1[j])/(Y-y[j]),j};
}
std::sort(K,K+tot);
for (int j=0;j<tot;j++) if (ans<(an+=(f[K[j].p]^=1)?x1[K[j].p]-x0[K[j].p]:x0[K[j].p]-x1[K[j].p]+0.0000003)) ans=an;
}
printf("%d",ans);
}