Question:
N columns are arranged in one row, and the angle is tilted. The left direction is positive, and the right direction is negative. The maximum water accumulation is obtained.
Ideas:
Find the highest pillar after the tilt, and then find the edge of the water. There is a small trick, and the water level is just in the middle of a pillar:
The following is an official issue report:
When angle is negative, the order of n columns is reversed. The absolute value of angle is the same. Therefore, you only need to consider tilting to the left.
At the same time, in order to avoid a large number of coordinate transformations, the column tilt is actually the horizon \ horizontal plane tilt. Therefore, the column remains unchanged and is set as the origin in the lower left corner of the column. draw a line from the origin point to the bottom right corner as the skewed horizon.
Next, calculate the maximum vertical distance between the top of each column and the horizon. Then, the water must be at a high distance between the two columns.
Find the column with the largest distance. The column on the left must match the column closest to the right and with the column whose distance is greater than or equal to the horizon. the column on the right must match a column on the left. therefore, two loops to the left and right can be used to find all the matching results.
My code:
[Cpp]
/*
Program: hdu_4368
Author: BlackAndWhite
*/
# Include <stdio. h>
# Include <math. h>
# Define eps 1e-5
# Define PI 3.1415926535897932384626
Int n, a [10001];
Double angle;
Int I, j, k, maxtop;
Double dis [10001], t, top, ans, low, h;
Int main ()
{
While (~ Scanf ("% d % lf", & n, & angle ))
{
If (angle <-eps) for (I = n-1; I> = 0; I --) scanf ("% d", & a [I]);
Else for (I = 0; I <n; I ++) scanf ("% d", & a [I]);
If (angle <-eps) angle =-angle; // only the left tilt is considered.
Angle = PI * angle/180.0;
T =-1; ans = 0;
For (I = 0; I <n; I ++)
{
Dis [I] = (I + 1) * tan (angle) + a [I] * 1.0)/sqrt (tan (angle) * tan (angle) + 1 );
If (t <dis [I]) {t = dis [I]; maxtop = I ;}// find the highest column on the ground
}
For (I = 0; I <maxtop; I ++) // front of the highest Column
{
Top = a [I];
For (j = I + 1; j <= maxtop; j ++)
{
If (dis [j]> dis [I] + eps)
{
If (dis [j]-sin (angle)> dis [I] + eps)
{
Top-= (j-i-1) * 1.0 * tan (angle );
Ans + = (j-i-1) * (j-i-1) * 0.5 * tan (angle );
}
Else // trick, the water is just in the middle of a column
{
Top-= (a [I]-a [j]) * 1.0;
Ans + = (a [I]-a [j]) * (a [I]-a [j]) * 0.5/tan (angle );
}
For (k = I + 1; k <j; k ++)
Ans + = (top-a [k]) * 1.0;
I = J-1;
Break;
}
}
}
For (I = n-1; I> maxtop; I --) // right side, relatively simple, no trick
{
For (j = I-1; j> = maxtop; j --)
{
H = I-j;
Low = h * tan (angle)-(a [j + 1]-a [I]);
Top = (h-1) * tan (angle)-(a [j + 1]-a [I]);
Ans + = (top + low)/2.0;
If (dis [j]> dis [I] + eps)
Break;
}
I = j + 1;
}
Printf ("%. 2lf \ n", ans );
}
Return 0;
}
Author: q411307827