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
Inputcopy | Outputcopy |
---|---|
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 组数据。
保证:
;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;
}