CF2249A.Rank Subsequence

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn elements arranged in a line, numbered 1,2,…,n1, 2, \ldots, n from left to right.

You may delete any number of elements (possibly none). The remaining elements form a subsequence and keep their relative order. Let the length of this subsequence be mm. If the element with original index ii becomes the jj-th element of the subsequence (1≤j≤m1 \le j \le m), define

  • its left rank as jj, and
  • its right rank as m−j+1m - j + 1.

For each element, you are given two integer segments [li,ri][l_i, r_i] and [ui,vi][u_i, v_i]. The ii-th element is valid in a subsequence of length mm if and only if, when it occupies position jj in that subsequence, both of the following hold:

  • Its left rank is not in the range [li,ri][l_i, r_i] (i.e., j∉[li,ri]j \notin [l_i, r_i]);
  • Its right rank is not in the range [ui,vi][u_i, v_i] (i.e., m−j+1∉[ui,vi]m - j + 1 \notin [u_i, v_i]).

A subsequence is valid if every element remaining in it is valid.

Find the maximum possible length of a valid subsequence. The answer may be 00.

给你 nn 个元素,从左到右依次编号为 1,2,…,n1, 2, \ldots, n。

你可以删除任意数量的元素(也可以一个都不删)。剩余元素构成一个子序列,并保持其原有相对顺序。设该子序列的长度为 mm。若原编号为 ii 的元素在该子序列中处于第 jj 个位置(1≤j≤m1 \le j \le m),则定义:

  • 它的左秩为 jj;
  • 它的右秩为 m−j+1m - j + 1。

对每个元素 ii,给定两个整数区间 [li,ri][l_i, r_i] 和 [ui,vi][u_i, v_i]。第 ii 个元素在长度为 mm 的子序列中是有效的,当且仅当它在该子序列中占据位置 jj 时,同时满足以下两个条件:

  • 它的左秩不在区间 [li,ri][l_i, r_i] 内(即 j∉[li,ri]j \notin [l_i, r_i]);
  • 它的右秩不在区间 [ui,vi][u_i, v_i] 内(即 m−j+1∉[ui,vi]m - j + 1 \notin [u_i, v_i])。

一个子序列是有效的,当且仅当其中所有保留的元素均有效。

求一个有效子序列可能达到的最大长度。答案可以为 00。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001 \le t \le 5000). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤50001\le n\le 5000) — the number of elements.

Then nn lines follow, the ii-th line containing the four integers lil_i, rir_i, uiu_i, and viv_i (1≤i≤n1\le i\le n, 1≤li≤ri≤n1\le l_i\le r_i\le n, 1≤ui≤vi≤n1\le u_i\le v_i\le n).

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤50001 \le t \le 5000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤50001\le n\le 5000)—— 元素个数。

接下来是 nn 行,其中第 ii 行包含四个整数 lil_i、rir_i、uiu_i 和 viv_i(1≤i≤n1\le i\le n,1≤li≤ri≤n1\le l_i\le r_i\le n,1≤ui≤vi≤n1\le u_i\le v_i\le n)。

保证所有测试用例的 nn 值之和不超过 50005000。

输出格式

For each test case, print one integer — the maximum possible length of a valid subsequence.

对于每个测试用例,输出一个整数——有效子序列的最大可能长度。

输入输出样例

  • 输入#1

    5
    1
    1 1 1 1
    4
    2 2 2 2
    1 1 1 1
    1 1 1 1
    1 1 2 2
    5
    1 1 3 3
    2 2 2 2
    3 3 1 1
    1 5 1 5
    5 5 5 5
    2
    2 2 1 1
    1 1 2 2
    6
    1 1 6 6
    1 2 5 6
    2 3 4 5
    3 4 3 4
    4 5 2 3
    5 6 1 2

    输出#1

    0
    4
    3
    2
    3

说明/提示

In the first test case, the only element cannot form a valid subsequence of length 11, so the answer is 00.

In the second test case, all 44 elements can be kept. Their pairs of left and right ranks are (1,4)(1, 4), (2,3)(2, 3), (3,2)(3, 2), and (4,1)(4, 1), respectively, and all of them are valid.

In the third test case, one optimal choice is to keep the elements with original indices 22, 33, and 55.

In the fourth test case, keeping both elements yields a valid subsequence of length 22. Note that no valid subsequence of length 11 exists.

In the fifth test case, one optimal choice is to keep the elements with original indices 33, 44, and 55.

在第一个测试用例中,唯一的一个元素无法构成长度为 11 的有效子序列,因此答案为 00。

在第二个测试用例中,全部 44 个元素均可保留。它们对应的左右排名对分别为 (1,4)(1, 4)、(2,3)(2, 3)、(3,2)(3, 2) 和 (4,1)(4, 1),且全部有效。

在第三个测试用例中,一种最优选择是保留原始下标为 22、33 和 55 的元素。

在第四个测试用例中,保留两个元素可得到一个长度为 22 的有效子序列。注意:不存在长度为 11 的有效子序列。

在第五个测试用例中,一种最优选择是保留原始下标为 33、44 和 55 的元素。

输入解题思路,AI测评打分。不知道怎么写?

首页