問題一覧 > 通常問題

No.121 Trends and Countermeasures: Kadomatsu Sequences (Part 2)

レベル : / AI推定難易度 : 1300 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 66
作問者 : LayCurse
お気に入りにしたユーザー ProblemId : 292 / 自分の提出
問題文最終更新日: 2026-06-25 02:17:04
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 74 (順位表) の他の問題:

Problem Statement

Over the past few days, problems about Kadomatsu sequences have been appearing frequently.
Here, let us deepen our understanding of Kadomatsu sequences through practice, so that we can solve Kadomatsu sequence problems whenever they are set.
As those of you taking this Kadomatsu sequence preparation course may already know, a Kadomatsu sequence is a sequence $a,b,c$ consisting of $3$ elements that satisfies the following $2$ conditions.
・$a,b,c$ are all distinct
・Among the $3$ elements, $b$ is the largest, or $b$ is the smallest

Now, Yuki has an integer sequence $A_1,A_2,\ldots,A_N$ of $N$ elements.
Yuki wants to know how many times a Kadomatsu sequence appears as a subsequence of this array.
Write a program that finds the number of Kadomatsu sequences.
That is, write a program that finds the number of $3$-tuples of integers $(i,j,k)$ satisfying the following condition.
・$1 \leq i < j < k \leq N$ and $A_i,A_j,A_k$ is a Kadomatsu sequence

Input

$N$
$A_1$ $A_2$ $\cdots$ $A_N$

$1 \leq N \leq 1000000 = 10^6$
$1 \leq A_k \leq 1000000000 = 10^9$

Output

Output the answer on $1$ line.

Sample

Sample 1
Input
5
1 4 2 5 2
Output
5

There are the following $5$ occurrences:
・When $(i,j,k) = (1,2,3)$, $(A_i,A_j,A_k) = (1,4,2)$
・When $(i,j,k) = (1,2,5)$, $(A_i,A_j,A_k) = (1,4,2)$
・When $(i,j,k) = (1,4,5)$, $(A_i,A_j,A_k) = (1,5,2)$
・When $(i,j,k) = (2,3,4)$, $(A_i,A_j,A_k) = (4,2,5)$
・When $(i,j,k) = (2,4,5)$, $(A_i,A_j,A_k) = (4,5,2)$
The Kadomatsu sequence $1,4,2$ is included $2$ times, but since their positions differ, both are counted.

Sample 2
Input
2
1 1
Output
0

There is no subsequence of length $3$ in the first place.

Sample 3
Input
35
1 3 5 5 4 3 5 5 5 3 3 4 2 5 4 4 3 2 3 1 3 1 5 1 5 1 2 1 5 2 4 1 4 2 1
Output
2015

Happy New Year.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。