아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Cow Frisbee

시간 제한1초메모리 제한1024 MB

요약
소의 키 순열이 주어질 때, 두 소 사이의 모든 소가 둘 다보다 작은 쌍 (i, j)의 거리 j-i+1의 합을 구한다.
난이도

보통10점 중 7점

유형
스택, 배열, 그리디, 구현
정답자
아직 제출이 없습니다

문제

Farmer John's NN cows (N≤3×105)N \leq 3 \times 10^5) have heights 1,2,…,N1, 2, \ldots, N. One day, the cows are standing in a line in some order playing frisbee; let h_1…h_Nh\_1 \ldots h\_N denote the heights of the cows in this order (so the hh's are a permutation of 1…N1 \ldots N).

Two cows at positions ii and jj in the line can successfully throw the frisbee back and forth if and only if every cow between them has height lower than min⁡(h_i,h_j)\min(h\_i, h\_j).

Please compute the sum of distances between all pairs of locations i\<ji\<j at which there resides a pair of cows that can successfully throw the frisbee back and forth. The distance between locations ii and jj is j−i+1j-i+1.

입력

The first line of input contains a single integer NN. The next line of input contains h_1…h_Nh\_1 \ldots h\_N, separated by spaces.

출력

Output the sum of distances of all pairs of locations at which there are cows that can throw the frisbee back and forth. Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

힌트

The pairs of successful locations in this example are as follows:

(1, 2), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (4, 5), (5, 6), (6, 7)

예제1

  1. 예제 1

    입력
    7
    4 3 1 2 5 6 7
    
    예상 출력
    24