CF2187E.Doors and Keys

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

There are n+1n+1 rooms arranged in a line, numbered from 11 to n+1n+1 from left to right. There are also nn doors numbered from 11 to nn, where the ii-th door connects rooms ii and i+1i+1. Each door ii has an associated non-negative integer aia_i.

You are given a binary string∗^{\text{∗}} ss of length nn. For each 1≤i≤n1 \le i \le n:

  • If si=1s_i=\texttt{1}, then room ii initially contains one key.
  • Otherwise, room ii contains no key.

At the beginning of the 00-th second, you are in room 11, and all doors are closed. For each k≥0k \ge 0, the following events will happen in order in the kk-th second:

  • At the beginning of the kk-th second, all doors jj with aj=ka_j=k will open.
  • Suppose you are in room ii:
    • If you are not carrying any keys and there is at least one key in room ii, you may pick it up and carry it with you.
    • If you are carrying a key, you may drop it in room ii.
  • At the end of the kk-th second:
    • You may move to room i+1i+1. If door ii is still closed, you have to be carrying a key to move to room i+1i+1. The key will be consumed after use, and door ii will open.
    • You may move to room i−1i-1 if i>1i \gt 1.
    • You may stay in room ii.

For any door, no matter how it was opened (automatically or with a key), it will remain open forever.

At any moment, you may carry with you at most one key. Also, even though initially each room contains at most one key, during the process, multiple keys may be placed in the same room.

For each 1≤i≤n1 \le i \le n, find the minimum number of seconds required to reach room i+1i+1. Note that the answers to each ii are independent.

∗^{\text{∗}}A binary string is a string consisting of only 0 or 1.

有 n+1n+1 个房间排成一行,从左到右编号为 11 到 n+1n+1。另有 nn 扇门,编号为 11 到 nn,其中第 ii 扇门连接房间 ii 和 i+1i+1。每扇门 ii 关联一个非负整数 aia_i。

给定一个长度为 nn 的二进制字符串∗^{\text{∗}} ss。对每个 1≤i≤n1 \le i \le n:

  • 若 si=1s_i=\texttt{1},则房间 ii 初始时包含一把钥匙;
  • 否则,房间 ii 不含钥匙。

在第 00 秒初,你位于房间 11,且所有门均处于关闭状态。对每个 k≥0k \ge 0,在第 kk 秒内将按如下顺序发生以下事件:

  • 在第 kk 秒初,所有满足 aj=ka_j=k 的门 jj 将自动开启;
  • 假设你当前位于房间 ii:
    • 若你未携带任何钥匙,且房间 ii 中至少有一把钥匙,则你可以拾取其中一把并随身携带;
    • 若你正携带一把钥匙,则你可以将其丢弃在房间 ii 中;
  • 在第 kk 秒末:
    • 你可以移动至房间 i+1i+1;若门 ii 仍处于关闭状态,则你必须携带一把钥匙才能通过,且该钥匙将在使用后被消耗,同时门 ii 将开启;
    • 若 i>1i > 1,你可以移动至房间 i−1i-1;
    • 你可以停留在房间 ii 中。

对于任意一扇门,无论其是自动开启还是通过使用钥匙开启,一旦开启,将永远保持开启状态。

在任意时刻,你最多只能携带一把钥匙。此外,尽管初始时每个房间至多含有一把钥匙,但在整个过程中,同一房间中可能放置多把钥匙。

对每个 1≤i≤n1 \le i \le n,求到达房间 i+1i+1 所需的最少秒数。注意:各 ii 对应的答案相互独立。

∗^{\text{∗}}二进制字符串是指仅由字符 0 或 1 组成的字符串。

输入格式

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

The first line of each test case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of doors.

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai≤2⋅1050 \le a_i \le 2 \cdot 10^5) — the integers associated with each door.

The third line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 门的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai≤2⋅1050 \le a_i \le 2 \cdot 10^5)—— 与每扇门关联的整数。

每个测试用例的第三行包含一个长度为 nn 的二进制字符串 ss。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output nn integers, where the ii-th integer represents the minimum number of seconds to reach room i+1i+1.

对于每个测试用例,输出 nn 个整数,其中第 ii 个整数表示到达房间 i+1i+1 所需的最少秒数。

输入输出样例

  • 输入#1

    5
    1
    0
    0
    1
    2
    0
    1
    114514
    1
    4
    0 9 10 9
    1101
    10
    0 1 4 2 3 14 10 18 7 25
    1110000001

    输出#1

    1 
    3 
    1 
    1 2 5 6 
    1 2 3 4 5 8 11 17 18 19

说明/提示

In the first test case, door 11 opens at the beginning of the 00-th second. You can move to room 22 at the end of the 00-th second, thus reaching room 22 at the 11-st second.

In the second test case, door 11 opens at the beginning of the 22-nd second. You have to wait in room 11 before moving to room 22 at the end of the 22-nd second, thus reaching room 22 at the 33-rd second.

In the third test case, you may pick up the key in room 11 and use it to open door ii in the 00-th second, thus reaching room 22 at the 11-st second.

在第一个测试用例中,门 11 在第 00 秒初开启。你可在第 00 秒末移动至房间 22,从而于第 11 秒到达房间 22。

在第二个测试用例中,门 11 在第 22 秒初开启。你必须先在房间 11 中等待,再于第 22 秒末移至房间 22,从而于第 33 秒到达房间 22。

在第三个测试用例中,你可在第 00 秒于房间 11 拾取钥匙,并立即用其开启门 ii,从而于第 11 秒到达房间 22。

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

首页