bzoj2732: [HNOI2012]射箭 半平面交+二分答案

bzoj2732: [HNOI2012]

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

HINT


数据已加强By WWT15。特鸣谢!—2015.03.09
数据再加一组—2017.3.25

分析

设抛物线 y=f(x)=ax2+bx(a>0,b<0)
对于一条线段 (x1,y1)(x1,y2)
有不等式 y1f(x1)y2
ax21+bx1y1
by1x1ax1
这样得到了一组关于 a,b 的不等式
对于每条线段造两个不等式。
然后二分答案,判断能射到哪里,然后用半平面交判断是否有交集即可。

代码

注意这是一道“二分精度”题
所有坑点在:bzoj2732discuss里面
注意一下就好,实在不行。。。可以精神AC

/**************************************************************
    Problem: 2732
    User: 2014lvzelong
    Language: C++
    Result: Accepted
    Time:3560 ms
    Memory:27084 kb
****************************************************************/

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstring>
#define double long double
using namespace std;
const double eps = 1e-18, inf = 1e15;
const int N = 220000;
int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
double sgn(double x) {return x > eps ? 1 : (x < -eps ? -1 : 0);}
struct P {
    double x, y;
    P(double a = 0, double b = 0) : x(a), y(b) {}
    P operator - (P a) const {return P(x - a.x, y - a.y);}
    P operator + (P a) const {return P(x + a.x, y + a.y);}
    P operator * (double a) {return P(x * a, y * a);}
    P operator / (double a) {return P(x / a, y / a);}
    double operator * (P a) const {return x * a.y - y * a.x;}
}ret[N], p[N];
double xmult(P a, P b, P c) {return (b - a) * (c - a);}
double angle(P a) {return atan2(a.y, a.x);}

struct line {
    P a, b; double ang;
    line(P c = P(0, 0), P d = P(0, 0)) : a(c), b(d), ang(angle(c - d)) {}
    P operator + (line c) {
        double w1 = xmult(c.a, a, b), w2 = xmult(c.b, b, a);
        return (c.a * w2 + c.b * w1) / (w1 + w2);
    }
}l[N];
int top, id[N], q[N], I[N], cnt, n;
double ans;
bool cmp(int a, int b) {
    int t = sgn(l[a].ang - l[b].ang);
    if(!t) return sgn(xmult(l[a].a, l[b].a, l[b].b)) > 0;
    return t < 0;
}

bool HPI(int mid) {
    int tot = 0, L = 1, R = 2;
    for(int i = 1;i <= top; ++i)
    if(id[i] <= mid && sgn(l[I[tot]].ang - l[id[i]].ang)) I[++tot] = id[i];
    q[1] = I[1]; q[2] = I[2];
    for(int i = 3;i <= tot; ++i) {
        while(L < R && sgn(xmult(l[q[R - 1]] + l[q[R]], l[I[i]].a, l[I[i]].b)) < 0) --R;
        while(L < R && sgn(xmult(l[q[L + 1]] + l[q[L]], l[I[i]].a, l[I[i]].b)) < 0) ++L;
        q[++R] = I[i];
    }
    while(L < R && sgn(xmult(l[q[R - 1]] + l[q[R]], l[q[L]].a, l[q[L]].b)) < 0) --R;
    while(L < R && sgn(xmult(l[q[L + 1]] + l[q[L]], l[q[R]].a, l[q[R]].b)) < 0) ++L;
    return R - L >= 2;
}
double calc(double a, double b, double x) {return b / a - a * x;}

int main()
{
    n = read(); P d[4] = {P(-eps, inf), P(-inf, inf), P(-inf, eps), P(-eps, eps)};
    for(int i = 0;i < 4; ++i) l[++top] = line(d[i], d[(i + 1) % 4]), id[top] = top;
    for(int i = 1;i <= n; ++i) {
        double x = read(), dy = read() - eps, uy = read() + eps;
        l[++top] = line(P(-1, calc(x, dy, -1)), P(1, calc(x, dy, 1))); id[top] = top;
        l[++top] = line(P(1, calc(x, uy, 1)), P(-1, calc(x, uy, -1))); id[top] = top;
    }
    sort(id + 1, id + top + 1, cmp); 
    int L = 1, R = n, ans = 0;
    while(L <= R) {
        int mid = L + R >> 1;
        if(HPI(mid + 2 << 1)) L = (ans = mid) + 1;
        else R = mid - 1;
    }
    printf("%d\n", ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值