题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816

Problem Description
The Bathysphere is a spherical deep-sea submersible which was unpowered and lowered into the ocean on a cable, and was used to conduct a series of dives under the sea. The Bathysphere was designed for studying undersea wildlife.

The Bathysphere was conducted from the deck of a ship. After counted, the ship should not move, so choosing the position where the Bathysphere was conducted is important.

A group of scientists want to study the secrets of undersea world along the equator, and they would like to use the Bathysphere. They want to choose the position where the Bathysphere can dive as deep as possible. Before conducting the Bathysphere, they have a map of the seabed, which tell them the shape of the seabed. They draw a line on the equator of the map to mark where they will release the Bathysphere, as a number axis. Suppose the axis is draw from 0 to L. But when they release the Bathysphere, they can't know where they are accurately, i.e., if they choose position x to release the Bathysphere, the real position will distribute between x-d and x+d with an equal probability, where d is given. The objective of the scientists is very simple, i.e., to maximize the expected depth.

For the ease of presentation, the shape of the seabed is described as a poly line. Given N points ) , ( Xi,Yi ) as the vertices, where Xi and Yi indicate the position and the depth of the i-th vertex, respectively, the ploy line is composed of the line segments that connect consecutive vertices.

 
Input
The first line contains an integer T (1 ≤ T ≤ 25), the number of test cases.

Then T test cases follow. In each test case, the first line contains two integers N (2 ≤ N ≤ 2*10^5) and L (2 ≤ L ≤ 10^9), as described above. Then N lines follow, each line contains two integer Xi and Yi (1≤i≤N, 0≤ Yi ≤10^9), where point ( Xi,Yi ) is a vertex of the ploy line. It is assumed that X1 == 0 and Xn == L and Xi < Xi+1 for 1 ≤ i < N. Then the following line contains one integer d (0 ≤ d ≤ L/2), as described above.

 
Output
For each test case, choose a position between d and L-d, both inclusive, to conducted the Bathysphere, and calculate the expected depth. Output the expected depth in a line, rounded to 3 digits after the decimal point.
 
————————————————————————————————————————————————————————————————————————————————
贴一下官方题解:

题目大意:
在海平面上找一点投放潜水艇,投放的准确地点存在误差D,求最大的潜水深度期望。
题目分析:
即在海平面下再画一条折线,然后用间距为2×D的竖线将图截出,求截出的图形的最大面积。
解法:
可以看出当将两竖线不断右移的过程中,除了一种状态以外,其余状态对于面积的影响均为单调的。
此状态为当左边竖线所相交的折线为向上趋势并且右边竖线所相交的折线为向下趋势并且在到达端点前,两竖线与折线的交点的高度为相同的值时,此时面积最大。
所以,可以直接将两竖线从左往右移动,每次移动一个端点的距离,如果出现该情况则计算中途可能出现的最大面积,否则记录当前最大面积,即可于O(N)时间内得出结果。

————————————————————————————————————————————————————————————————————————————————

PS:我写这题在HDU上不用long double就过不了。也完全不知道怎么用double过了,有知道怎么办的请务必告诉我!三分就不要了……

代码(2390MS):

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

 ;
 ;

 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));
         ) {
             LDB t = - b /  / a;
              && sgn(t - r) <= )
                 res = val_at(t);
         }
         return res;
     }
 };

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

 LDB area(int pos) {
     ]) / ] - x[pos]);
 }

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

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

随机推荐

  1. ORM框架示例及查询测试,上首页修改版(11种框架)

    继上次ORM之殇,我们需要什么样的ORM框架? 整理了11个ORM框架测试示例,通过示例代码和结果,能很容易了解各种框架的特性,优缺点,排名不分先后 EF PDF XCODE CRL NHiberna ...

  2. Redis集群~StackExchange.redis连接Twemproxy代理服务器

    回到目录 本文是Redis集群系列的一篇文章,主要介绍使用StackExchange.Redis进行Twemproxy(文中简称TW)代理服务的连接过程,事务上,对于TW来说,我们需要理解一下它的物理 ...

  3. [转]使用Oracle SQL Developer 17410 提示无法从套接字获取更多数据如何解决

    本文转自:http://m.educity.cn/ite/1121475.html 将oracle?安装目录下的Network/admin/listener.ora文件中的 (PROGRAM = ex ...

  4. [CareerCup] 15.3 Renting Apartment III 租房之三

    Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...

  5. mongodb数据库设计原则

    1.一对很少  one-to-few  可以采用内嵌文档 person集合中 { name:'张三', age:20, address:[ {country:"中国",provin ...

  6. JavaScript对象就是一组属性(方法)的集合

    在JavaScript中,每个对象可以看作是多个属性(方法)的集合,引用一个属性(方法) 很简单,即: 对象名.属性(方法)名 除此之外,还可以用方括号的形式来引用: 对象名[“属性(方法)名”] 注 ...

  7. 【个人笔记】001-PHP基础-01-PHP快速入门-01-PHP职业路线及PHP前景

    001-PHP基础-01-PHP快速入门 01-PHP职业路线及PHP前景 PHP职业路线 PHP初级工程师 1年以下 3k-6k PHP中级工程师 1-3年6k-10k PHP高级工程师 3年以上  ...

  8. http请求头响应头大全

    转:http://www.jb51.net/article/51951.htm 本文为多篇“HTTP请求头相关文章”及<HTTP权威指南>一书的阅读后个人汇总整理版,以便于理解. 通常HT ...

  9. LeetCode OJ 66. Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  10. Python并发编程之多线程使用

    目录 一 开启线程的两种方式 二 在一个进程下开启多个线程与在一个进程下开启多个子进程的区别 三 练习 四 线程相关的其他方法 五 守护线程 六 Python GIL(Global Interpret ...