inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
import sys
import math
# sys.stdin = open("input.txt")
for _ in range(int(input())):
n=int(input())
a=[int(o) for o in input().split()]
print(*a[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for i in range(int(input())):
n = int(input())
p = list(map(int, input().split()))
p.reverse()
print(" ".join(str(num) for num in p))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
"""
Author: Q.E.D
Time: 2020-09-06 09:35:20
"""
T = int(input())
for _ in range(T):
n = int(input())
a = list(map(int, input().split()))
print(' '.join(map(str, a[::-1])))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
arr = arr[-1::-1]
print(*arr)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for t in range(int(input())):
n=int(input())
a = list(map(str, input().split()))
print(" ".join(a[::-1]))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n = int(input())
p = [*list(map(int, input().split()))][::-1]
print(*p)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for _ in range(int(input())):
n = int(input())
p = list(map(int, input().split()))
p.reverse()
print(*p)
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
nList = list(map(int, input().split()))
print(*nList[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for T in range(int(input())) :
n = int(input())
l = input().split()
print(*l[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
import sys
letters = ascii_letters
input = stdin.readline
#print = stdout.write
for _ in range(int(input())):
n = int(input())
print(*(list(map(int, input().split()))[::-1]))
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
def main():
n = int(input())
p = list(map(int,input().split()))
p = list(reversed(p))
for i in p:
print(i, end=" ")
print()
def __starting_point():
t = int(input())
for i in range(t):
main()
"""
60, 61
"""
"""
"""
__starting_point()
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
for i in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
print(*a[::-1])
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
def solve():
N = int(input())
A = [int(s) for s in input().split()]
A = A[::-1]
print(*A)
return
T = int(input())
for t in range(T):
solve()
|
A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Let $p$ be any permutation of length $n$. We define the fingerprint $F(p)$ of $p$ as the sorted array of sums of adjacent elements in $p$. More formally,
$$F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).$$
For example, if $n=4$ and $p=[1,4,2,3],$ then the fingerprint is given by $F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]$.
You are given a permutation $p$ of length $n$. Your task is to find a different permutation $p'$ with the same fingerprint. Two permutations $p$ and $p'$ are considered different if there is some index $i$ such that $p_i \ne p'_i$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 668$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 100$) — the length of the permutation.
The second line of each test case contains $n$ integers $p_1,\ldots,p_n$ ($1\le p_i\le n$). It is guaranteed that $p$ is a permutation.
-----Output-----
For each test case, output $n$ integers $p'_1,\ldots, p'_n$ — a permutation such that $p'\ne p$ and $F(p')=F(p)$.
We can prove that for every permutation satisfying the input constraints, a solution exists.
If there are multiple solutions, you may output any.
-----Example-----
Input
3
2
1 2
6
2 1 6 5 4 3
5
2 4 3 1 5
Output
2 1
1 2 5 6 3 4
3 1 5 2 4
-----Note-----
In the first test case, $F(p)=\mathrm{sort}([1+2])=[3]$.
And $F(p')=\mathrm{sort}([2+1])=[3]$.
In the second test case, $F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
And $F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]$.
In the third test case, $F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]$.
And $F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]$.
|
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left, bisect_right
from functools import lru_cache
MOD = int(1e9)+7
INF = float('inf')
def solve():
n = int(input())
p = [int(x) for x in input().split()]
p.reverse()
print(*p)
t = 1
t = int(input())
for case in range(1,t+1):
ans = solve()
"""
"""
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
def one():
return int(input())
def two():
return list(map(int, input().split()))
def lis():
return list(map(int, input().split()))
def st():
return input()
for _ in range(one()):
x, y, a, b = list(map(int, input().split()))
d = y - x
if d%(a+b)==0:
print(d//(a+b))
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
t = int(input())
for _ in range(t):
x,y,a,b = list(map(int,input().split()))
k = y - x
if k % (a + b) == 0:
print(k // (a+b))
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
from math import *
t = int(input())
for i in range(t):
x, y, a, b = list(map(int, input().split()))
if (y - x) % (a + b) == 0:
print((y - x) // (a + b))
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
for __ in range(int(input())):
x, y, a, b = map(int, input().split())
z = y - x
c = a + b
print(-1 if z % c else z // c)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
for _ in range(int(input())):
x, y, a, b = list(map(int, input().split()))
t = (y - x) / (a + b)
tz = (y - x) // (a + b)
if t != tz:
print(-1)
else:
print(tz)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
# ========= /\ /| |====/|
# | / \ | | / |
# | /____\ | | / |
# | / \ | | / |
# ========= / \ ===== |/====|
# code
def main():
t = int(input())
for _ in range(t):
x,y,a,b = map(int,input().split())
i = (y - x)//(a + b)
if x + a * i == y - b * i:
print(i)
else:
print(-1)
return
def __starting_point():
main()
__starting_point()
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
from math import *
import itertools
zzz = int(input())
for zz in range(zzz):
x, y, a, b = list(map(int, input().split()))
s = y - x
t = s / (a + b)
if int(t) == t:
print(int(t))
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
from math import *
t = int(input())
for y in range(t):
x,y,a,b = list(map(int,input().split()))
if (y-x)%(a+b) != 0: print(-1)
else:
print((y-x)//(a+b))
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for x, y, a, b in Query:
d = y-x
r = a+b
if d%r == 0:
print(d//r)
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
t = int(input())
for _ in range(t):
x,y,a,b = [int(i) for i in input().split()]
s = (y-x)/(a+b)
if s.is_integer():
print(int(s))
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
t=int(input())
for _ in range(t):
x,y,a,b=list(map(int,input().split()))
if (y-x)%(a+b)!=0:
print(-1)
else:
print(int((y-x)/(a+b)))
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
for _ in range(int(input())):
x, y, a, b = map(int, input().split())
d = y - x
if d % (a + b ) == 0:
print(d // (a + b))
else:
print(-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
for nt in range(int(input())):
x,y,a,b=map(int,input().split())
if (y-x)%(a+b)==0:
print ((y-x)//(a+b))
else:
print (-1)
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
def solve(X, Y, A, B):
d, r = divmod(Y - X, A + B)
if r == 0:
return d
else:
return -1
def __starting_point():
T, = list(map(int, input().split()))
for t in range(T):
X, Y, A, B = list(map(int, input().split()))
ans = solve(X, Y, A, B)
print(ans)
__starting_point()
|
Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.
He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $x$, and the shorter rabbit is currently on position $y$ ($x \lt y$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $a$, and the shorter rabbit hops to the negative direction by $b$.
[Image]
For example, let's say $x=0$, $y=10$, $a=2$, and $b=3$. At the $1$-st second, each rabbit will be at position $2$ and $7$. At the $2$-nd second, both rabbits will be at position $4$.
Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.
-----Input-----
Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$).
Each test case contains exactly one line. The line consists of four integers $x$, $y$, $a$, $b$ ($0 \le x \lt y \le 10^9$, $1 \le a,b \le 10^9$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.
-----Output-----
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.
If the two rabbits will never be at the same position simultaneously, print $-1$.
-----Example-----
Input
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
Output
2
-1
10
-1
1
-----Note-----
The first case is explained in the description.
In the second case, each rabbit will be at position $3$ and $7$ respectively at the $1$-st second. But in the $2$-nd second they will be at $6$ and $4$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.
|
import sys
import math
def input():
return sys.stdin.readline().strip()
def iinput():
return [int(x) for x in sys.stdin.readline().split()]
def main():
x, y, a, b = iinput()
if (y - x) % (a + b) == 0:
print((y - x) // (a + b))
else:
print(-1)
return
for ______ in range(int(input())):
main()
|
Let's look at the following process: initially you have an empty stack and an array $s$ of the length $l$. You are trying to push array elements to the stack in the order $s_1, s_2, s_3, \dots s_{l}$. Moreover, if the stack is empty or the element at the top of this stack is not equal to the current element, then you just push the current element to the top of the stack. Otherwise, you don't push the current element to the stack and, moreover, pop the top element of the stack.
If after this process the stack remains empty, the array $s$ is considered stack exterminable.
There are samples of stack exterminable arrays: $[1, 1]$; $[2, 1, 1, 2]$; $[1, 1, 2, 2]$; $[1, 3, 3, 1, 2, 2]$; $[3, 1, 3, 3, 1, 3]$; $[3, 3, 3, 3, 3, 3]$; $[5, 1, 2, 2, 1, 4, 4, 5]$;
Let's consider the changing of stack more details if $s = [5, 1, 2, 2, 1, 4, 4, 5]$ (the top of stack is highlighted). after pushing $s_1 = 5$ the stack turn into $[\textbf{5}]$; after pushing $s_2 = 1$ the stack turn into $[5, \textbf{1}]$; after pushing $s_3 = 2$ the stack turn into $[5, 1, \textbf{2}]$; after pushing $s_4 = 2$ the stack turn into $[5, \textbf{1}]$; after pushing $s_5 = 1$ the stack turn into $[\textbf{5}]$; after pushing $s_6 = 4$ the stack turn into $[5, \textbf{4}]$; after pushing $s_7 = 4$ the stack turn into $[\textbf{5}]$; after pushing $s_8 = 5$ the stack is empty.
You are given an array $a_1, a_2, \ldots, a_n$. You have to calculate the number of its subarrays which are stack exterminable.
Note, that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.
The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) — the length of array $a$.
The second line of each query contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements.
It is guaranteed that the sum of all $n$ over all queries does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print one integer in single line — the number of stack exterminable subarrays of the array $a$.
-----Example-----
Input
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3
Output
4
1
8
-----Note-----
In the first query there are four stack exterminable subarrays: $a_{1 \ldots 4} = [2, 1, 1, 2], a_{2 \ldots 3} = [1, 1], a_{2 \ldots 5} = [1, 1, 2, 2], a_{4 \ldots 5} = [2, 2]$.
In the second query, only one subarray is exterminable subarray — $a_{3 \ldots 4}$.
In the third query, there are eight stack exterminable subarrays: $a_{1 \ldots 8}, a_{2 \ldots 5}, a_{2 \ldots 7}, a_{2 \ldots 9}, a_{3 \ldots 4}, a_{6 \ldots 7}, a_{6 \ldots 9}, a_{8 \ldots 9}$.
|
# encoding: utf-8
from sys import stdin
def solve(a):
# root node of tries denotes empty stack
stack = [None]
node_stack = [[1, {}]]
trie = node_stack[-1]
counter = 0
for i in range(len(a)):
el = a[i]
if len(stack) == 0 or stack[-1] != el:
current_node = node_stack[-1]
stack.append(el)
if el not in current_node[1]:
current_node[1][el] = [0, {}]
next_node = current_node[1][el]
next_node[0] += 1
node_stack.append(next_node)
else:
# just go up in trie
stack.pop()
node_stack.pop()
node_stack[-1][0] += 1
value = node_stack[-1][0]
counter -= (((value - 1) * (value - 2)) // 2)
counter += (((value) * (value - 1)) // 2)
return counter
q = int(stdin.readline().strip())
for _ in range(q):
n = int(stdin.readline().strip())
a = [int(i) for i in stdin.readline().strip().split()]
print(solve(a))
|
Let's look at the following process: initially you have an empty stack and an array $s$ of the length $l$. You are trying to push array elements to the stack in the order $s_1, s_2, s_3, \dots s_{l}$. Moreover, if the stack is empty or the element at the top of this stack is not equal to the current element, then you just push the current element to the top of the stack. Otherwise, you don't push the current element to the stack and, moreover, pop the top element of the stack.
If after this process the stack remains empty, the array $s$ is considered stack exterminable.
There are samples of stack exterminable arrays: $[1, 1]$; $[2, 1, 1, 2]$; $[1, 1, 2, 2]$; $[1, 3, 3, 1, 2, 2]$; $[3, 1, 3, 3, 1, 3]$; $[3, 3, 3, 3, 3, 3]$; $[5, 1, 2, 2, 1, 4, 4, 5]$;
Let's consider the changing of stack more details if $s = [5, 1, 2, 2, 1, 4, 4, 5]$ (the top of stack is highlighted). after pushing $s_1 = 5$ the stack turn into $[\textbf{5}]$; after pushing $s_2 = 1$ the stack turn into $[5, \textbf{1}]$; after pushing $s_3 = 2$ the stack turn into $[5, 1, \textbf{2}]$; after pushing $s_4 = 2$ the stack turn into $[5, \textbf{1}]$; after pushing $s_5 = 1$ the stack turn into $[\textbf{5}]$; after pushing $s_6 = 4$ the stack turn into $[5, \textbf{4}]$; after pushing $s_7 = 4$ the stack turn into $[\textbf{5}]$; after pushing $s_8 = 5$ the stack is empty.
You are given an array $a_1, a_2, \ldots, a_n$. You have to calculate the number of its subarrays which are stack exterminable.
Note, that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.
The first line of each query contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$) — the length of array $a$.
The second line of each query contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements.
It is guaranteed that the sum of all $n$ over all queries does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case print one integer in single line — the number of stack exterminable subarrays of the array $a$.
-----Example-----
Input
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3
Output
4
1
8
-----Note-----
In the first query there are four stack exterminable subarrays: $a_{1 \ldots 4} = [2, 1, 1, 2], a_{2 \ldots 3} = [1, 1], a_{2 \ldots 5} = [1, 1, 2, 2], a_{4 \ldots 5} = [2, 2]$.
In the second query, only one subarray is exterminable subarray — $a_{3 \ldots 4}$.
In the third query, there are eight stack exterminable subarrays: $a_{1 \ldots 8}, a_{2 \ldots 5}, a_{2 \ldots 7}, a_{2 \ldots 9}, a_{3 \ldots 4}, a_{6 \ldots 7}, a_{6 \ldots 9}, a_{8 \ldots 9}$.
|
# encoding: utf-8
from sys import stdin
def solve(a):
# root node of tries denotes empty stack
stack = [None]
node_stack = [[1, {}]]
counter = 0
for i in range(len(a)):
el = a[i]
if len(stack) == 0 or stack[-1] != el:
current_node = node_stack[-1]
stack.append(el)
if el not in current_node[1]:
current_node[1][el] = [0, {}]
next_node = current_node[1][el]
next_node[0] += 1
node_stack.append(next_node)
else:
# just go up in trie
stack.pop()
node_stack.pop()
node_stack[-1][0] += 1
value = node_stack[-1][0]
counter += (value - 1)
return counter
q = int(stdin.readline().strip())
for _ in range(q):
n = int(stdin.readline().strip())
a = [int(i) for i in stdin.readline().strip().split()]
print(solve(a))
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
import sys
input=sys.stdin.readline
for _ in range(int(input())):
s=input().rstrip()
x=int(input())
n=len(s)
ans=["1" for i in range(n)]
for i in range(n):
if s[i]=="0":
if i-x>=0:
ans[i-x]="0"
if i+x<n:
ans[i+x]="0"
for i in range(n):
if s[i]=="1":
check=False
if i-x>=0:
check|=(ans[i-x]=="1")
if i+x<n:
check|=(ans[i+x]=="1")
if not check:
print(-1)
break
else:
print("".join(ans))
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
s = input().strip()
n = len(s)
x = int(input())
outl = ['1'] * n
for i in range(n):
if s[i] == '0':
if i >= x:
outl[i-x] = '0'
if i + x < n:
outl[i+x] = '0'
out = ''.join(outl)
works = True
for i in range(n):
if s[i] == '1':
if (i < x or out[i - x] == '0') and (i + x >= n or out[i + x] == '0'):
works = False
break
if works:
print(out)
else:
print(-1)
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
t = int(input())
for _ in range(t):
s = input()
n = len(s)
x = int(input())
w = [0]*n
for i in range(n):
if 0 <= i + x < n and 0 <= i - x < n:
if s[i+x] == "1" and s[i-x] == "1":
w[i] = 1
elif 0 <= i + x < n:
if s[i+x] == "1":
w[i] = 1
elif 0 <= i - x < n:
if s[i-x] == "1":
w[i] = 1
for i in range(n):
if 0 <= i-x < n and w[i-x] == 1:
if s[i] == "1":
continue
else:
print(-1)
break
if 0 <= i + x < n and w[i+x] == 1:
if s[i] == "1":
continue
else:
print(-1)
break
if s[i] == "1":
print(-1)
break
else:
print(*w, sep="")
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
def process():
li=list(input())
x=int(input())
n=len(li)
ans=['1' for i in range(n)]
for i in range(0,n):
if(li[i]=='0'):
if(i-x>=0):
ans[i-x]='0'
if(i+x<n):
ans[i+x]='0'
for i in range(0,n):
chr='0'
if(i-x>=0 and ans[i-x]=='1'):
chr='1'
if(i+x<n and ans[i+x]=='1'):
chr='1'
if(li[i]==chr):
pass
else:
print(-1)
return
print("".join(ans))
tests=int(input())
for i in range(tests):
process()
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
import math
import collections
from sys import stdin,stdout,setrecursionlimit
import bisect as bs
setrecursionlimit(2**20)
M = 10**9+7
T = int(stdin.readline())
# T = 1
for _ in range(T):
# n = int(stdin.readline())
# n,d,m = list(map(int,stdin.readline().split()))
# a = list(map(int,stdin.readline().split()))
# q = int(stdin.readline())
# a = list(map(int,stdin.readline().split()))
# b = list(map(int,stdin.readline().split()))
s = stdin.readline().strip('\n')
x = int(stdin.readline())
n = len(s)
a = [-1]*n
res = True
for i in range(n):
if(s[i] == '1'):
if((i-x) >= 0 and a[i-x] != 0):
a[i-x] = 1
continue
if((i+x) < n):
if(a[i+x] == 0):
res = False
break
else:
a[i+x] = 1
continue
res = False
break
else:
if((i-x) >= 0):
if(a[i-x] == 1):
res = False
break
else:
a[i-x] = 0
if((i+x) < n):
if(a[i+x] == 1):
res = False
break
else:
a[i+x] = 0
ans = ''
for i in range(n):
if(a[i] != -1):
ans = ans + str(a[i])
else:
ans = ans + '1'
if(res): print(ans)
else: print(-1)
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
l = [0] + li3()
x = val()
n = len(l) - 1
ans = [None] * (n + 1)
flag = 0
# print(l, n, x)
for i in range(1, n + 1):
# print(ans, i)
if l[i] == 1:
if i > x and (ans[i - x] == 1 or ans[i - x] == None):
ans[i - x] = 1
elif i + x <= n:
ans[i + x] = 1
else:
flag = 1
break
else:
if (i <= x or (ans[i - x] == None or ans[i - x ] == 0)) and (i + x > n or (ans[i + x] == None)):
if i > x:ans[i - x] = 0
if i + x <= n:ans[i + x] = 0
else:
flag = 1
break
for i in range(1, n + 1):
if ans[i] == None:ans[i] = 1
if flag:
print(-1)
else:
print(*ans[1:n + 1], sep = '')
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
for t in range(int(input())):
s = [int(c == "1") for c in input()]
x = int(input())
n = len(s)
sat = lambda i: (s[i] if i in range(n) else 1)
w = [(sat(i - x) & sat(i + x)) for i in range(n)]
wat = lambda i: (w[i] if i in range(n) else 0)
s_ref = [(wat(i - x) | wat(i + x)) for i in range(n)]
if s != s_ref:
print(-1)
else:
print("".join(map(str, w)))
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
def solve():
s = list(map(int, input()))
n = len(s)
x = int(input())
w = [0] * n
d = [False] * n
for i in range(n):
if s[i] == 0:
if i - x >= 0:
w[i - x] = 0
d[i - x] = True
if i + x < n:
w[i + x] = 0
d[i + x] = True
for i in range(n):
if not d[i]:
w[i] = 1
d[i] = True
t = [0] * n
for i in range(n):
if i - x >= 0 and w[i - x] == 1:
t[i] = 1
if i + x < n and w[i + x] == 1:
t[i] = 1
if s != t:
print('-1')
return
print(''.join(map(str, w)))
t = int(input())
for _ in range(t):
solve()
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
t = int(input())
for i in range(t):
s = input()
m = len(s)
x = int(input())
ANS = [1] * m
for i in range(m):
if s[i] == "0":
if i-x >= 0:
ANS[i-x] = 0
if i+x < m:
ANS[i+x] = 0
ng = 0
for i in range(m):
one = 0
if (i-x >= 0 and ANS[i-x] == 1) or (i+x < m and ANS[i+x] == 1):
one = 1
if (one == 1 and s[i] == "0") or (one == 0 and s[i] == "1"):
ng = 1
break
if ng == 1:
print(-1)
else:
print("".join([str(i) for i in ANS]))
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
for _ in range(int(input())):
s = input()
n = len(s)
x = int(input())
w = ['1'] * len(s)
for i in range(n):
if s[i] == '0':
if i + x < n:
w[i + x] = '0'
if i - x >= 0:
w[i - x] = '0'
temp = ['0'] * len(s)
for i in range(n):
if i + x < n:
if w[i + x] == '1':
temp[i] = '1'
if i - x >= 0:
if w[i - x] == '1':
temp[i] = '1'
if ''.join(temp) == s:
print(''.join(w))
else:
print(-1)
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
t=int(input())
for you in range(t):
s=input()
n=len(s)
x=int(input())
arr=[1 for i in range(n)]
poss=1
for i in range(n):
if(s[i]=='1'):
if(i-x>=0 and arr[i-x]==0 and i+x<n and arr[i+x]==0):
poss=0
break
else:
if(i-x>=0):
arr[i-x]=0
if(i+x<n):
arr[i+x]=0
if(poss==0):
print(-1)
continue
isposs=""
for i in range(n):
if(i-x>=0 and arr[i-x]):
isposs=isposs+'1'
elif(i+x<n and arr[i+x]):
isposs=isposs+'1'
else:
isposs=isposs+'0'
if(isposs==s):
for i in arr:
print(i,end="")
print()
else:
print(-1)
|
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
|
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
for _ in range(II()):
s=SI()
x=II()
n=len(s)
ans=[1]*n
for i,c in enumerate(s):
if c=="0":
if i-x>=0:ans[i-x]=0
if i+x<n:ans[i+x]=0
def ok():
for i in range(n):
if s[i]=="1":
flag=0
if i-x>=0:flag|=ans[i-x]
if i+x<n:flag|=ans[i+x]
if flag==0:return False
return True
if ok():print(*ans,sep="")
else:print(-1)
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
def solve():
n = int(input())
lst = list(map(int,input().split()))
if sum(lst) <= n // 2:
print(n//2)
print("0 " * (n // 2))
else:
print(n//2 + (n // 2) % 2)
print("1 " * (n//2 + (n // 2) % 2))
for i in range(int(input())):
solve()
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int,input().split()))
o = 0
z = 0
for i in range(n):
if(l[i]): o+=1
else: z+=1
if(o>z):
o-=o%2
print(o)
for i in range(o):
print(1, end =" ")
else:
print(z)
for i in range(z):
print(0, end=" ")
print()
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
#OM GANESHAY NAMH
#GANPATI BAPPA MORYA
import math,queue,heapq
import sys
sys.setrecursionlimit(10**6)
fastinput=sys.stdin.readline
fastout=sys.stdout.write
t=int(fastinput())
while t:
t-=1
n=int(fastinput())
a=list(map(int,fastinput().split()))
c=a.count(1)
if c==0 or c==n:
print(n)
print(*a)
elif c<=n//2:
print(n-c)
print('0 '*(n-c))
else:
if c%2==0:
print(c)
print('1 '*c)
else:
c-=1
print(c)
print('1 '*c)
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
res = []
for i in range(n // 2):
if a[i * 2] == 1 and a[i * 2 + 1] == 1:
res.append(1)
res.append(1)
elif a[i * 2] == 0 and a[i * 2 + 1] == 0:
res.append(0)
res.append(0)
else:
res.append(0)
print(len(res))
print(*res)
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n=int(input())
A=list(map(int,input().split()))
ZERO=A.count(0)
if ZERO>=n//2:
print(ZERO)
print(*[0]*ZERO)
else:
ONE=n-ZERO
print(ONE//2*2)
print(*[1]*(ONE//2*2))
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
a = arr.count(0)
b = arr.count(1)
if b > n // 2:
print(b - b % 2)
print(*[1 for _ in range(b - b % 2)])
elif b == n // 2:
print(a)
print(*[0 for _ in range(a)])
else:
print(a - a % 2)
print(*[0 for _ in range(a - a % 2)])
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
for i in ' '*(int(input())):
n=int(input())
L=list(map(int,input().split()))
s=sum(L)
if s*2>n:
if (n//2)%2:
print(n//2+1)
for i in ' '*(n//2+1):print(1,end=' ')
else:
print(n//2)
for i in ' '*(n//2):print(1,end=' ')
else:
print(n//2)
for i in ' '*(n//2):print(0,end=' ')
print()
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
from math import log2, ceil
def readGenerator():
while True:
tokens = input().split(' ')
for t in tokens:
yield t
reader = readGenerator()
def readWord():
return next(reader)
def readInt():
return int(next(reader))
def readFloat():
return float(next(reader))
def readLine():
return input()
def solve(a):
v0, v1 = 0, 0
for i in a:
if i == 0:
v0 += 1
else:
v1 += 1
if v1 > v0:
if v1 % 2 != 0:
v1 -= 1
print(v1)
return '1 ' * v1
print(v0)
return '0 ' * v0
tests = readInt()
for i in range(tests):
n = readInt()
a = [readInt() for _ in range(n)]
print(solve(a))
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
def solve():
n = int(input())
a = list(map(int, input().split()))
if a.count(0) >= n // 2:
print(n // 2)
print(' '.join('0' * (n // 2)))
else:
m = n // 2
if m % 2 == 1:
m += 1
print(m)
print(' '.join('1' * (m)))
t = int(input())
for _ in range(t):
solve()
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
t = int(input())
for _ in range(t):
n = int(input())
arr = [int(j) for j in input().split()]
o = arr.count(1)
z = arr.count(0)
if o > z:
if (n-z)%2 == 1:
print(n-z-1)
print(*([1]*(n-z-1)))
else:
print(n-z)
print(*([1]*(n-z)))
else:
print(n-o)
print(*([0]*(n-o)))
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
for _ in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
one = l.count(1)
zero = n-one
if zero>=n//2:
print(zero)
print(*[0]*zero)
else:
one -= one%2
print(one)
print(*[1]*one)
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
z = 0
o = 0
ans = []
for i in a:
if i == 0:
z += 1
else:
o += 1
if z == 2:
z = 0
o = 0
ans.append(0)
ans.append(0)
elif o == 2:
z = 0
o = 0
ans.append(1)
ans.append(1)
if z > 0:
ans.append(0)
print(len(ans))
print(*ans)
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
if a.count(0)>=n//2:
print(n//2)
print(*[0]*(n//2))
else:
if (n//2)%2==0:
print(n//2)
print(*[1]*(n//2))
else:
print(n//2+1)
print(*[1]*(n//2+1))
|
Alexandra has an even-length array $a$, consisting of $0$s and $1$s. The elements of the array are enumerated from $1$ to $n$. She wants to remove at most $\frac{n}{2}$ elements (where $n$ — length of array) in the way that alternating sum of the array will be equal $0$ (i.e. $a_1 - a_2 + a_3 - a_4 + \dotsc = 0$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.
For example, if she has $a = [1, 0, 1, 0, 0, 0]$ and she removes $2$nd and $4$th elements, $a$ will become equal $[1, 1, 0, 0]$ and its alternating sum is $1 - 1 + 0 - 0 = 0$.
Help her!
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 10^3$, $n$ is even) — length of the array.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^3$.
-----Output-----
For each test case, firstly, print $k$ ($\frac{n}{2} \leq k \leq n$) — number of elements that will remain after removing in the order they appear in $a$. Then, print this $k$ numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
-----Example-----
Input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
Output
1
0
1
0
2
1 1
4
1 1 0 0
-----Note-----
In the first and second cases, alternating sum of the array, obviously, equals $0$.
In the third case, alternating sum of the array equals $1 - 1 = 0$.
In the fourth case, alternating sum already equals $1 - 1 + 0 - 0 = 0$, so we don't have to remove anything.
|
import sys
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
#take input
n = int(input())
arr = input_split()
s = sum(arr)
if s <= n//2:
ans = [0]*(n//2)
else:
#num of ones is greater than equal to n//2
if n%4 == 0:
ans = [1]*(n//2)
else:
ans = [1]*((n//2) + 1)
# ans = [1]*()
answers.append(ans)
for ans in answers:
print(len(ans))
print(*ans, sep = ' ')
|
Berland year consists of $m$ months with $d$ days each. Months are numbered from $1$ to $m$. Berland week consists of $w$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $w$ days.
A pair $(x, y)$ such that $x < y$ is ambiguous if day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
Count the number of ambiguous pairs.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 10^9$) — the number of months in a year, the number of days in a month and the number of days in a week.
-----Output-----
Print $t$ integers — for each testcase output the number of pairs $(x, y)$ such that $x < y$ and day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
-----Example-----
Input
5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324
Output
6
9
5
0
116461800
-----Note-----
Here are the pairs for the first test case: $$
|
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')
def gcd(a, b):
while b:
a, b = b, a%b
return a
def solve():
m, d, w = nm()
g = w // gcd(d-1, w)
c = min(m, d)
v = c // g
ans = v * (v - 1) // 2 * g
ans += (c - g * v) * v
print(ans)
return
# solve()
T = ni()
for _ in range(T):
solve()
|
Berland year consists of $m$ months with $d$ days each. Months are numbered from $1$ to $m$. Berland week consists of $w$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $w$ days.
A pair $(x, y)$ such that $x < y$ is ambiguous if day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
Count the number of ambiguous pairs.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 10^9$) — the number of months in a year, the number of days in a month and the number of days in a week.
-----Output-----
Print $t$ integers — for each testcase output the number of pairs $(x, y)$ such that $x < y$ and day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
-----Example-----
Input
5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324
Output
6
9
5
0
116461800
-----Note-----
Here are the pairs for the first test case: $$
|
from math import gcd
for _ in range(int(input())):
m,d,w=list(map(int,input().split()))
M=min(m,d)
W=w//gcd(w,d-1)
Q=M//W
R=M%W
ans=(W-R)*(Q*(Q-1)//2)+R*(Q*(Q+1)//2)
print(ans)
|
Berland year consists of $m$ months with $d$ days each. Months are numbered from $1$ to $m$. Berland week consists of $w$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $w$ days.
A pair $(x, y)$ such that $x < y$ is ambiguous if day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
Count the number of ambiguous pairs.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 10^9$) — the number of months in a year, the number of days in a month and the number of days in a week.
-----Output-----
Print $t$ integers — for each testcase output the number of pairs $(x, y)$ such that $x < y$ and day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
-----Example-----
Input
5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324
Output
6
9
5
0
116461800
-----Note-----
Here are the pairs for the first test case: $$
|
import sys
import math
T = int(sys.stdin.readline().strip())
for t in range (0, T):
m, d, w = list(map(int, sys.stdin.readline().strip().split()))
w = w // math.gcd(w, d-1)
d = min(d, m)
m = d
ans = -d
ans = ans + (m // w) * d
m = m - (m // w) * w
ans = ans + (d // w) * m
d = d - (d // w) * w
ans = ans + d
print(ans//2)
|
Berland year consists of $m$ months with $d$ days each. Months are numbered from $1$ to $m$. Berland week consists of $w$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $w$ days.
A pair $(x, y)$ such that $x < y$ is ambiguous if day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
Count the number of ambiguous pairs.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 10^9$) — the number of months in a year, the number of days in a month and the number of days in a week.
-----Output-----
Print $t$ integers — for each testcase output the number of pairs $(x, y)$ such that $x < y$ and day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
-----Example-----
Input
5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324
Output
6
9
5
0
116461800
-----Note-----
Here are the pairs for the first test case: $$
|
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict
# available on Google, not available on Codeforces
# import numpy as np
# import scipy
def solve(m,d,w): # fix inputs here
console("----- solving ------")
a = int(w/math.gcd(w,1-d))
b = min(m,d)
console(a,b)
return ((1 + b//a)*(b + b%a))//2 - b
def console(*args): # the judge will not read these print statement
print('\033[36m', *args, '\033[0m', file=sys.stderr)
return
# fast read all
# sys.stdin.readlines()
for case_num in range(int(input())):
# read line as a string
# strr = input()
# read line as an integer
# k = int(input())
# read one line and parse each word as a string
# lst = input().split()
# read one line and parse each word as an integer
m,d,w = list(map(int,input().split()))
# read matrix and parse as integers (after reading read nrows)
# lst = list(map(int,input().split()))
# nrows = lst[0] # index containing information, please change
# grid = []
# for _ in range(nrows):
# grid.append(list(map(int,input().split())))
res = solve(m,d,w) # please change
# Google - case number required
# print("Case #{}: {}".format(case_num+1, res))
# Codeforces - no case number required
print(res)
|
Berland year consists of $m$ months with $d$ days each. Months are numbered from $1$ to $m$. Berland week consists of $w$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $w$ days.
A pair $(x, y)$ such that $x < y$ is ambiguous if day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
Count the number of ambiguous pairs.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 10^9$) — the number of months in a year, the number of days in a month and the number of days in a week.
-----Output-----
Print $t$ integers — for each testcase output the number of pairs $(x, y)$ such that $x < y$ and day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
-----Example-----
Input
5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324
Output
6
9
5
0
116461800
-----Note-----
Here are the pairs for the first test case: $$
|
from math import gcd
def sum_of_floor(n, p, q):
t = gcd(p, q)
p, q = p // t, q // t
s = 0
z = 1
while q > 0 and n > 0:
t = p // q
s = s + z * t * n * (n + 1) // 2
p = p - q * t
t = n // q
s = s + z * p * t * (n + 1) - z * t * (p * q * t + p + q - 1) // 2
n = n - q * t
t = (n * p) // q
s = s + z * t * n
n = t
p, q = q, p
z = -z
return s
def solve():
m, d, w = map(int, input().split())
if d == 1:
print(0)
return
u = gcd(d - 1, w)
res = sum_of_floor(min(m, d) - 1, 1, w // u)
print(res)
t = int(input())
for _ in range(t):
solve()
|
Berland year consists of $m$ months with $d$ days each. Months are numbered from $1$ to $m$. Berland week consists of $w$ days. The first day of the year is also the first day of the week. Note that the last week of the year might be shorter than $w$ days.
A pair $(x, y)$ such that $x < y$ is ambiguous if day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
Count the number of ambiguous pairs.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Each of the next $t$ lines contains three integers $m$, $d$ and $w$ ($1 \le m, d, w \le 10^9$) — the number of months in a year, the number of days in a month and the number of days in a week.
-----Output-----
Print $t$ integers — for each testcase output the number of pairs $(x, y)$ such that $x < y$ and day $x$ of month $y$ is the same day of the week as day $y$ of month $x$.
-----Example-----
Input
5
6 7 4
10 7 12
12 30 7
1 1 1
3247834 10298779 625324
Output
6
9
5
0
116461800
-----Note-----
Here are the pairs for the first test case: $$
|
from math import gcd
for _ in range(int(input())):
m, d, w = list(map(int, input().split()))
if d == 1:
print(0)
else:
w1 = w // gcd(w, d - 1)
md = min(m, d)
# sum(max(md - w1 * n, 0) for n in 1..inf)
# sum(md - w1 * n for n in 1..md // w1)
mdd_w1 = md // w1
print((md * 2 - w1 - mdd_w1 * w1) * mdd_w1 // 2)
|
Bob is playing with $6$-sided dice. A net of such standard cube is shown below.
[Image]
He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.
For example, the number of visible pips on the tower below is $29$ — the number visible on the top is $1$, from the south $5$ and $3$, from the west $4$ and $2$, from the north $2$ and $4$ and from the east $3$ and $5$.
[Image]
The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.
Bob also has $t$ favourite integers $x_i$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $x_i$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of favourite integers of Bob.
The second line contains $t$ space-separated integers $x_i$ ($1 \leq x_i \leq 10^{18}$) — Bob's favourite integers.
-----Output-----
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
-----Example-----
Input
4
29 34 19 38
Output
YES
YES
YES
NO
-----Note-----
The first example is mentioned in the problem statement.
In the second example, one can build the tower by flipping the top dice from the previous tower.
In the third example, one can use a single die that has $5$ on top.
The fourth example is impossible.
|
n = input()
a = list(map(int, input().split()))
for i in a:
if i % 7 == 0 or (i // 7) % 2 == 1 or i <= 14:
print('NO')
else:
print('YES')
|
Bob is playing with $6$-sided dice. A net of such standard cube is shown below.
[Image]
He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.
For example, the number of visible pips on the tower below is $29$ — the number visible on the top is $1$, from the south $5$ and $3$, from the west $4$ and $2$, from the north $2$ and $4$ and from the east $3$ and $5$.
[Image]
The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.
Bob also has $t$ favourite integers $x_i$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $x_i$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of favourite integers of Bob.
The second line contains $t$ space-separated integers $x_i$ ($1 \leq x_i \leq 10^{18}$) — Bob's favourite integers.
-----Output-----
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
-----Example-----
Input
4
29 34 19 38
Output
YES
YES
YES
NO
-----Note-----
The first example is mentioned in the problem statement.
In the second example, one can build the tower by flipping the top dice from the previous tower.
In the third example, one can use a single die that has $5$ on top.
The fourth example is impossible.
|
n = int(input())
arr = list(map(int, input().split()))
for x in arr:
if x < 15:
print('NO')
continue
if x % 14 >= 7 or x % 14 == 0:
print('NO')
continue
else:
print('YES')
|
Bob is playing with $6$-sided dice. A net of such standard cube is shown below.
[Image]
He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.
For example, the number of visible pips on the tower below is $29$ — the number visible on the top is $1$, from the south $5$ and $3$, from the west $4$ and $2$, from the north $2$ and $4$ and from the east $3$ and $5$.
[Image]
The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.
Bob also has $t$ favourite integers $x_i$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $x_i$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of favourite integers of Bob.
The second line contains $t$ space-separated integers $x_i$ ($1 \leq x_i \leq 10^{18}$) — Bob's favourite integers.
-----Output-----
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
-----Example-----
Input
4
29 34 19 38
Output
YES
YES
YES
NO
-----Note-----
The first example is mentioned in the problem statement.
In the second example, one can build the tower by flipping the top dice from the previous tower.
In the third example, one can use a single die that has $5$ on top.
The fourth example is impossible.
|
n = int(input())
a = list(map(int, input().split()))
for i in a:
tmp = i%14
if i>=15 and tmp>=1 and tmp<=6:
print('YES')
else:
print('NO')
|
Bob is playing with $6$-sided dice. A net of such standard cube is shown below.
[Image]
He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.
For example, the number of visible pips on the tower below is $29$ — the number visible on the top is $1$, from the south $5$ and $3$, from the west $4$ and $2$, from the north $2$ and $4$ and from the east $3$ and $5$.
[Image]
The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.
Bob also has $t$ favourite integers $x_i$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $x_i$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of favourite integers of Bob.
The second line contains $t$ space-separated integers $x_i$ ($1 \leq x_i \leq 10^{18}$) — Bob's favourite integers.
-----Output-----
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
-----Example-----
Input
4
29 34 19 38
Output
YES
YES
YES
NO
-----Note-----
The first example is mentioned in the problem statement.
In the second example, one can build the tower by flipping the top dice from the previous tower.
In the third example, one can use a single die that has $5$ on top.
The fourth example is impossible.
|
n = int(input())
for x in map(int, input().split()):
print("YES" if x > 14 and x % 14 > 0 and x % 14 <= 6 else "NO")
|
Bob is playing with $6$-sided dice. A net of such standard cube is shown below.
[Image]
He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.
For example, the number of visible pips on the tower below is $29$ — the number visible on the top is $1$, from the south $5$ and $3$, from the west $4$ and $2$, from the north $2$ and $4$ and from the east $3$ and $5$.
[Image]
The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.
Bob also has $t$ favourite integers $x_i$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $x_i$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of favourite integers of Bob.
The second line contains $t$ space-separated integers $x_i$ ($1 \leq x_i \leq 10^{18}$) — Bob's favourite integers.
-----Output-----
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
-----Example-----
Input
4
29 34 19 38
Output
YES
YES
YES
NO
-----Note-----
The first example is mentioned in the problem statement.
In the second example, one can build the tower by flipping the top dice from the previous tower.
In the third example, one can use a single die that has $5$ on top.
The fourth example is impossible.
|
n=int(input())
arr=list(map(int,input().split()))
for i in arr:
if i<=14:
print("NO")
else:
flg=False
for j in range(1,7):
if (i-j)%14==0:
flg=True
if flg:
print("YES")
else:
print("NO")
|
Monocarp had a tree which consisted of $n$ vertices and was rooted at vertex $1$. He decided to study BFS (Breadth-first search), so he ran BFS on his tree, starting from the root. BFS can be described by the following pseudocode:a = [] # the order in which vertices were processed
q = Queue()
q.put(1) # place the root at the end of the queue
while not q.empty():
k = q.pop() # retrieve the first vertex from the queue
a.append(k) # append k to the end of the sequence in which vertices were visited
for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order
q.put(y)
Monocarp was fascinated by BFS so much that, in the end, he lost his tree. Fortunately, he still has a sequence of vertices, in which order vertices were visited by the BFS algorithm (the array a from the pseudocode). Monocarp knows that each vertex was visited exactly once (since they were put and taken from the queue exactly once). Also, he knows that all children of each vertex were viewed in ascending order.
Monocarp knows that there are many trees (in the general case) with the same visiting order $a$, so he doesn't hope to restore his tree. Monocarp is okay with any tree that has minimum height.
The height of a tree is the maximum depth of the tree's vertices, and the depth of a vertex is the number of edges in the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all root's children are $1$.
Help Monocarp to find any tree with given visiting order $a$ and minimum height.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$; $a_i \neq a_j$; $a_1 = 1$) — the order in which the vertices were visited by the BFS algorithm.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the minimum possible height of a tree with the given visiting order $a$.
-----Example-----
Input
3
4
1 4 3 2
2
1 2
3
1 2 3
Output
3
1
1
-----Note-----
In the first test case, there is only one tree with the given visiting order: [Image]
In the second test case, there is only one tree with the given visiting order as well: [Image]
In the third test case, an optimal tree with the given visiting order is shown below: [Image]
|
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
lis = [ [1] ]
now = []
tmp = 0
for i in range(1,n):
if len(now) == 0:
now.append(a[i])
tmp = 1
elif now[-1] > a[i]:
if tmp == len(lis[-1]):
lis.append(now)
now = [a[i]]
tmp = 1
else:
tmp += 1
now.append(a[i])
else:
now.append(a[i])
if len(now) > 0:
lis.append(now)
#print (lis)
print (len(lis)-1)
|
Monocarp had a tree which consisted of $n$ vertices and was rooted at vertex $1$. He decided to study BFS (Breadth-first search), so he ran BFS on his tree, starting from the root. BFS can be described by the following pseudocode:a = [] # the order in which vertices were processed
q = Queue()
q.put(1) # place the root at the end of the queue
while not q.empty():
k = q.pop() # retrieve the first vertex from the queue
a.append(k) # append k to the end of the sequence in which vertices were visited
for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order
q.put(y)
Monocarp was fascinated by BFS so much that, in the end, he lost his tree. Fortunately, he still has a sequence of vertices, in which order vertices were visited by the BFS algorithm (the array a from the pseudocode). Monocarp knows that each vertex was visited exactly once (since they were put and taken from the queue exactly once). Also, he knows that all children of each vertex were viewed in ascending order.
Monocarp knows that there are many trees (in the general case) with the same visiting order $a$, so he doesn't hope to restore his tree. Monocarp is okay with any tree that has minimum height.
The height of a tree is the maximum depth of the tree's vertices, and the depth of a vertex is the number of edges in the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all root's children are $1$.
Help Monocarp to find any tree with given visiting order $a$ and minimum height.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$; $a_i \neq a_j$; $a_1 = 1$) — the order in which the vertices were visited by the BFS algorithm.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the minimum possible height of a tree with the given visiting order $a$.
-----Example-----
Input
3
4
1 4 3 2
2
1 2
3
1 2 3
Output
3
1
1
-----Note-----
In the first test case, there is only one tree with the given visiting order: [Image]
In the second test case, there is only one tree with the given visiting order as well: [Image]
In the third test case, an optimal tree with the given visiting order is shown below: [Image]
|
for _ in range(int(input())):
N = int(input())
A = [int(x) for x in input().split()]
last = i = j = 1
ans = nxt = cur = 0
while j < N:
while j < N-1 and A[j+1] > A[j]:
j += 1
if cur == 0:
ans += 1
nxt += j - i + 1
j += 1
i = j
cur += 1
if cur == last:
last = nxt
nxt = cur = 0
print(ans)
|
Monocarp had a tree which consisted of $n$ vertices and was rooted at vertex $1$. He decided to study BFS (Breadth-first search), so he ran BFS on his tree, starting from the root. BFS can be described by the following pseudocode:a = [] # the order in which vertices were processed
q = Queue()
q.put(1) # place the root at the end of the queue
while not q.empty():
k = q.pop() # retrieve the first vertex from the queue
a.append(k) # append k to the end of the sequence in which vertices were visited
for y in g[k]: # g[k] is the list of all children of vertex k, sorted in ascending order
q.put(y)
Monocarp was fascinated by BFS so much that, in the end, he lost his tree. Fortunately, he still has a sequence of vertices, in which order vertices were visited by the BFS algorithm (the array a from the pseudocode). Monocarp knows that each vertex was visited exactly once (since they were put and taken from the queue exactly once). Also, he knows that all children of each vertex were viewed in ascending order.
Monocarp knows that there are many trees (in the general case) with the same visiting order $a$, so he doesn't hope to restore his tree. Monocarp is okay with any tree that has minimum height.
The height of a tree is the maximum depth of the tree's vertices, and the depth of a vertex is the number of edges in the path from the root to it. For example, the depth of vertex $1$ is $0$, since it's the root, and the depth of all root's children are $1$.
Help Monocarp to find any tree with given visiting order $a$ and minimum height.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$; $a_i \neq a_j$; $a_1 = 1$) — the order in which the vertices were visited by the BFS algorithm.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case print the minimum possible height of a tree with the given visiting order $a$.
-----Example-----
Input
3
4
1 4 3 2
2
1 2
3
1 2 3
Output
3
1
1
-----Note-----
In the first test case, there is only one tree with the given visiting order: [Image]
In the second test case, there is only one tree with the given visiting order as well: [Image]
In the third test case, an optimal tree with the given visiting order is shown below: [Image]
|
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
if n < 3:
print(1)
continue
old = [0]
h = 1
nind = prev = cum = 0
for i in range(1,n):
if a[i] < prev:
nind += 1
if nind >= len(old):
old = [0] * cum
nind = cum = 0
h += 1
prev = a[i]
old[nind] += 1
cum += 1
print(h)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
import sys
letters = ascii_letters
input = stdin.readline
#print = stdout.write
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
can = list(map(int, input().split()))
vals = sorted([i for i in range(n) if not can[i]], key=lambda x: -arr[x])
res = [0] * n
last = 0
for i in range(n):
if can[i]:
res[i] = arr[i]
else:
res[i] = arr[vals[last]]
last += 1
print(*res)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
import sys
import math as mt
import collections as cc
import sys
import itertools as it
input=sys.stdin.readline
I=lambda:list(map(int,input().split()))
for tc in range(int(input())):
n,=I()
ar=I()
l=I()
s=[]
ll=l.copy()
loc=cc.defaultdict(int)
for i in range(n):
if l[i]==0:
s.append(ar[i])
loc[i]=-10**6
else:
loc[i]=ar[i]
s.sort(reverse=True)
j=0
for i in range(n):
if l[i]==0:
l[i]=s[j]
j+=1
else:
l[i]=ar[i]
print(*l)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
from collections import deque
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
l = list(map(int, input().split()))
bckt = []
for i in range(n):
if not l[i]:
bckt.append(a[i])
a[i] = 10**9
bckt.sort(reverse=True)
bckt = deque(bckt)
for i in range(n):
if a[i] == 10**9:
a[i] = bckt.popleft()
print(*a)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
l = list(map(int, input().split()))
s = []
out = [10**6] * n
for i in range(n):
if l[i]:
out[i] = a[i]
else:
s.append(a[i])
s.sort()
for i in range(n):
if out[i] == 10**6:
out[i] = s.pop()
print(' '.join(map(str,out)))
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
for _ in range (int(input())):
n = int(input())
a = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
b = []
for i in range (n):
if l[i]==0:
b.append(a[i])
b.sort(reverse=True)
ind = 0
for i in range (n):
if l[i]==0:
a[i]=b[ind]
ind+=1
print(*a)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
from sys import stdin
def input(): return stdin.readline().rstrip()
for _ in range(int(input())):
L = int(input())
lockSort = []
s = list(map(int, input().split()))
l = list(map(int, input().split()))
for i, c in enumerate(s):
if l[i] == 0:
lockSort.append(c)
lockSort = sorted(lockSort)[::-1]
cnt = 0
for i, c in enumerate(s):
if l[i] == 1:
print(c, end=" ")
else:
print(lockSort[cnt], end=" ")
cnt += 1
print()
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
lock = list(map(int, input().split()))
b = []
for i in range(n):
if lock[i] == 0:
b.append(a[i])
b.sort()
b=b[::-1]
ind = 0
for i in range(n):
if lock[i] == 0:
a[i]=b[ind]
ind+=1
a=[str(i) for i in a]
print(" ".join(a))
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
INF = float('inf')
def prod(arr):
ret = 1
for x in arr:
ret *= x
return ret
def tc():
n = int(input())
a = list(map(int, input().split()))
l = list(map(int, input().split()))
uis = []
for i in range(n):
if not l[i]:
uis.append(i)
uvals = [a[i] for i in uis]
uvals.sort()
for i in uis:
a[i] = uvals.pop()
print(' '.join(map(str, a)))
################
T = int(input())
for _ in range(T):
tc()
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
l = list(map(int, input().split()))
b = []
for i in range(n):
if l[i] == 0:
b.append(a[i])
b.sort()
b.reverse()
j = 0
for i in range(n):
if l[i] == 0:
a[i] = b[j]
j += 1
print(*a)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
input=__import__('sys').stdin.readline
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
blocked=[]
binds=[]
b=list(map(int,input().split()))
for i in range(n):
if not b[i]:blocked+=a[i],;binds+=i,
blocked.sort()
for i in range(len(binds)):
a[binds[i]]=blocked[i]
k1=-1
cs=0
for i in range(n):
cs+=a[i]
if cs<0:k1=i
ans1=a.copy()
blocked.reverse()
for i in range(len(binds)):
a[binds[i]]=blocked[i]
k2=-1
cs=0
for i in range(n):
cs+=a[i]
if cs<0:k2=i
ans2=a.copy()
ans=ans1 if k1<k2 else ans2
print(*ans)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
import sys
input = sys.stdin.readline
t=int(input())
for tests in range(t):
n=int(input())
A=list(map(int,input().split()))
l=list(map(int,input().split()))
B=[]
for i in range(n):
if l[i]==0:
B.append(A[i])
B.sort(reverse=True)
ind=0
for i in range(n):
if l[i]==0:
A[i]=B[ind]
ind+=1
print(*A)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
t = int(input())
for i in range(t):
n = int(input())
nums = list(map(int, input().split()))
st = list(map(int, input().split()))
sv = []
for j in range(n):
if st[j] == 0:
sv.append(nums[j])
sv.sort(reverse=True)
new = []
k = 0
for j in range(n):
if st[j] == 1:
new.append(nums[j])
else:
new.append(sv[k])
k += 1
print(*new)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
l = list(map(int,input().split()))
b = []
if sum(l) < n: # exist non-blocked
for i in range(n):
if l[i] == 0:
b.append(a[i])
b.sort(reverse=True)
j = 0
for i in range(n):
if l[i] == 0:
print(b[j], end =' ')
j += 1
else:
print(a[i], end =' ')
print()
else:
for i in range(n):
print(a[i],end=' ')
print()
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
import sys,os,io
input = sys.stdin.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
T = int(input())
ans = [0]*T
for t in range(T):
N = int(input())
A = list(map(int, input().split()))
L = list(map(int, input().split()))
B = [A[i] for i in range(N) if L[i]==0]
B.sort()
ans[t] = []
for i in range(N):
if L[i]==0:
ans[t].append(B.pop())
else:
ans[t].append(A[i])
for a in ans:
print(*a)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
for _ in range(int(input())):
n = int(input())
*arr, = list(map(int, input().split()))
*locks, = list(map(int, input().split()))
locked = []
unlocked = []
for v, is_locked in zip(arr, locks):
if not is_locked:
unlocked.append(v)
unlocked.sort(reverse=True)
arr_idx = 0
unlocked_idx = 0
while unlocked_idx < len(unlocked) and arr_idx < len(arr):
if not locks[arr_idx]:
arr[arr_idx] = unlocked[unlocked_idx]
unlocked_idx += 1
arr_idx += 1
print(*arr)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
from math import inf, ceil
from heapq import *
from collections import defaultdict
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
crr = [arr[i] for i in range(n) if not brr[i]]
crr.sort(reverse=True)
ind = 0
for i in range(n):
if not brr[i]:
arr[i] = crr[ind]
ind += 1
print(*arr)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
def main():
n = int(input())
a = list(map(int,input().split()))
l = list(map(int,input().split()))
new_lst = []
for i in range(n):
if not l[i]:
new_lst.append(a[i])
new_lst = sorted(new_lst, reverse=True)
k = 0
for i in range(n):
if not l[i]:
a[i] = new_lst[k]
k += 1
sm = 0
for i in a:
print(i, end=" ")
print()
def __starting_point():
t = int(input())
for i in range(t):
main()
__starting_point()
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
for _ in range(int(input())):
N = int(input())
List = [int(x) for x in input().split()]
Lock = [int(x) for x in input().split()]
X = []
for i in range(N):
if(not Lock[i]):
X.append(List[i])
X.sort(reverse = True)
j = 0
for i in range(N):
if(not Lock[i]):
List[i] = X[j]
j+=1
print(*List)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split(" ")))
b = list(map(int, input().split(" ")))
c=[]
for i in range(n):
if b[i]==0:
c.append(a[i])
c.sort(reverse=True)
k=0
for i in range(n):
if b[i]==0:
a[i]=c[k]
k+=1
print(" ".join(str(j) for j in a))
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict
# available on Google, not available on Codeforces
# import numpy as np
# import scipy
def solve(arr, brr): # fix inputs here
console("----- solving ------")
movable = []
for a,b in zip(arr,brr):
if b == 0:
movable.append(a)
movable = sorted(movable)
# console(movable)
for i in range(len(arr)):
if brr[i] == 0:
arr[i] = movable.pop()
# return a string (i.e. not a list or matrix)
return arr
def console(*args): # the judge will not read these print statement
print('\033[36m', *args, '\033[0m', file=sys.stderr)
return
# fast read all
# sys.stdin.readlines()
for case_num in range(int(input())):
# read line as a string
# strr = input()
# read line as an integer
k = int(input())
# read one line and parse each word as a string
# lst = input().split()
# read one line and parse each word as an integer
arr = list(map(int,input().split()))
brr = list(map(int,input().split()))
# read matrix and parse as integers (after reading read nrows)
# lst = list(map(int,input().split()))
# nrows = lst[0] # index containing information, please change
# grid = []
# for _ in range(nrows):
# grid.append(list(map(int,input().split())))
res = solve(arr,brr) # please change
# Google - case number required
# print("Case #{}: {}".format(case_num+1, res))
# Codeforces - no case number required
print(" ".join([str(x) for x in res]))
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))
t,=I()
for i in range(t):
n,=I()
a=I()
l=I()
ar=[a[i] for i in range(n) if l[i]==0]
ar.sort(reverse=True)
x=0
for i in range(n):
if l[i]==0:
a[i]=ar[x]
x+=1
print(*a)
|
You are given an array $a$, consisting of $n$ integers.
Each position $i$ ($1 \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, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$, the underlined positions are locked. You can obtain the following arrays: $[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$; $[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$; $[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$; and some others.
Let $p$ be a sequence of prefix sums of the array $a$ after the rearrangement. So $p_1 = a_1$, $p_2 = a_1 + a_2$, $p_3 = a_1 + a_2 + a_3$, $\dots$, $p_n = a_1 + a_2 + \dots + a_n$.
Let $k$ be the maximum $j$ ($1 \le j \le n$) such that $p_j < 0$. If there are no $j$ such that $p_j < 0$, then $k = 0$.
Your goal is to rearrange the values in such a way that $k$ is minimum possible.
Output the array $a$ after the rearrangement such that the value $k$ for it is minimum possible. If there are multiple answers then print any of them.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then $t$ testcases follow.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the array $a$.
The second line of each testcase contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^5 \le a_i \le 10^5$) — the initial array $a$.
The third line of each testcase contains $n$ integers $l_1, l_2, \dots, l_n$ ($0 \le l_i \le 1$), where $l_i = 0$ means that the position $i$ is unlocked and $l_i = 1$ means that the position $i$ is locked.
-----Output-----
Print $n$ integers — the array $a$ after the rearrangement. Value $k$ (the maximum $j$ such that $p_j < 0$ (or $0$ if there are no such $j$)) 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.
-----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 $p_j < 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]$.
|
from sys import stdin
def input(): return stdin.readline()
def lmi(): return list(map(int, input().split()))
T = int(input())
for _ in range(T):
input()
temp = list()
s, l = lmi(), lmi()
for i, c in enumerate(s):
if l[i] == 0: temp.append(c)
temp = sorted(temp)[::-1]
cc = 0
for i, x in enumerate(s):
if l[i] == 1: print(x, end=" ")
else:
print(temp[cc], end=" ")
cc += 1
print()
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
t = int(input())
for faw in range(t):
n = int(input())
a = [0] + list(map(int,input().split()))
nun = []
ans = []
f = True
for i in range(1, n + 1):
if a[i] == a[i-1]:
if len(nun) == 0:
f = False
break
else:
ans.append(nun.pop())
else:
ans.append(a[i])
for i in range(a[i - 1] + 1, a[i]):
nun.append(i)
if f:
print(*ans)
else:
print(-1)
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
t=int(input())
for i in range(t):
n=int(input())
a=[int(x) for x in input().split()]
b=[0] * n
m=0
c=set(range(1, n+1))
for i in range(n):
if a[i] > m:
b[i] = a[i]
m = a[i]
c.discard(a[i])
c=sorted(c)
j=0
for i in range(n):
if b[i] == 0:
b[i] = c[j]
j+=1
if b[i] > a[i]:
print(-1)
break
else:
print(*b)
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
t = int(input())
for z in range(t):
n = int(input())
arr = list(map(int, input().split()))
ret = [0 for i in range(n)]
was = set()
can = set([i for i in range(1, n + 1)])
for i in range(n):
x = arr[i]
if not x in was:
ret[i] = x
was.add(x)
left = sorted(list(can - was), reverse=True)
for i in range(n):
if not left:
break
x = left[-1]
if not ret[i]:
ret[i] = x
left.pop()
mx = ret[0]
flag = True
for i in range(n):
mx = max(mx, ret[i])
if mx != arr[i]:
flag = False
break
if flag:
print(*ret)
else:
print(-1)
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
n = int(input())
for i in range(n):
k = int(input())
used = [0]*k
a = list(map(int, input().split()))
p = 0
ans = [0]*k
for i in range(k):
if a[i] != p:
ans[i] = a[i]
used[a[i]-1] = 1
p = a[i]
t = 0
flag = 0
for i in range(k):
if ans[i] == 0:
while used[t] == 1 and t < a[i]:
t +=1
if t == a[i]:
flag = 1
break
else:
used[t] = 1
ans[i] = t+1
if flag == 1:
print(-1)
else:
print(*ans)
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 2)
c = 1
d = a[0]
ans = [a[0]]
b[a[0]] = 1
for i in range(1, len(a)):
if a[i] == a[i - 1]:
while b[c] != 0:
c += 1
if c > a[i]:
ans = -1
break
else:
b[c] = 1
ans.append(c)
else:
ans.append(a[i])
b[a[i]] = 1
if ans == -1:
print(ans)
else:
print(*ans)
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
t = int(input())
for i in range(t):
n = int(input())
a = [int(ii) for ii in input().split()]
b = [0 for ii in range(n)]
was = [0 for ii in range(n + 1)]
minimum = 1
for j in range(n):
if j == 0 or a[j] != a[j - 1]:
was[a[j]] = 1
b[j] = a[j]
else:
while was[minimum] == 1:
minimum += 1
if minimum > a[j]:
minimum = -1
break
b[j] = minimum
was[minimum] = 1
if minimum == -1:
print (-1)
else:
for j in b:
print(j, end = ' ')
print ('')
|
Permutation $p$ is a sequence of integers $p=[p_1, p_2, \dots, p_n]$, consisting of $n$ distinct (unique) positive integers between $1$ and $n$, inclusive. For example, the following sequences are permutations: $[3, 4, 1, 2]$, $[1]$, $[1, 2]$. The following sequences are not permutations: $[0]$, $[1, 2, 1]$, $[2, 3]$, $[0, 1, 2]$.
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $p$ of length $n$.
You don't know this permutation, you only know the array $q$ of prefix maximums of this permutation. Formally: $q_1=p_1$, $q_2=\max(p_1, p_2)$, $q_3=\max(p_1, p_2,p_3)$, ... $q_n=\max(p_1, p_2,\dots,p_n)$.
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $q$ for this permutation is equal to the given array).
-----Input-----
The first line contains integer number $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. Then $t$ test cases follow.
The first line of a test case contains one integer $n$ $(1 \le n \le 10^{5})$ — the number of elements in the secret code permutation $p$.
The second line of a test case contains $n$ integers $q_1, q_2, \dots, q_n$ $(1 \le q_i \le n)$ — elements of the array $q$ for secret permutation. It is guaranteed that $q_i \le q_{i+1}$ for all $i$ ($1 \le i < n$).
The sum of all values $n$ over all the test cases in the input doesn't exceed $10^5$.
-----Output-----
For each test case, print: If it's impossible to find such a permutation $p$, print "-1" (without quotes). Otherwise, print $n$ distinct integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). If there are multiple possible answers, you can print any of them.
-----Example-----
Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1
-----Note-----
In the first test case of the example answer $[1,3,4,5,2]$ is the only possible answer: $q_{1} = p_{1} = 1$; $q_{2} = \max(p_{1}, p_{2}) = 3$; $q_{3} = \max(p_{1}, p_{2}, p_{3}) = 4$; $q_{4} = \max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$; $q_{5} = \max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$.
It can be proved that there are no answers for the second test case of the example.
|
a = int(input())
Ans = []
for i in range(a):
B = []
x = int(input())
A = list(map(int, input().split()))
C = []
for j in range(1, len(A) + 1):
C.append([j, 0])
k = 1
n = 0
for j in range(len(A)):
if A[j] >k:
B.append(A[j])
k = A[j]
C[A[j] - 1][1] = 1
else:
while C[n][1] == 1:
n += 1
C[n][1] = 1
B.append(n + 1)
b0 = B[0]
Tr = True
for j in range(len(B)):
b0 = max(b0, B[j])
if A[j] != b0:
Tr = False
if Tr:
Ans.append(B)
else:
Ans.append([-1])
#Ans.append(B[-1])
for b in Ans:
print(*b)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.