Reverse Sort Sum

Suppose you had an array A of n elements, each of which is 0 or 1.

Let us define a function f(k,A) which returns another array B, the result of sorting the first k elements of A in non-decreasing order. For example, f(4,[0,1,1,0,0,1,0])=[0,0,1,1,0,1,0]. Note that the first 4 elements were sorted.

Now consider the arrays B1​,B2​,…,Bn​ generated by f(1,A),f(2,A),…,f(n,A). Let C be the array obtained by taking the element-wise sum of B1​,B2​,…,Bn​.

For example, let A=[0,1,0,1]. Then we have B1​=[0,1,0,1],B2​=[0,1,0,1], B3​=[0,0,1,1],B4​=[0,0,1,1]. Then C=B1​+B2​+B3​+B4​=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4].

You are given C. Determine a binary array A that would give C when processed as above. It is guaranteed that an array A exists for given C in the input.

Input

The first line contains a single integer t (1≤t≤1000)  — the number of test cases.

Each test case has two lines. The first line contains a single integer n (1≤n≤2⋅105).

The second line contains n integers c1​,c2​,…,cn​ (0≤ci​≤n). It is guaranteed that a valid array A exists for the given C.

The sum of n over all test cases does not exceed 2⋅105.

Output

For each test case, output a single line containing n integers a1​,a2​,…,an​ (ai​ is 0 or 1). If there are multiple answers, you may output any of them.

Sample 1

InputcopyOutputcopy
5
4
2 4 2 4
7
0 3 4 2 3 2 7
3
0 0 0
4
0 0 0 4
3
1 2 3
1 1 0 1 
0 1 1 0 0 0 1 
0 0 0 
0 0 0 1 
1 0 1 

Note

Here's the explanation for the first test case. Given that A=[1,1,0,1], we can construct each Bi​:

  • B1​=[1,1,0,1];
  • B2​=[1,1,0,1];
  • B3​=[0,1,1,1];
  • B4​=[0,1,1,1]

And then, we can sum up each column above to get C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4].

题意翻译

对于序列 A,我们定义f(k,A) 表示将 A 的前 k 个元素从小到大排序后得到的序列。
现在我们有一个长度为 n 的 01 序列 A,我们将根据其进行下列操作:

  • 对于 1∼n 的每个整数 i,设序列 Bi​=f(i,A)。
  • 然后设长度为 n 的序列 C,满足 C=B1​+B2​+B3​+B4+...+Bn。

给你 n 和 C,你需要构造任意一个长度为 n 的 01 序列 A,满足我们根据 A 经过上述操作可以得到 C。题目保证有解
每个测试点包含 T 组数据。

保证:
1\leq{T}\leq10^3
1\leq\sum_{i=1}^{T}n_{i}\leq2\times10^{5};0≤Ci​≤n;

题目链接:

点击这里跳转题目链接:Reverse Sort Sum

思路解析:

先将数组全部赋值为'1',
然后把该是'0'的位置改成'0'即可,
这是一道很简单的题,
不难推出:
如果(c[i]==0||str[i]=='0')时l=c[i]+i,
否则l=c[i]+1,
然后str[l]='0',l++;
C语言代码如下
if(c[i]*(str[i]^48)) l=c[i]+1;
else l=c[i]+i;
str[l++]='0';
或者
if(c[i]==0||str[i]=='0') l=c[i]+i;
else l=c[i]+1;
str[l++]='0';
详细代码请看下面的完整正确代码

正确代码

#include<stdio.h>
char str[555555];
void Solve() {
	int n,e;
	scanf("%d",&n);
	for(int j=1; j<=n; j++) str[j]='1';
	for(int i=1,l=1; i<=n; i++) {
		scanf("%d",&e);
		if(e*(str[i]^48)) l=e+1;
		else l=e+i;
		str[l++]='0';
		printf("%c%s",str[i],i==n?"\n":" ");
	}
}
signed main() {
	int T;
	scanf("%d",&T);
	while(T--) Solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

❃黑洞᭄ꦿ࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值