310166: CF1791G2. Teleporters (Hard Version)

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Teleporters (Hard Version)

题意翻译

一条直线上有 $n+2$ 个点 $0,1,2,...,n+1$,其中 $1,2,...,n$ 号点上有传送点,你刚开始在 $0$ 号点,有 $c$ 枚金币。如果你现在在 $i$ 号点,你可以: - 当 $i\neq0$ 时,向左走一步,花费 $1$ 金币。 - 当 $i\neq n+1$ 时,向右走一步,花费 $1$ 金币。 - 当 $i$ 号点有传送点时,花费 $a_i$ 个金币传送到 $0$ 号点或者 $n+1$ 号点(你可以选择传送到哪儿),同时 $i$ 号点的传送点消失。 求你最多能使用几个传送点。

题目描述

The only difference between the easy and hard versions are the locations you can teleport to. Consider the points $ 0,1,\dots,n+1 $ on the number line. There is a teleporter located on each of the points $ 1,2,\dots,n $ . At point $ i $ , you can do the following: - Move left one unit: it costs $ 1 $ coin. - Move right one unit: it costs $ 1 $ coin. - Use a teleporter at point $ i $ , if it exists: it costs $ a_i $ coins. As a result, you can choose whether to teleport to point $ 0 $ or point $ n+1 $ . Once you use a teleporter, you can't use it again. You have $ c $ coins, and you start at point $ 0 $ . What's the most number of teleporters you can use?

输入输出格式

输入格式


The input consists of multiple test cases. The first line contains an integer $ t $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $ n $ and $ c $ ( $ 1 \leq n \leq 2\cdot10^5 $ ; $ 1 \leq c \leq 10^9 $ ) — the length of the array and the number of coins you have respectively. The following line contains $ n $ space-separated positive integers $ a_1,a_2,\dots,a_n $ ( $ 1 \leq a_i \leq 10^9 $ ) — the costs to use the teleporters. It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2\cdot10^5 $ .

输出格式


For each test case, output the maximum number of teleporters you can use.

输入输出样例

输入样例 #1

10
5 6
1 1 1 1 1
8 32
100 52 13 6 9 4 100 35
1 1
5
4 5
4 3 2 1
5 9
2 3 1 4 1
5 8
2 3 1 4 1
4 3
2 3 4 1
4 9
5 4 3 3
2 14
7 5
5 600000000
500000000 400000000 300000000 200000000 100000000

输出样例 #1

2
3
0
1
3
2
1
1
2
2

说明

In the first test case, you can move one unit to the right, use the teleporter at index $ 1 $ and teleport to point $ n+1 $ , move one unit to the left and use the teleporter at index $ 5 $ . You are left with $ 6-1-1-1-1 = 2 $ coins, and wherever you teleport, you won't have enough coins to use another teleporter. You have used two teleporters, so the answer is two. In the second test case, you go four units to the right and use the teleporter to go to $ n+1 $ , then go three units left and use the teleporter at index $ 6 $ to go to $ n+1 $ , and finally, you go left four times and use the teleporter. The total cost will be $ 4+6+3+4+4+9 = 30 $ , and you used three teleporters. In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.

Input

题意翻译

一条直线上有 $n+2$ 个点 $0,1,2,...,n+1$,其中 $1,2,...,n$ 号点上有传送点,你刚开始在 $0$ 号点,有 $c$ 枚金币。如果你现在在 $i$ 号点,你可以: - 当 $i\neq0$ 时,向左走一步,花费 $1$ 金币。 - 当 $i\neq n+1$ 时,向右走一步,花费 $1$ 金币。 - 当 $i$ 号点有传送点时,花费 $a_i$ 个金币传送到 $0$ 号点或者 $n+1$ 号点(你可以选择传送到哪儿),同时 $i$ 号点的传送点消失。 求你最多能使用几个传送点。

Output

**题意翻译**

在一条直线上有 $n+2$ 个点 $0,1,2,...,n+1$,其中 $1,2,...,n$ 号点上有传送点。你从 $0$ 号点出发,有 $c$ 枚金币。如果你现在在 $i$ 号点,你可以:

- 当 $i\neq0$ 时,向左走一步,花费 $1$ 金币。
- 当 $i\neq n+1$ 时,向右走一步,花费 $1$ 金币。
- 当 $i$ 号点有传送点时,花费 $a_i$ 个金币传送到 $0$ 号点或者 $n+1$ 号点(你可以选择传送到哪儿),同时 $i$ 号点的传送点消失。

求你最多能使用几个传送点。

**题目描述**

简单版本和困难版本的区别在于你可以传送到的位置。

考虑数轴上的点 $ 0,1,\dots,n+1 $。每个点 $ 1,2,\dots,n $ 上都有一个传送器。在点 $ i $,你可以:

- 向左移动一个单位:花费 $ 1 $ 枚金币。
- 向右移动一个单位:花费 $ 1 $ 枚金币。
- 使用点 $ i $ 上的传送器(如果存在):花费 $ a_i $ 枚金币。结果是你可以选择传送到点 $ 0 $ 或点 $ n+1 $。一旦你使用了传送器,你就不能再使用它。

你有 $ c $ 枚金币,从点 $ 0 $ 开始。你能使用的传送器最多有几个?

**输入输出格式**

**输入格式**

输入由多个测试案例组成。第一行包含一个整数 $ t $($ 1 \leq t \leq 1000 $)——测试案例的数量。接下来是每个测试案例的描述。

