#P7477. 「C.E.L.U-02」划分可重集

    ID: 6192 远端评测题 5000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>O2优化图的建立,建图2-SAT主席树

「C.E.L.U-02」划分可重集

Description

You are given a sequence vv of length nn. Please partition it into two multisets aa and bb. You will partition from left to right, and each number must be put into at least one multiset.

A number viv_i can be put into aa if and only if all vjv_j with j<i and vjvikj<i \ and\ v_j\le v_i-k have not been put into the current aa.

A number viv_i can be put into bb if and only if all vjv_j with j<i and vjvi+kj<i\ and\ v_j\ge v_i+k have not been put into the current bb.

At the same time, mm pairs of relations are given. Each relation means that uu and vv cannot be put into the same multiset. Find the minimum kk that makes a successful partition possible. If no valid partition exists, output -1.

Input Format

The first line contains two integers n,mn,m, as described in the statement.
The next line contains nn integers, representing vv.
The following mm lines each contain two integers, representing one relation.

Output Format

Output one integer, the answer.

6 0
6 2 8 5 7 3
2
10 3
1 3 4 3 8 2 3 4 5 6
2 3
6 7
1 9

5

Hint

Sample Explanation

Sample Explanation 1

The following is one valid partition:
|6|2|8|5|7|3| |:---:|:---:|:---:|:---:|:---:|:---:| |a|b|b|a|b|a|

Sample Explanation 2

The following is one valid partition:
|1|3|4|3|8|2|3|4|5|6| |:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:| |b|b|a|b|a|b|a|a|a|b|

Constraints

Test ID nn mm
121\sim2 103\le10^3 00
343\sim4 103\le10^3
565\sim6 2×104\le2\times10^4 00
7107\sim10 2×104\le2\times10^4

For 100%100\% of the testdata, n,m2×104n,m\le2\times10^4 and vi109v_i\le10^9. It is guaranteed that u<vnu<v\le n, and there are no duplicate pairs (u,v)(u,v).

Translated by ChatGPT 5