题目
题解
这个答案具有二分性,,因为你是知道应该往左还是往右的_ (:зゝ∠) _,给做出来的神犇跪了。对嘛,二分之后y从小到大来确定每一条线段的倾角范围,然后取交,不为空说明有解,至于不合法往哪边走,如果按与x轴正方向倾角来说的话(小写代表当前线段,大写代表总的),l>R时往右,r< L时往左。
然后就是斜率为0,还有精度误差!!!这个精度快把我搞吐血了,才知道原来二分实数时有的值是达不到的_ (:зゝ∠) _比如说l和r肯定是,所以check里面需要容忍一些误差,使得mid很接近ans时能check出真来。。所以要写成l>R+eps之类的,判斜率不存在时要判一下。2h调试时间(//w//包括大部分聊天时间) = get实数精度坑爹点。还有什么明明返回0是可行却当成非0可行之类的煞笔错误不想再说。。
代码
//QWsin
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=5000+10;
typedef long double ld;
const ld pi=acos(-1.0);
const ld eps=1e-9;
int n,W;
struct Node{
ld y,x1,x2;
inline void input(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
y=a;x1=b;x2=c;
}
inline bool operator < (const Node &rhs)const{
return y<rhs.y;
}
}a[maxn];
inline ld work(ld x1,ld y1,ld x2,ld y2){
if(fabs(x1-x2)<eps) return pi/2;
ld ret=atan((y1-y2)/(x1-x2));
return ret<0?ret+pi:ret;
}
inline int check(ld x)
{
ld L=0,R=pi,l,r;
for(int i=1;i<=n;++i)
{
l=work(a[i].x2,a[i].y,x,0);
r=work(a[i].x1,a[i].y,x,0);
if(eps<L-r) return -1;
if(-eps>R-l) return 1;
L=max(L,l);R=min(R,r);
}
return 0;
}
inline void solve()
{
cin>>W>>n;
for(int i=1;i<=n;++i) a[i].input();
sort(a+1,a+n+1);
ld l=0,r=W;
while(r-l > eps)
{
ld mid=(l+r)/2;
int tmp=check(mid);
if(!tmp) {puts("YES");return ;}
if(tmp==-1) r=mid;
else l=mid;
}
puts("NO");
}
int main()
{
int T;cin>>T;
while(T--) solve();
return 0;
}