PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000068. Bức ảnh bò cân đối (Balanced Photo)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bức ảnh bò cân đối (Balanced Photo)

Balanced Photo

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Farmer John lines up NN cows for a photograph. Cow ii has height hih_i, and all heights are distinct.

For each cow, let LiL_i be the number of taller cows to its left, and RiR_i the number of taller cows to its right. A cow is unbalanced if the larger of Li,RiL_i,R_i is strictly greater than twice the smaller. This includes the case where one side has no taller cows but the other side has at least one.

Count the unbalanced cows in the photograph.

Input

The first line contains NN.

Next come NN integers h1,…,hNh_1,\ldots,h_N in left-to-right order; the heights may be given one per line.

Output

Print one integer: the number of unbalanced cows.

Subtasks

  • Subtask 1 (20 points): 1≤N≤601\le N\le 60; 0≤hi≤1090\le h_i\le 10^9 for all cows; all heights are pairwise distinct.

  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 0≤hi≤1090\le h_i\le 10^9 for all cows; all heights are pairwise distinct.

  • Subtask 3 (50 points): 1≤N≤1000001\le N\le 100000; 0≤hi≤1090\le h_i\le 10^9 for all cows; all heights are pairwise distinct.

Examples

Input

7
34
6
23
0
5
99
2

Output

3

Explanation

The heights are [34,6,23,0,5,99,2][34,6,23,0,5,99,2]. The three unbalanced cows are:

  • Height 3434: L=0L=0, R=1R=1 (only height 9999), and 1>2⋅01>2\cdot0.
  • Height 55: L=3L=3 (heights 34,6,2334,6,23), R=1R=1 (height 9999), so 3>2⋅13>2\cdot1.
  • Height 22: L=5L=5 (heights 34,6,23,5,9934,6,23,5,99), R=0R=0.

The other four cows fail the strict inequality. Therefore the answer is 3.