CF1418B.Negative Prefixes

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa, consisting of nn integers.

Each position ii (1≤i≤n1 \le i \le n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.

For example, let a=[−1,1,3‾,2,−2‾,1,−4,0‾]a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:

  • [−1,1,3‾,2,−2‾,1,−4,0‾][-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
  • [−4,−1,3‾,2,−2‾,1,1,0‾][-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
  • [1,−1,3‾,2,−2‾,1,−4,0‾][1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
  • [1,2,3‾,−1,−2‾,−4,1,0‾][1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
  • and some others.

Let pp be a sequence of prefix sums of the array aa after the rearrangement. So p1=a1p_1 = a_1, p2=a1+a2p_2 = a_1 + a_2, p3=a1+a2+a3p_3 = a_1 + a_2 + a_3, …\dots, pn=a1+a2+⋯+anp_n = a_1 + a_2 + \dots + a_n.

Let kk be the maximum jj (1≤j≤n1 \le j \le n) such that pj<0p_j \lt 0. If there are no jj such that pj<0p_j \lt 0, then k=0k = 0.

Your goal is to rearrange the values in such a way that kk is minimum possible.

Output the array aa after the rearrangement such that the value kk for it is minimum possible. If there are multiple answers then print any of them.

给你一个包含 nn 个整数的数组 aa。

数组中每个位置 ii(1≤i≤n1 \le i \le n)要么是锁定的,要么是未锁定的。你可以取出所有未锁定位置上的值,以任意顺序重新排列,并将它们放回未锁定的位置中。你不允许删除任何值、添加新值,或对锁定位置上的值进行重排。你也可以选择保持未锁定位置上的值顺序不变。

例如,设 a=[−1,1,3‾,2,−2‾,1,−4,0‾]a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}],其中带下划线的位置是锁定的。你可以得到如下一些数组:

  • [−1,1,3‾,2,−2‾,1,−4,0‾][-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
  • [−4,−1,3‾,2,−2‾,1,1,0‾][-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
  • [1,−1,3‾,2,−2‾,1,−4,0‾][1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
  • [1,2,3‾,−1,−2‾,−4,1,0‾][1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
  • 以及其他若干可能。

令 pp 表示重排后数组 aa 的前缀和序列。即 p1=a1p_1 = a_1,p2=a1+a2p_2 = a_1 + a_2,p3=a1+a2+a3p_3 = a_1 + a_2 + a_3,…\dots,pn=a1+a2+⋯+anp_n = a_1 + a_2 + \dots + a_n。

令 kk 为满足 pj<0p_j \lt 0 的最大下标 jj(1≤j≤n1 \le j \le n)。若不存在这样的 jj,则定义 k=0k = 0。

你的目标是通过重排未锁定位置上的值,使得 kk 尽可能小。

请输出一个重排后的数组 aa,使得对应的 kk 值最小。若有多个满足条件的答案,输出任意一个即可。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of testcases.

Then tt testcases follow.

The first line of each testcase contains a single integer nn (1≤n≤1001 \le n \le 100) — the number of elements in the array aa.

The second line of each testcase contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (−105≤ai≤105-10^5 \le a_i \le 10^5) — the initial array aa.

The third line of each testcase contains nn integers l1,l2,…,lnl_1, l_2, \dots, l_n (0≤li≤10 \le l_i \le 1), where li=0l_i = 0 means that the position ii is unlocked and li=1l_i = 1 means that the position ii is locked.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

接下来是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)—— 数组 aa 中元素的个数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(−105≤ai≤105-10^5 \le a_i \le 10^5)—— 初始数组 aa。

每个测试用例的第三行包含 nn 个整数 l1,l2,…,lnl_1, l_2, \dots, l_n(0≤li≤10 \le l_i \le 1),其中 li=0l_i = 0 表示位置 ii 是未锁定的,li=1l_i = 1 表示位置 ii 是锁定的。

输出格式

Print nn integers — the array aa after the rearrangement. Value kk (the maximum jj such that pj<0p_j \lt 0 (or 00 if there are no such jj)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.

If there are multiple answers then print any of them.

输出 nn 个整数——重排后的数组 aa。令 kk 表示满足 pj<0p_j \lt 0 的最大下标 jj(若不存在这样的 jj,则 k=0k = 0),要求 kk 尽可能小。对于每个被锁定的位置,输出的值必须等于其初始值;对于未被锁定的位置,其值应为所有初始值的一个排列。

若存在多个合法答案,输出任意一个即可。

输入输出样例

  • 输入#1

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

    输出#1

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

说明/提示

In the first testcase you can rearrange all values however you want but any arrangement will result in k=0k = 0. For example, for an arrangement [1,2,3][1, 2, 3], p=[1,3,6]p=[1, 3, 6], so there are no jj such that pj<0p_j \lt 0. Thus, k=0k = 0.

In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.

In the third testcase the prefix sums for the printed array are p=[−8,−14,−13,−9,−5,2,0]p = [-8, -14, -13, -9, -5, 2, 0]. The maximum jj is 55, thus k=5k = 5. There are no arrangements such that k<5k \lt 5.

In the fourth testcase p=[−4,−4,−3,3,6]p = [-4, -4, -3, 3, 6].

In the fifth testcase p=[−1,3,10,2,12,11]p = [-1, 3, 10, 2, 12, 11].

在第一个测试用例中,你可以任意重新排列所有数值,但任何排列都会导致 k=0k = 0。例如,对排列 [1,2,3][1, 2, 3],其前缀和数组为 p=[1,3,6]p=[1, 3, 6],因此不存在满足 pj<0p_j \lt 0 的下标 jj。于是,k=0k = 0。

在第二个测试用例中,不允许重新排列任何元素。因此,输出的数组必须与初始数组完全相同。

在第三个测试用例中,输出数组的前缀和为 p=[−8,−14,−13,−9,−5,2,0]p = [-8, -14, -13, -9, -5, 2, 0]。满足条件的最大下标 jj 为 55,因此 k=5k = 5。不存在能使 k<5k \lt 5 的排列方式。

在第四个测试用例中,p=[−4,−4,−3,3,6]p = [-4, -4, -3, 3, 6]。

在第五个测试用例中,p=[−1,3,10,2,12,11]p = [-1, 3, 10, 2, 12, 11]。

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

首页