#USACOC2223B. Cow Frisbee
Cow Frisbee
Description
Farmer John's cows ( have heights . One day, the cows arestanding in a line in some order playing frisbee; let denotethe heights of the cows in this order (so the 's are a permutation of).
Two cows at positions and in the line can successfully throw the frisbeeback and forth if and only if every cow between them has height lower than.
Please compute the sum of distances between all pairs of locations atwhich there resides a pair of cows that can successfully throw the frisbee backand forth. The distance between locations and is .
Input Format
The first line of input contains a single integer . The next line of inputcontains , separated by spaces.
Output Format
Output the sum of distances of all pairs of locations at which there are cowsthat can throw the frisbee back and forth. Note that the large size ofintegers involved in this problem may require the use of 64-bit integer datatypes (e.g., a long long
in C/C++).
7
4 3 1 2 5 6 7
7
4 3 1 2 5 6 7
Scoring
- Test cases 1-3 satisfy .
- Test cases 4-11 satisfy noadditional constraints.
Problem Credit
Problem credits: Quanquan Liu