text
stringlengths 0
801
|
---|
Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation.
|
An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
|
The first line of each test case contains two integers $n, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le \frac{n}{2}$) — the length of the permutation and the number of intervals.
|
The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) — the $i$-th interval.
|
Additional constraint on the input in this version: $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$.
|
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.
|
For each test case, if there is no interesting permutation, output a single integer $-1$.
|
Otherwise, output a single integer — the maximum number of inversions.
|
In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$.
|
In the fourth test case, the interesting permutation with the maximum number of inversions is $[4, 8, 7, 6, 3, 2, 1, 5]$. In this case, we can let $[k
|
This is a hard version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved.
|
Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i < r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds:
|
$$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$
|
Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation.
|
An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
|
The first line of each test case contains two integers $n, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le 5 \cdot 10^3$) — the length of the permutation and the number of intervals.
|
The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) — the $i$-th interval. Note that there may exist identical intervals (i.e., there may exist two different indices $i, j$ such that $l_i = l_j$ and $r_i = r_j$).
|
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^3$ and the sum of $m$ over all test cases does not exceed $5 \cdot 10^3$.
|
For each test case, if there is no interesting permutation, output a single integer $-1$.
|
Otherwise, output a single integer — the maximum number of inversions.
|
In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$.
|
In the fourth test case, the interesting permutation
|
Piggy gives Turtle three sequences $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$, and $c_1, c_2, \ldots, c_n$.
|
Turtle will choose a subsequence of $1, 2, \ldots, n$ of length $m$, let it be $p_1, p_2, \ldots, p_m$. The subsequence should satisfy the following conditions:
|
* $a_{p_1} \le a_{p_2} \le \cdots \le a_{p_m}$; * All $b_{p_i}$ for all indices $i$ are pairwise distinct, i.e., there don't exist two different indices $i$, $j$ such that $b_{p_i} = b_{p_j}$.
|
Help him find the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$, or tell him that it is impossible to choose a subsequence of length $m$ that satisfies the conditions above.
|
Recall that a sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements.
|
The first line contains two integers $n$ and $m$ ($1 \le n \le 3000$, $1 \le m \le 5$) — the lengths of the three sequences and the required length of the subsequence.
|
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of the sequence $a$.
|
The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$) — the elements of the sequence $b$.
|
The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^4$) — the elements of the sequence $c$.
|
Output a single integer — the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$. If it is impossible to choose a subsequence of length $m$ that satisfies the conditions above, output $-1$.
|
In the first example, we can choose $p = [1, 2]$, then $c_{p_1} + c_{p_2} = 1 + 4 = 5$. We can't choose $p = [2, 4]$ since $a_2 > a_4$, violating the first condition. We can't choose $p = [2, 3]$ either since $b_2 = b_3$, violating the second condition. We can choose $p = [1, 4]$, but $c_1 + c_4 = 4$, which isn't maximum.
|
In the second example, we can choose $p = [4, 6, 7]$.
|
In the third example, it is impossible to choose a subsequence of length $3$ that satisfies both of the conditions.
|
Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$.
|
The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to $k$ is strictly less than the distance from $j$ to $i$. In other words, all other points from the set have distance to $j$ greater or equal to $|i - j|$.
|
For example, consider a set of points $\\{1, 3, 5, 8\\}$:
|
* for the point $1$, the closest point is $3$ (other points have distance greater than $|1-3| = 2$); * for the point $3$, there are two closest points: $1$ and $5$; * for the point $5$, the closest point is $3$ (but not $8$, since its distance is greater than $|3-5|$); * for the point $8$, the closest point is $5$.
|
You are given a set of points. You have to add an integer point into this set in such a way that it is different from every existing point in the set, and it becomes the closest point to every point in the set. Is it possible?
|
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
|
Each test case consists of two lines:
|
* the first line contains one integer $n$ ($2 \le n \le 40$) — the number of points in the set; * the second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_1 < x_2 < \dots < x_n \le 100$) — the points from the set.
|
For each test case, print YES if it is possible to add a new point according to the conditions from the statement. Otherwise, print NO.
|
In the first example, the point $7$ will be the closest to both $3$ and $8$.
|
In the second example, it is impossible to add an integer point so that it becomes the closest to both $5$ and $6$, and is different from both of them.
|
There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked.
|
We say that room $x$ is reachable from room $y$ if all doors between them are unlocked.
|
You know that:
|
* Alice is in some room from the segment $[l, r]$; * Bob is in some room from the segment $[L, R]$; * Alice and Bob are in different rooms.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.