每个测试案例的第一行包含两个整数 $ n $ 和 $ c $($ 1 \leq n \leq 2\cdot10^5 $;$ 1 \leq c \leq 10^9 $)——数组的长度和你的金币数量。

接下来的一行包含 $ n $ 个由空格分隔的正整数 $ a_1,a_2,\dots,a_n $($ 1 \leq a_i \leq 10^9 $)——使用传送器的费用。

保证所有测试案例的 $ n $ 之和不超过 $ 2\cdot10^5 $。

**输出格式**

对于每个测试案例,输出你能使用的传送器的最大数量。

**输入输出样例**

**输入样例 #1**

```
10
5 6
1 1 1 1 1
8 32
100 52 13 6 9 4 100 35
1 1
5
4 5
4 3 2 1
5 9
2 3 1 4 1
5 8
2 3 1 4 1
4 3
2 3 4 1
4 9
5 4 3 3
2 14
7 5
5 600000000
500000000 400000000 300000000 200000000 100000000
```

**输出样例 #1**

```
2
3
0
1
3
2
1
1
2
2
```

**说明**

在第一个测试案例中,你可以向右移动一个单位,使用索引 $ 1 $ 处的传送器并传送到点 $ n+1 $,向左移动一个单位并使用索引 $ 5 $ 处的传送器。你剩下 $ 6-1-1-1-1 = 2 $ 枚金币,无论你传送到哪里,你都没有足够的金币再使用另一个传送器。你使用了两个传送器,所以答案是两个。

在第二个测试案例中,你向右移动四个单位并使用传送器传送到 $ n+1 $,然后向左移动三个单位并使用索引 $ 6 $ 处的传送器传送到 $ n+1 $,最后你向左移动四次并使用传送器。总花费将是 $ 4+6+3+4+4+9 = 30 $,你使用了三个传送器。

在第三个测试案例中,你没有足够的金币使用任何传送器,所以答案是零。**题意翻译** 在一条直线上有 $n+2$ 个点 $0,1,2,...,n+1$,其中 $1,2,...,n$ 号点上有传送点。你从 $0$ 号点出发,有 $c$ 枚金币。如果你现在在 $i$ 号点,你可以: - 当 $i\neq0$ 时,向左走一步,花费 $1$ 金币。 - 当 $i\neq n+1$ 时,向右走一步,花费 $1$ 金币。 - 当 $i$ 号点有传送点时,花费 $a_i$ 个金币传送到 $0$ 号点或者 $n+1$ 号点(你可以选择传送到哪儿),同时 $i$ 号点的传送点消失。 求你最多能使用几个传送点。 **题目描述** 简单版本和困难版本的区别在于你可以传送到的位置。 考虑数轴上的点 $ 0,1,\dots,n+1 $。每个点 $ 1,2,\dots,n $ 上都有一个传送器。在点 $ i $,你可以: - 向左移动一个单位:花费 $ 1 $ 枚金币。 - 向右移动一个单位:花费 $ 1 $ 枚金币。 - 使用点 $ i $ 上的传送器(如果存在):花费 $ a_i $ 枚金币。结果是你可以选择传送到点 $ 0 $ 或点 $ n+1 $。一旦你使用了传送器,你就不能再使用它。 你有 $ c $ 枚金币,从点 $ 0 $ 开始。你能使用的传送器最多有几个? **输入输出格式** **输入格式** 输入由多个测试案例组成。第一行包含一个整数 $ t $($ 1 \leq t \leq 1000 $)——测试案例的数量。接下来是每个测试案例的描述。 每个测试案例的第一行包含两个整数 $ n $ 和 $ c $($ 1 \leq n \leq 2\cdot10^5 $;$ 1 \leq c \leq 10^9 $)——数组的长度和你的金币数量。 接下来的一行包含 $ n $ 个由空格分隔的正整数 $ a_1,a_2,\dots,a_n $($ 1 \leq a_i \leq 10^9 $)——使用传送器的费用。 保证所有测试案例的 $ n $ 之和不超过 $ 2\cdot10^5 $。 **输出格式** 对于每个测试案例,输出你能使用的传送器的最大数量。 **输入输出样例** **输入样例 #1** ``` 10 5 6 1 1 1 1 1 8 32 100 52 13 6 9 4 100 35 1 1 5 4 5 4 3 2 1 5 9 2 3 1 4 1 5 8 2 3 1 4 1 4 3 2 3 4 1 4 9 5 4 3 3 2 14 7 5 5 600000000 500000000 400000000 300000000 200000000 100000000 ``` **输出样例 #1** ``` 2 3 0 1 3 2 1 1 2 2 ``` **说明** 在第一个测试案例中,你可以向右移动一个单位,使用索引 $ 1 $ 处的传送器并传送到点 $ n+1 $,向左移动一个单位并使用索引 $ 5 $ 处的传送器。你剩下 $ 6-1-1-1-1 = 2 $ 枚金币,无论你传送到哪里,你都没有足够的金币再使用另一个传送器。你使用了两个传送器,所以答案是两个。 在第二个测试案例中,你向右移动四个单位并使用传送器传送到 $ n+1 $,然后向左移动三个单位并使用索引 $ 6 $ 处的传送器传送到 $ n+1 $,最后你向左移动四次并使用传送器。总花费将是 $ 4+6+3+4+4+9 = 30 $,你使用了三个传送器。 在第三个测试案例中,你没有足够的金币使用任何传送器,所以答案是零。

加入题单

算法标签: