Hydrocephalus

Turtle house roof is uneven, so every time the rain will water. In order to know whether the roof would fall off after heavy rains, the shape of the roof he gave you, I hope you help him calculate the total amount of water after heavy rains the roof.
Turtle by the roof are arranged in order in the same horizontal line n of width 1 and a height of an integer (respectively shown) composed of tiles. Given e.g. n = 5, the height of the tile, respectively 4, 2, 3, 5, 1, the roof can be meshed as shown in FIG lower, grid to gray tiles.
After the storm, if the left and right sides of a square extending square tiles occupy can reach, it will water. Therefore, the wavy line in FIG grid will be in the storm water, the total number of squares of the roof 3 water.

Input
two integers n, R1, represents a width of the first item and the number of columns generated roof. From left to right of the i (1 <= i <= n ) of the height of tiles ai = Ri mod 10
the number of columns used to generate questions R is defined as follows: an integer of 0 ≤ R1 <201701 are given in the input. For i> 1, Ri = (Ri -1 × 6807 + 2831) mod 201701.

About the highest post in the smaller of the two sides and compare current columns

void init() {
    cin >>n>>R1;
    for (int i=0; i<n; ++i) {
        height.push_back(R1%10);
        R1 = (R1*6807 + 2831) % 201701;
    }
}

int trap() {
    int left = 0, right = n-1;//双指针
    int ans = 0;
    int l_max = height[0];
    int r_max = height[n-1];
    
    while (left <= right) {
        l_max = max(l_max, height[left]);
        r_max = max(r_max, height[right]);
//        min(l_max,r_max) - height
        if (l_max < r_max) {
            ans += l_max - height[left];
            ++left;
        } else {
            ans += r_max - height[right];
            --right;
        }
    }
    return ans;
}

Guess you like

Origin www.cnblogs.com/i-8023-/p/12091664.html