題目鏈接
題目描述
Description
沫沫最近在玩一個二維的射箭遊戲,如下圖 1 所示,這個遊戲中的 x 軸在地面,第一象限中有一些豎直線段作爲靶子,任意兩個靶子都沒有公共部分,也不會接觸座標軸。沫沫控制一個位於(0,0)的弓箭手,可以朝 0 至 90?中的任意角度(不包括 0度和 90度),以任意大小的力量射出帶有穿透能力的光之箭。由於遊戲中沒有空氣阻力,並且光之箭沒有箭身,箭的軌跡會是一條標準的拋物線,被軌跡穿過的所有靶子都認爲被沫沫射中了,包括那些 只有端點被射中的靶子。這個遊戲有多種模式,其中沫沫最喜歡的是闖關模式。在闖關模式中,第一關只有一個靶 子,射中這個靶子即可進入第二關,這時在第一關的基礎上會出現另外一個靶子,若能夠一箭 雙鵰射中這兩個靶子便可進入第三關,這時會出現第三個靶子。依此類推,每過一關都會新出 現一個靶子,在第 K 關必須一箭射中前 K 關出現的所有 K 個靶子才能進入第 K+1 關,否則遊戲 結束。沫沫花了很多時間在這個遊戲上,卻最多隻能玩到第七關“七星連珠”,這讓她非常困惑。 於是她設法獲得了每一關出現的靶子的位置,想讓你告訴她,最多能通過多少關
Input
輸入文件第一行是一個正整數N,表示一共有N關。接下來有N行,第i+1行是用空格隔開的三個正整數xi,yi1,yi2(yi1
Output
僅包含一個整數,表示最多的通關數。
Sample Input
5
2 8 12
5 4 5
3 8 10
6 2 3
1 3 7
Sample Output
3
題解
我們二分答案,對於一個答案,我們可以列出n個不等式。我們只要用半平面交判斷解集是否爲空即可。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define exp 1e-80
#define maxn 200010
struct node{
long double x,y;
node(){};
node(long double _x,long double _y){
x=_x; y=_y;
}
}d[maxn],p[maxn];
typedef node Vector;
struct line{
node p;
Vector v;
long double ang;
line(){};
line(node _p,Vector _v){
p=_p; v=_v;
ang=atan2(v.y,v.x);
}
friend bool operator <(line a,line b){
return a.ang<b.ang;
}
}l[maxn],q[maxn];
int T,n,m;
long double x[maxn],y_1[maxn],y_2[maxn];
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }
long double operator * (Vector A,Vector B) {return A.x*B.y-B.x*A.y; }
bool ok(line L,node P){
return L.v*(P-L.p)>=0;
}
node getnode(line a,line b){
Vector u=a.p-b.p;
long double t=b.v*u/(a.v*b.v);
return a.p+a.v*t;
}
bool solve(int n){
sort(l,l+n);
int first,last;
q[first=last=0]=l[0];
for(int i=1;i<n;i++){
while(first<last&& !ok(l[i],p[last-1]))last--;
while(first<last&& !ok(l[i],p[first]))first++;
q[++last]=l[i];
if(fabs(q[last].v*(q[last-1].v))<exp){
last--;
if(ok(q[last],l[i].p))q[last]=l[i];
}
if(first<last) p[last-1]=getnode(q[last-1],q[last]);
}
while(first<last&&!ok(q[first],p[last-1]))last--;
if(last-first<=1)return 0;
p[last]=getnode(q[last],q[first]);
return 1;
}
bool ok(int t){
for(int i=0;i<t;i++){
l[i]=line(node(0,y_1[i]/x[i]),node(y_1[i]/(x[i]*x[i]),0)-node(0,y_1[i]/x[i]));
l[i+t]=line(node(y_2[i]/(x[i]*x[i]),0),node(0,y_2[i]/x[i])-node(y_2[i]/(x[i]*x[i]),0));
}
return solve(t*2);
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%Lf%Lf%Lf",&x[i],&y_1[i],&y_2[i]);
int l=1,r=n+1;
while(r-l>1){
int mid=(l+r)>>1;
if(ok(mid)) l=mid;
else r=mid;
}
printf("%d\n",l);
return 0;
}