CF1824A.LuoTianyi and the Show

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn people taking part in a show about VOCALOID. They will sit in the row of seats, numbered 11 to mm from left to right.

The nn people come and sit in order. Each person occupies a seat in one of three ways:

  1. Sit in the seat next to the left of the leftmost person who is already sitting, or if seat 11 is taken, then leave the show. If there is no one currently sitting, sit in seat mm.
  2. Sit in the seat next to the right of the rightmost person who is already sitting, or if seat mm is taken, then leave the show. If there is no one currently sitting, sit in seat 11.
  3. Sit in the seat numbered xix_i. If this seat is taken, then leave the show.

Now you want to know what is the maximum number of people that can take a seat, if you can let people into the show in any order?

有 nn 个人参加一场关于 VOCALOID 的演出。他们将坐在一排编号从左到右为 11 到 mm 的座位上。

这 nn 个人按某种顺序依次入场就座。每个人以以下三种方式之一选择座位:

  1. 坐在当前已就座者中最左侧者的左侧相邻座位;若该位置不存在(例如最左侧者坐在座位 11 上),则此人离场;若此时尚无任何人就座,则坐在座位 mm 上。
  2. 坐在当前已就座者中最右侧者的右侧相邻座位;若该位置不存在(例如最右侧者坐在座位 mm 上),则此人离场;若此时尚无任何人就座,则坐在座位 11 上。
  3. 坐在编号为 xix_i 的座位上;若该座位已被占用,则此人离场。

现在你想知道:若你可以任意安排这 nn 个人的入场顺序,最多能有多少人成功就座?

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤1051 \le n, m \le 10^5) — the number of people and the number of seats.

The second line of each test case contains nn integers x1,x2,…,xnx_1, x_2, \ldots, x_n (−2≤xi≤m-2 \le x_i \le m, xi≠0x_i \ne 0), the ii-th of which describes the way in which the ii-th person occupies a seat:

  1. If xi=−1x_i=-1, then ii-th person takes the seat in the first way.
  2. If xi=−2x_i=-2, then ii-th person takes the seat in the second way.
  3. If xi>0x_i \gt 0, then the ii-th person takes a seat in the third way, i.e. he wants to sit in the seat with the number xix_i or leave the show if it is occupied..

It is guaranteed that sum of nn and the sum of mm over all test cases don't exceed 10510^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤1051 \le n, m \le 10^5),分别表示人数和座位数。

每个测试用例的第二行包含 nn 个整数 x1,x2,…,xnx_1, x_2, \ldots, x_n(−2≤xi≤m-2 \le x_i \le m,且 xi≠0x_i \ne 0),其中第 ii 个数描述了第 ii 个人就座的方式:

  1. 若 xi=−1x_i = -1,则第 ii 个人以第一种方式就座;
  2. 若 xi=−2x_i = -2,则第 ii 个人以第二种方式就座;
  3. 若 xi>0x_i > 0,则第 ii 个人以第三种方式就座,即他希望坐在编号为 xix_i 的座位上;若该座位已被占用,则他将离开演出。

保证所有测试用例中 nn 的总和与 mm 的总和均不超过 10510^5。

输出格式

For each test case output a single integer — the maximum number of people who can occupy a seat.

对于每个测试用例,输出一个整数——能够就座的人数的最大值。

输入输出样例

  • 输入#1

    10
    3 10
    5 5 5
    4 6
    1 -2 -2 1
    5 7
    -1 -1 4 -2 -2
    6 7
    5 -2 -2 -2 -2 -2
    6 6
    -1 1 4 5 -1 4
    6 8
    -1 -1 -1 3 -1 -2
    6 7
    5 -1 -2 -2 -2 -2
    3 1
    -2 -2 1
    2 5
    5 -2
    1 2
    -1

    输出#1

    1
    3
    5
    6
    5
    5
    5
    1
    2
    1

说明/提示

In the first test case, all the people want to occupy the 55 seat, so only 11 people can occupy the seat.

In the second test case, we can let people in order 1,2,3,41, 2, 3, 4, then all but the last person can take a seat.

In the third test case, we can let people into the show in that order:

Let the third person in:

–

–

–

3

–

–

–

Let the fourth person in:

–

–

–

3

4

–

–

Let the fifth person in:

–

–

–

3

4

5

–

Let the first person in:

–

–

1

3

4

5

–

Let the second person in:

–

2

1

3

4

5

–

Thus, all 55 people took seats.

In the fifth test case, we can let people into the show in this order:

Let the fourth person in:

–

–

–

–

4

–

Let the third person in:

–

–

–

3

4

–

Let the sixth person in, he'll leave the show because he takes the third seat the third way and has to sit in the 44 seat, but it's already taken:

–

–

–

3

4

–

Let the fifth person in:

–

–

5

3

4

–

Let the first person in:

–

1

5

3

4

–

Let the second person in:

2

1

5

3

4

–

Thus, 55 of people took seats.

In the seventh test case, we can let people into the show in this order:

Let the third person in:

3

–

–

–

–

–

–

Let the fourth person in:

3

4

–

–

–

–

–

Let the fifth person in:

3

4

5

–

–

–

–

Let the sixth person in:

3

4

5

6

–

–

–

Let the first person in:

3

4

5

6

1

–

–

Let the second person in, he will leave the show because he occupies the first way, but the 11 seat is taken:

3

4

5

6

1

–

–

Thus, 55 people took seats.

在第一个测试用例中,所有人都想坐第 55 个座位,因此只有 11 个人能坐到该座位。

在第二个测试用例中,我们可以按顺序让第 1,2,3,41, 2, 3, 4 号人入场,则除最后一人外,其余人均可入座。

在第三个测试用例中,我们可以按如下顺序让人入场:

让第三人入场:

–

–

–

3

–

–

–

让第四人入场:

–

–

–

3

4

–

–

让第五人入场:

–

–

–

3

4

5

–

让第一人入场:

–

–

1

3

4

5

–

让第二人入场:

–

2

1

3

4

5

–

因此,全部 55 人都成功入座。

在第五个测试用例中,我们可以按如下顺序让人入场:

让第四人入场:

–

–

–

–

4

–

让第三人入场:

–

–

–

3

4

–

让第六人入场:他将离场,因为他按第三种方式选择座位时本应坐第 33 号座位,但实际需坐第 44 号座位,而该座位已被占用:

–

–

–

3

4

–

让第五人入场:

–

–

5

3

4

–

让第一人入场:

–

1

5

3

4

–

让第二人入场:

2

1

5

3

4

–

因此,共有 55 人成功入座。

在第七个测试用例中,我们可以按如下顺序让人入场:

让第三人入场:

3

–

–

–

–

–

–

让第四人入场:

3

4

–

–

–

–

–

让第五人入场:

3

4

5

–

–

–

–

让第六人入场:

3

4

5

6

–

–

–

让第一人入场:

3

4

5

6

1

–

–

让第二人入场:他将离场,因为他按第一种方式选择座位时本应坐第 11 号座位,但该座位已被占用:

3

4

5

6

1

–

–

因此,共有 55 人成功入座。

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

首页