2013 Asia Regional Changchun HDU 4816 Bathysphere(数学)

题目大意:
在海平面上找一点投放潜水艇,投放的准确地点存在误差D,求最大的潜水深度期望。
题目分析:
即在海平面下再画一条折线,然后用间距为2×D的竖线将图截出,求截出的图形的最大面积。
解法:
可以看出当将两竖线不断右移的过程中,除了一种状态以外,其余状态对于面积的影响均为单调的。
此状态为当左边竖线所相交的折线为向上趋势并且右边竖线所相交的折线为向下趋势并且在到达端点前,两竖线与折线的交点的高度为相同的值时,此时面积最大。

所以,可以直接将两竖线从左往右移动,每次移动一个端点的距离,如果出现该情况则计算中途可能出现的最大面积,否则记录当前最大面积,即可于O(N)时间内得出结果。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef long double LDB;

const int MAXN = 200010;
const LDB EPS = 1e-6;

inline int sgn(LDB x) {
    return (x > EPS) - (x < -EPS);
}

int x[MAXN], y[MAXN];
int n, d, L, T;

struct Game {
    LDB a, b, c;
    Game() {}
    Game(LDB a, LDB b, LDB c): a(a), b(b), c(c) {}
    Game operator - (const Game &rhs) const {
        return Game(a - rhs.a, b - rhs.b, c - rhs.c);
    }
    LDB val_at(LDB x) {
        return a * x * x + b * x + c;
    }
    LDB max_val(LDB l, LDB r) {
        LDB res = max(val_at(l), val_at(r));
        if(sgn(a) < 0) {
            LDB t = - b / 2 / a;
            if(sgn(l - t) <= 0 && sgn(t - r) <= 0)
                res = val_at(t);
        }
        return res;
    }
};

Game get(int pos, LDB v = 0.0) {
    LDB k = LDB(y[pos + 1] - y[pos]) / (x[pos + 1] - x[pos]);
    LDB t = y[pos] - k * x[pos];
    LDB a = k / 2, b = t, c = -((k / 2) * x[pos] + t) * x[pos];
    return Game(a, 2 * v * a + b, a * v * v + b * v + c);
}

LDB area(int pos) {
    return (y[pos] + y[pos + 1]) / 2.0 * (x[pos + 1] - x[pos]);
}

LDB solve() {
    if(d == 0) {
        int res = 0;
        for(int i = 1; i <= n; ++i) res = max(res, y[i]);
        return res;
    }
    LDB nowx = 0, res = 0, s = 0;
    int l = 1, r = 1;
    while(r < n && x[r + 1] <= d)
        s += area(r++);
    if(r == n) res = s;
    while(r < n) {
        LDB minx = min(x[l + 1] - nowx, x[r + 1] - nowx - d);
        res = max(res, s + (get(r, d) - get(l)).max_val(nowx, nowx + minx));
        nowx += minx;
        if(sgn(x[l + 1] - nowx) == 0) s -= area(l++), nowx = x[l];
        if(sgn(x[r + 1] - nowx - d) == 0) s += area(r++), nowx = x[r] - d;
    }
    return res / d;
}

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &L);
        for(int i = 1; i <= n; ++i) scanf("%d%d", &x[i], &y[i]);
        x[n + 1] = x[n];
        scanf("%d", &d); d <<= 1;
        printf("%.3f\n", (double)solve());
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值