A14872 | Reverse Sort Sum
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$ . Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$ .
For example, let $A=[0,1,0,1]$ . Then we have $B_1=[0,1,0,1]$ , $B_2=[0,1,0,1]$ , $B_3=[0,0,1,1]$ , $B_4=[0,0,1,1]$ . Then $C=B_1+B_2+B_3+B_4=[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.
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 $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$ . Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$ .
For example, let $A=[0,1,0,1]$ . Then we have $B_1=[0,1,0,1]$ , $B_2=[0,1,0,1]$ , $B_3=[0,0,1,1]$ , $B_4=[0,0,1,1]$ . Then $C=B_1+B_2+B_3+B_4=[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.
输入格式
The first line contains a single integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases.
Each test case has two lines. The first line contains a single integer $n$ ( $1 \leq n \leq 2 \cdot 10^5$ ).
The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ( $0 \leq c_i \leq 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 \cdot 10^5$ .
Each test case has two lines. The first line contains a single integer $n$ ( $1 \leq n \leq 2 \cdot 10^5$ ).
The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ( $0 \leq c_i \leq 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 \cdot 10^5$ .
输出格式
For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ( $a_i$ is $0$ or $1$ ). If there are multiple answers, you may output any of them.
输入输出样例
输入 #1
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 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1
Here's the explanation for the first test case. Given that $A=[1,1,0,1]$ , we can construct each $B_i$ :
- $B_1=[\color{blue}{1},1,0,1]$ ;
- $B_2=[\color{blue}{1},\color{blue}{1},0,1]$ ;
- $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$ ;
- $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{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]$ .
- $B_1=[\color{blue}{1},1,0,1]$ ;
- $B_2=[\color{blue}{1},\color{blue}{1},0,1]$ ;
- $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$ ;
- $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{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]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted