Example
input
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
output
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
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k=0. For example, for an arrangement [1,2,3], p=[1,3,6], so there are no j such that pj<0. Thus, k=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]. The maximum j is 5, thus k=5. There are no arrangements such that k<5.
In the fourth testcase p=[−4,−4,−3,3,6].
In the fifth testcase p=[−1,3,10,2,12,11].
有两个数组 a , b 其中 b 数组的值为 1 时,所对应的 a 数组的值不可以随便移动
现在可以将 a 数组可以移动的值相互交换使得 k 最小
定义 k :在 a 数组中前 j 个数之和小于 0 , j ϵ [ 1 , n ] , k 为最大的 j 的值
输出使得 k 最小的排列后的数组
讨论
-
当所有数和大于等于 0, 可以满足 k = 0 , 降序排序 即可让 k取最小值0
-
当所有数和小于0 , 那么一定存在一个 pj<0 , 无论怎么排 都会使 k = n(即最后一个最大的 j )为答案不唯一的清况 也降序排就完了 都一样
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[109];
int b[109];
int c[109];
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int cnt = 0;
for(int i = 0; i < n; i++) scanf("%d",&a[i]);
for(int i = 0; i < n; i++) scanf("%d",&b[i]);
for(int i = 0; i < n; i++) {
if(b[i] == 0) c[cnt++] = a[i];
}
sort(c,c+cnt);
cnt--;
for(int i = 0; i < n; i++)
{
if(b[i] == 0) printf("%d",c[cnt--]);
else printf("%d",a[i]);
if(i != n-1) printf(" ");
}
printf("\n");
}
return 0;
}