#P17329. [ICPC 2018 Nanjing R] Lagrange the Chef

    ID: 19671 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>动态规划 DP2018ICPC南京

[ICPC 2018 Nanjing R] Lagrange the Chef

Problem Description

Lagrange is a chef. He has developed several theories related to food.

In those theories, the most famous one is called Compatible Theory. Specifically, Lagrange has invented 10610^6 different dishes. However, one pair of dishes, namely XX and YY, when tasted one after another (regardless of order), would have a negative impact on the dining experience. Lagrange called the pairs of dishes "incompatible".

The concept of "compatible" can also be extended to a full meal. A meal, consisting of several dishes served in a specific order, is compatible if no two dishes served consecutively are incompatible.

One day, a guest requests a meal consists of NN dishes a0,a1,,aN1a_0, a_1,\cdots,a_{N-1} to be served in order. Since the guest didn't know Compatible Theory, the requested meal can be incompatible.

Lagrange wants to adjust the order of dishes to make the meal compatible while keeping the adjusted list not differ a lot from the original one. Hence, he defines an "adjusting step" as moving a dish in the list to any other position.

Your job is to calculate the minimum number of adjusting step required to make the meal compatible, or determine that this is impossible.

Input Format

The first line contains three positive integers N,X,YN, X, Y (1N5000,1X,Y106,XY1 \le N \le 5000, 1 \le X, Y \le 10^6, X \ne Y).

The second line contains NN positive integers, a0,a1,,aN1a_0, a_1,\cdots,a_{N-1} (1ai1061 \le a_i \le 10^6).

Output Format

If it is not possible to make the meal compatible, print -1\texttt{-1}. You should not print the quotation marks.

Otherwise, print an integer \text{---} the minimum number of adjusting step required to make the meal compatible.

3 1 2
1 2 3
1
3 1 2
1 2 2
-1