HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

题目链接: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):

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 typedef long double LDB;
 8
 9 const int MAXN = 200010;
10 const LDB EPS = 1e-6;
11
12 inline int sgn(LDB x) {
13     return (x > EPS) - (x < -EPS);
14 }
15
16 int x[MAXN], y[MAXN];
17 int n, d, L, T;
18
19 struct Game {
20     LDB a, b, c;
21     Game() {}
22     Game(LDB a, LDB b, LDB c): a(a), b(b), c(c) {}
23     Game operator - (const Game &rhs) const {
24         return Game(a - rhs.a, b - rhs.b, c - rhs.c);
25     }
26     LDB val_at(LDB x) {
27         return a * x * x + b * x + c;
28     }
29     LDB max_val(LDB l, LDB r) {
30         LDB res = max(val_at(l), val_at(r));
31         if(sgn(a) < 0) {
32             LDB t = - b / 2 / a;
33             if(sgn(l - t) <= 0 && sgn(t - r) <= 0)
34                 res = val_at(t);
35         }
36         return res;
37     }
38 };
39
40 Game get(int pos, LDB v = 0.0) {
41     LDB k = LDB(y[pos + 1] - y[pos]) / (x[pos + 1] - x[pos]);
42     LDB t = y[pos] - k * x[pos];
43     LDB a = k / 2, b = t, c = -((k / 2) * x[pos] + t) * x[pos];
44     return Game(a, 2 * v * a + b, a * v * v + b * v + c);
45 }
46
47 LDB area(int pos) {
48     return (y[pos] + y[pos + 1]) / 2.0 * (x[pos + 1] - x[pos]);
49 }
50
51 LDB solve() {
52     if(d == 0) {
53         int res = 0;
54         for(int i = 1; i <= n; ++i) res = max(res, y[i]);
55         return res;
56     }
57     LDB nowx = 0, res = 0, s = 0;
58     int l = 1, r = 1;
59     while(r < n && x[r + 1] <= d)
60         s += area(r++);
61     if(r == n) res = s;
62     while(r < n) {
63         LDB minx = min(x[l + 1] - nowx, x[r + 1] - nowx - d);
64         res = max(res, s + (get(r, d) - get(l)).max_val(nowx, nowx + minx));
65         nowx += minx;
66         if(sgn(x[l + 1] - nowx) == 0) s -= area(l++), nowx = x[l];
67         if(sgn(x[r + 1] - nowx - d) == 0) s += area(r++), nowx = x[r] - d;
68     }
69     return res / d;
70 }
71
72 int main() {
73     scanf("%d", &T);
74     while(T--) {
75         scanf("%d%d", &n, &L);
76         for(int i = 1; i <= n; ++i) scanf("%d%d", &x[i], &y[i]);
77         x[n + 1] = x[n];
78         scanf("%d", &d); d <<= 1;
79         printf("%.3f\n", (double)solve());
80     }
81 }

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun),布布扣,bubuko.com

时间: 2024-08-02 15:14:48

HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)的相关文章

HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow, and Blue are in war. The map of battlefield is a tree, which means that there are N nodes and (N – 1) edges that connect all the nodes. Each country

2013 Asia Regional Changchun

Hard Code http://acm.hdu.edu.cn/showproblem.php?pid=4813 1 #include<cstdio> 2 char op[1024]; 3 int main(){ 4 int t,n,m; 5 while(~scanf("%d",&t)){ 6 while(t--){ 7 scanf("%d%d%s",&n,&m,op); 8 for(int i=0;i<n;i++){ 9

2013 Asia Regional Changchun C

Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 960    Accepted Submission(s): 344 Problem Description A crowd of little animals is visiting a mysterious laboratory

2013 Asia Regional Changchun I 题,HDU(4821),Hash

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比赛中的一个求平方和的题目思路很类似. 首先说一下hash,简单来说就是y = hash(x),有很多函数,可以参考这里:https://www.byvoid.com/blog/string-hash-compare/ 然后,我用的是这个:写法简单,而且重复的可能较小. // BKDR Hash Fu

(并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2061    Accepted Submission(s): 711 Problem Description Jack likes to travel around the wo

2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 329 Problem Description Elves are very peculiar creatures. As we all know, they can live for a very

2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图

Ponds                                   Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)                                        Problem Description Betty owns a lot of ponds, some of them are connected with other

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is