No.1095 Smallest Kadomatsu Subsequence
Problem Statement
A sequence $A,B,C$ of $3$ elements is called a Kadomatsu sequence if it satisfies the following conditions.
Also, a subsequence of $3$ elements of a sequence (not necessarily contiguous) that is a Kadomatsu sequence is called a Kadomatsu subsequence.
You are given a sequence $A_1,A_2,\cdots,A_N$ of $N$ elements, in which no $2$ elements are equal.
Find the size of the smallest-sized Kadomatsu subsequence of this sequence.
However, if the sequence $A_1,A_2,\cdots,A_N$ has no Kadomatsu subsequence, output
-1.
Input
$N$ $A_1 \ A_2 \ \cdots \ A_N$
The input satisfies the following constraints.
- All input values are integers
- $3 \leq N \leq 2 \times 10^5$
- $1 \leq A_i \leq 10^8 (1 \leq i \leq N)$
- $A_i \neq A_j (1 \leq i < j \leq N)$
Output
Output the size of the smallest-sized Kadomatsu subsequence of the sequence $A_1,A_2,\cdots,A_N$.
However, if the sequence $A_1,A_2,\cdots,A_N$ has no Kadomatsu subsequence, output -1.
Sample
Sample 1
Input
4 1 2 4 3
Output
8
The sequence $1,2,4,3$ has $2$ Kadomatsu subsequences.
Sample 2
Input
5 1 4 6 2 3
Output
7
Sample 3
Input
4 1 2 3 4
Output
-1
The sequence $1,2,3,4$ has no Kadomatsu subsequence, so output -1.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
SSRS
platinum