inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
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 request in range(t):
n = int(input())
result, initial = list(map(int, input().split())), []
box, flag = [], True
initial.append(result[0])
for d in range(1, result[0]):
box.append(d)
for i in range(1, n):
if result[i - 1] < result[i]:
initial.append(result[i])
for d in range(result[i - 1] + 1, result[i]):
box.append(d)
else:
try:
initial.append(box.pop())
except:
flag = False
break
if flag:
print(*initial)
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 tt in range(t):
n = int(input())
ent = list(map(int,input().split()))
used = [0]*(n+1)
mnex = 1
mx = 0
ans = []
ansex = True
for i in range(n):
if ent[i] > mx:
mx = ent[i]
if used[mx] == 0:
ans.append(mx)
used[mx] = 1
else:
ansex = False
break
else:
while used[mnex] == 1:
mnex += 1
if mnex <= mx:
used[mnex]=1
ans.append(mnex)
mnex+=1
else:
ansex = False
break
if ansex:
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())
q=list(map(int,input().split()))
pr=[0]*n
ans=[0]*n
ans[0]=q[0]
sh=0
s=set([q[0]])
for i in range(1,n):
if q[i]==q[i-1]:
pr[i]=pr[i-1]
sh+=1
else:
pr[i]=i
ans[i]=q[i]
s.add(q[i])
steak=[]
for i in range(n,0,-1):
if i not in s:
steak.append(i)
tr=True
for i in range(n):
if ans[i]==0:
x=steak.pop()
if x<q[pr[i]]:
ans[i]=x
else:
tr=False
break
if tr:
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.
|
a = int(input())
for i in range(a):
b = int(input())
l = list(map(int, input().split()))
k = []
t = [i for i in range(b + 1)]
k.append(l[0])
last = k[0]
j = 0
t[last] = 0
for i in l[1:]:
if i != last:
last = i
k.append(last)
t[last] = 0
else:
while t[j] == 0:
j += 1
k.append(t[j])
j += 1
ch = [k[0]]
for i in k[1:]:
ch.append(max(ch[-1], i))
if l != ch:
print(-1)
else:
print(*k)
|
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()))
used = []
for i in range(n):
used.append(False)
p = [str(a[0])]
used[a[0] - 1] = True
ans = 1
now = 0
for i in range(1, n):
while now < n and used[now]:
now += 1
if a[i] > a[i - 1]:
p.append(str(a[i]))
if used[a[i] - 1]:
ans = 0
break
used[a[i] - 1] = True
else:
if now + 1 > a[i] or used[now]:
ans = 0
break
used[now] = True
p.append(str(now + 1))
if ans:
print(" ".join(p))
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.
|
from collections import deque
for i in range(int(input())):
n = int(input())
h = deque([i+1 for i in range(n)])
used = [False]*n
ans = [0]*n
c = list(map(int,input().split()))
ans[0] = c[0]
used[c[0]-1] = True
f = True
for i in range(n):
if i+1>c[i]:
f = False
if not f:
print(-1)
continue
for i in range(n-1):
if c[i+1]!=c[i]:
ans[i+1] = c[i+1]
else:
x = h.popleft()
while used[x-1]:
x = h.popleft()
ans[i+1] = x
used[ans[i+1] - 1] = True
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 magic in range(t):
n=int(input())
res=[0 for j in range(n+1)]
have=[1 for sth in range(n+1)]
br_p=0
given=[0]+list(map(int,input().split()))
for i in range(1,n+1):
if given[i-1]<given[i]:
res[i]=given[i]
have[given[i]]=0
elif given[i-1]>given[i]:
br_p=1
break
if br_p:
print(-1)
else:
ind_last=1
for i in range(1,n+1):
if res[i]==0:
while have[ind_last]==0:
ind_last+=1
res[i]=ind_last
ind_last+=1
for i in range(1,n+1):
if given[i]<res[i]:
br_p=1
break
if br_p:
print(-1)
else:
print(*res[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 = list(map(int, input().split()))
b = [False] * n
b[a[0] - 1] = True
k = 0
res = [a[0]]
flag = True
for j in range(1, n):
if a[j] == a[j - 1]:
while k < n and b[k]:
k += 1
if k + 1 > a[j]:
flag = False
break
res.append(k + 1)
b[k] = True
else:
b[a[j] - 1] = True
res.append(a[j])
if flag:
print(' '.join(map(str, res)))
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 g in range(t):
n=int(input())
a=list(map(int,input().split()))
b=list(False for i in range(n))
flag=False
count=0
res=list(range(n))
for i in range(n):
if i==0:
res[i]=a[i]
b[a[i]-1]=True
elif a[i]!=a[i-1]:
res[i]=a[i]
b[a[i]-1]=True
else:
for j in range(count,n):
if j+1>a[i]:
flag=True
count=j
break
else:
if not b[j]:
res[i]=j+1
b[j]=True
count=j
break
if flag:
print(-1)
else:
for i in range(n):
print(res[i],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.
|
for __ in range(int(input())):
n = int(input())
ar1 = list(map(int, input().split()))
ar = ar1.copy()
lol = set()
for j in range(1, n + 1):
lol.add(j)
lol.discard(ar[0])
for i in range(1, n):
if ar1[i] > ar1[i - 1]:
lol.discard(ar1[i])
else:
ar[i] = 0
kek = list(lol)
kek.sort()
num = 0
flag = 0
for j in range(n):
if ar[j] == 0:
ar[j] = kek[num]
num += 1
if ar[j] > ar1[j]:
flag = 1
if flag == 1:
print(-1)
else:
print(*ar)
|
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.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
used = [False] * (n + 1)
ans[0] = a[0]
used[a[0]] = True
lst = 1
ok = True
for i in range(1, n):
if a[i] > a[i - 1]:
ans[i] = a[i]
used[a[i]] = True
elif a[i] < a[i - 1]:
print(-1)
ok = False
break
else:
while used[lst]:
lst += 1
#print(lst)
if a[i] < lst:
print(-1)
ok = False
break
else:
ans[i] = lst
lst += 1
used[ans[i]] = True
if ok:
for i in range(n):
print(ans[i], 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.
|
m = int(input())
for j in range(m):
n = int(input())
a = list(map(int, input().split()))
b = []
used = [0] * (n + 1)
t = 1
b.append(a[0])
f = True
used[a[0]] = 1
for k in range(1, n):
if a[k] == a[k - 1]:
while used[t] == 1:
t += 1
if t < a[k - 1]:
b.append(t)
used[t] = 1
t += 1
else:
f = False
break
elif a[k] > a[k - 1]:
b.append(a[k])
used[a[k]] = 1
else:
f = False
break
if f:
print(*b)
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())
q = list(map(int, input().split()))
used = set()
ans = []
used.add(q[0])
ans.append(q[0])
cnt = 1
flag = False
for i in range(1, n):
if q[i] == q[i - 1]:
while cnt in used:
cnt += 1
used.add(cnt)
if q[i] > cnt:
ans.append(cnt)
else:
flag = True
break
else:
used.add(q[i])
ans.append(q[i])
if flag:
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.
|
for i in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
q=-1
t=0
r=[]
for i in range(n):
if a[i]<q or a[i]<i+1:
t=1
break
else:
q=a[i]
if t==1:
print(-1)
else:
q=-1
w=[True]*n
z=0
for i in range(n):
if a[i]>q:
r.append(a[i])
w[a[i]-1]=False
q=a[i]
else:
while w[z]==False:
z+=1
r.append(z+1)
z+=1
print(*r)
|
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()))
used = [0for i in range(n + 2)]
ans = []
minimum = 1
ans.append(a[0])
used[a[0]] = 1
if a[0] == 1:
minimum = 2
for i in range(1, len(a)):
if a[i] != a[i - 1]:
ans.append(a[i])
used[a[i]] = 1
if a[i] == minimum:
minimum += 1
else:
ans.append(minimum)
used[minimum] = 1
while used[minimum] == 1:
minimum += 1
maximum = 0
flag = True
for i in range(len(ans)):
maximum = max(maximum, ans[i])
if a[i] != maximum:
flag = False
if flag and a[-1] == n:
print(" ".join(map(str, 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())
q = list(map(int, input().split()))
used = {q[0]: True}
seq = [q[0]]
ks = 1
for j in range(1, n):
if q[j] == q[j - 1]:
for k in range(ks, q[j]):
if used.get(k) is None:
seq.append(k)
used[k] = True
ks = k + 1
break
else:
print(-1)
break
else:
used[q[j]] = True
seq.append(q[j])
else:
print(*seq)
|
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.
|
w = int(input())
for q in range(w):
fr = 1
n = int(input())
a = list(map(int, input().split()))
a.append(a[0] - 1)
e = [1] * (n+1)
ei = 1
r = [0] * n
for i in range(n):
if a[i] == a[i-1]:
while e[ei] == 0 and ei < n:
ei += 1
if ei > a[i]:
print(-1)
fr = 0
break
r[i] = ei
e[ei] = 0
else:
if e[a[i]] == 1:
e[a[i]] = 0
r[i] = a[i]
else:
print(-1)
fr = 0
break
if fr:
print(*r)
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
for _ in range(int(input())):
print("YES" if set(input()).intersection(input()) else "NO")
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
for _ in range(int(input())):
s = input()
t = input()
ans = 'NO'
for c in s:
if c in t:
ans = 'YES'
break
print(ans)
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for z in range(q):
s = input()
t = input()
for c in s:
if c in t:
print('YES')
break
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for qq in range(q):
s1 = input()
s2 = input()
for i in s1:
if i in s2:
print('YES')
break
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
a = input()
b = input()
done = False
for i in a:
if i in b:
done = True
break
print('YES' if done else 'NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
n = int(input())
for i in range(n):
a = input()
b = input()
a, b = min(a, b), max(a, b)
h = True
for j in range(len(a)):
if a[j] in b:
print('YES')
h = False
break
if h:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
for _ in range(int(input())):
line1 = input()
line2 = input()
if set(line1) & set(line2):
print("YES")
else:
print("NO")
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
for i in range(int(input())):
s1 = set(input())
s2 = set(input())
if len(s1 & s2) > 0:
print("YES")
else:
print("NO")
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
s = input()
t = input()
s = set(s)
t = set(t)
if len(s.intersection(t)) > 0:
print('YES')
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
a = int(input())
for i in range(a):
b = input()
c = input()
y = 0
for i in b:
if i in c:
y = 1
else:
pass
if y == 1:
print('YES')
else:
print("NO")
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
for _ in range(int(input())):
s,t=input(), input()
k="NO"
for i in s:
if i in t:
k="YES"
break
print(k)
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
n=int(input())
for i in range(n):
s=input()
t=input()
s1 = set()
s2=set()
for q in s:
s1.add(q)
for q in t:
s2.add(q)
if len(s1.intersection(s2)):
print('YES')
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
s1 = input()
s2 = input()
if len(set(s1) & set(s2)) > 0:
print('YES')
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
s = input()
t = input()
if set(s) & set(t):
print('YES')
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
k = 0
s = input()
t = input()
for p in range(len(s)):
for j in range(len(s)):
if s[p] == t[j]:
k += 1
if k > 0:
print('YES')
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
import sys
import math
from math import ceil
import bisect
def input():
return sys.stdin.readline().strip()
def iinput():
return int(input())
def finput():
return float(input())
def tinput():
return input().split()
def rinput():
return map(float, tinput())
def rlinput():
return list(rinput())
def sli():
return set(list(input()))
def modst(a, s):
res = 1
while s:
if s % 2:
res *= a
a *= a
s //= 2
return res
def pro(x):
if x < 37:
return (x - 1) // 4
else:
return 8 - (x - 37) // 2
def main():
q = sli()
w = sli()
flag = False
for i in q:
if i in w:
flag = True
break
if flag:
print('YES')
else:
print('NO')
for i in range(iinput()):
main()
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
c = True
s = input()
t = input()
for j in s:
if j in t:
print("YES")
c = False
break
if c:
print("NO")
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q=int(input())
for i in range(q):
test=set()
s=list(input())
t=list(input())
for item in s:
test.add(item)
for item in t:
if item in test:
print('YES')
break
else:
print('NO')
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
n = int(input())
for i in range(n):
m1 = set(list(input()))
m2 = set(list(input()))
f = 'NO'
for el in m1:
if el in m2:
f = 'YES'
print(f)
|
You are given two strings of equal length $s$ and $t$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.
During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.
For example, if $s$ is "acbc" you can get the following strings in one operation: "aabc" (if you perform $s_2 = s_1$); "ccbc" (if you perform $s_1 = s_2$); "accc" (if you perform $s_3 = s_2$ or $s_3 = s_4$); "abbc" (if you perform $s_2 = s_3$); "acbb" (if you perform $s_4 = s_3$);
Note that you can also apply this operation to the string $t$.
Please determine whether it is possible to transform $s$ into $t$, applying the operation above any number of times.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 100$)Β β the number of queries. Each query is represented by two consecutive lines.
The first line of each query contains the string $s$ ($1 \le |s| \le 100$) consisting of lowercase Latin letters.
The second line of each query contains the string $t$ ($1 \le |t| \leq 100$, $|t| = |s|$) consisting of lowercase Latin letters.
-----Output-----
For each query, print "YES" if it is possible to make $s$ equal to $t$, and "NO" otherwise.
You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).
-----Example-----
Input
3
xabb
aabx
technocup
technocup
a
z
Output
YES
YES
NO
-----Note-----
In the first query, you can perform two operations $s_1 = s_2$ (after it $s$ turns into "aabb") and $t_4 = t_3$ (after it $t$ turns into "aabb").
In the second query, the strings are equal initially, so the answer is "YES".
In the third query, you can not make strings $s$ and $t$ equal. Therefore, the answer is "NO".
|
q = int(input())
for i in range(q):
s1 = input()
s2 = input()
f = 1
for i in range(len(s1)):
for j in range(len(s2)):
if f and s1[i] == s2[j]:
print("YES")
f = 0
if f:
print("NO")
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
for tc in range(int(input())):
n,m = list(map(int, input().split()))
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
aidx = {}
for i,e in enumerate(al):
aidx[e]=i
midx = -1
res = 0
for i,e in enumerate(bl):
idx = aidx[e]
if idx <= midx:
res += 1
else:
res += 2*(idx-i)+1
midx = max(midx, idx)
print(res)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
for q11 in range(int(input())):
n, m = list(map(int, input().split()))
a = [int(q)-1 for q in input().split()]
s = [int(q)-1 for q in input().split()]
d = [0]*n
for q in range(n):
d[a[q]] = q
max1, ans = -1, 0
for q in range(m):
if d[s[q]] > max1:
ans += 2*(d[s[q]]-q)+1
max1 = d[s[q]]
else:
ans += 1
print(ans)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
t = int(input())
for _ in range(t):
n, m = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
order = [0] * (n + 1)
for i in range(n):
order[a[i]] = i
pref_max = 0
ans = 0
for i in range(m):
if order[b[i]] < pref_max:
ans += 1
else:
pref_max = order[b[i]]
ans += 2 * (order[b[i]] - i) + 1
print(ans)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
T = int(input())
for t in range(T):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
a[i] -= 1
for j in range(m):
b[j] -= 1
nummap = [0 for i in range(n)]
for i in range(n):
nummap[a[i]] = i
b = [nummap[b[i]] for i in range(m)]
largest = -1
res = 0
for i in range(m):
if b[i] >= largest:
res += 2*(b[i]-i) + 1
largest = b[i]
else:
res += 1
print(res)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
from math import *
import os, sys
from bisect import *
from io import BytesIO
#input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.setrecursionlimit(10 ** 9)
#sys.stdin = open("moobuzz.in", 'r')
#sys.stdout = open("moobuzz.out", 'w')
for i in range(int(input())):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
for i in range(n):
d[a[i]] = i
ans = 0
mx = 0
for i in range(m):
if mx < d[b[i]]:
ans += 2 * (d[b[i]] - i) + 1
mx = d[b[i]]
else:
ans += 1
print(ans)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
A = [int(a)-1 for a in input().split()]
B = [int(a)-1 for a in input().split()]
X = [0] * N
for i, a in enumerate(A):
X[a] = i
ans = 0
ma = -1
for i, b in enumerate(B):
ans += (X[b] - i) * 2 + 1 if X[b] > ma else 1
ma = max(ma, X[b])
print(ans)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
memo = {}
for i in range(n):
memo[a[i]] = i
max_num = -1
cnt = 0
ans = 0
for i in range(m):
if max_num < memo[b[i]]:
ans += 2 * (memo[b[i]] - cnt) + 1
max_num = memo[b[i]]
cnt += 1
else:
ans += 1
cnt += 1
print(ans)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
n,m=list(map(int,input().split()))
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
ans=0
ind=0
vis=[0 for i in range(n)]
co=0
for i in range(m):
if vis[bb[i]-1]==1:
ans+=1
co-=1
continue
while ind<n:
co+=1
if aa[ind]==bb[i]:
vis[aa[ind]-1]=1
ind+=1
break
else:
vis[aa[ind]-1]=1
ind+=1
co-=1
ans+=co*2+1
# print(ans,ind)
print(ans)
# print()
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
q = int(input())
t = 1
vis = [0] * 300000
for i in range(q):
n, m = [int(x) for x in input().split()]
a = [ int(x) for x in input().split()]
b = [int(x) for x in input().split()]
arr = 0
i = 0
k = 0
for item in b:
if vis[item] == t:
arr += 1
k -= 1
continue
while i < n:
vis[a[i]] = t
if a[i] == item:
arr += (2 * k) + 1
i += 1
break
i += 1
k += 1
print(arr)
t += 1
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
for _ in range(int(input())):
n, m = list(map(int, input().split()))
topmost = set()
a = iter(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for bi in b:
if bi in topmost:
k = 0
topmost.remove(bi)
else:
k = len(topmost)
for ai in a:
if ai == bi:
break
topmost.add(ai)
k += 1
else:
raise ValueError(f'No {bi} in a')
ans += 2 * k + 1
print(ans)
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None]*T
for qu in range(T):
N, M = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
A.reverse()
res = 0
seen = set()
for b in B:
res += 1
if b in seen:
seen.remove(b)
continue
res += 2*len(seen)
while A[-1] != b:
seen.add(A.pop())
res += 2
A.pop()
Ans[qu] = res
print('\n'.join(map(str, Ans)))
|
Santa has to send presents to the kids. He has a large stack of $n$ presents, numbered from $1$ to $n$; the topmost present has number $a_1$, the next present is $a_2$, and so on; the bottom present has number $a_n$. All numbers are distinct.
Santa has a list of $m$ distinct presents he has to send: $b_1$, $b_2$, ..., $b_m$. He will send them in the order they appear in the list.
To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $k$ presents above the present Santa wants to send, it takes him $2k + 1$ seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).
What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.
Your program has to answer $t$ different test cases.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 100$) β the number of test cases.
Then the test cases follow, each represented by three lines.
The first line contains two integers $n$ and $m$ ($1 \le m \le n \le 10^5$) β the number of presents in the stack and the number of presents Santa wants to send, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le n$, all $a_i$ are unique) β the order of presents in the stack.
The third line contains $m$ integers $b_1$, $b_2$, ..., $b_m$ ($1 \le b_i \le n$, all $b_i$ are unique) β the ordered list of presents Santa has to send.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
-----Example-----
Input
2
3 3
3 1 2
3 2 1
7 2
2 1 7 3 4 5 6
3 1
Output
5
8
|
import sys
# inf = open('input.txt', 'r')
# reader = (map(int, line.split()) for line in inf)
reader = (list(map(int, line.split())) for line in sys.stdin)
input = reader.__next__
t, = input()
for _ in range(t):
n, m = input()
a = list(input())
b = list(input())
d = {el:i for i, el in enumerate(a)}
maxPos = d[b[0]]
ans = 2 * maxPos + 1
Nremoved = 1
for el in b[1:]:
pos = d[el]
if pos < maxPos:
ans += 1
else:
ans += 2 * (pos - Nremoved) + 1
maxPos = pos
Nremoved += 1
print(ans)
# inf.close()
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
T = int(input())
for test in range(T):
n,t = list(map(int,input().split()))
a = list(map(int,input().split()))
res = []
j=0
for i in a:
if(i*2<t):
res+=["0"]
elif(i*2>t):
res+=["1"]
else:
res.append(["0","1"][j])
j = 1-j
print(" ".join(res))
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
t = int(input())
for _ in range(t):
n, T = map(int, input().split())
a = list(map(int, input().split()))
white = set()
if not T%2 and T//2 in a:
halfcount = 0
for i in range(len(a)):
if a[i] == T//2:
if halfcount % 2:
a[i] = 1
else:
a[i] = 0
halfcount += 1
else:
if T-a[i] in white:
a[i] = 1
else:
white.add(a[i])
a[i] = 0
else:
for i in range(len(a)):
if T-a[i] in white:
a[i] = 1
else:
white.add(a[i])
a[i] = 0
print(*a)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import sys
import math
def II():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def MI():
return list(map(int, sys.stdin.readline().split()))
def SI():
return sys.stdin.readline().strip()
t = II()
for q in range(t):
n,k = MI()
a = LI()
d = [0]*n
c = a.count(k//2)
boo = k%2 == 0
count = 0
for i in range(n):
if a[i]<k//2:
d[i] = 0
elif a[i] == k//2:
if not boo:
d[i] = 0
elif count<c//2:
d[i] = 0
count+=1
else:
d[i] = 1
else:
d[i] = 1
print(*d)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
from collections import defaultdict
T = int(input())
for t in range(T):
n, T = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
pen_in_c = defaultdict(int)
pen_in_d = defaultdict(int)
answer = []
for el in A:
if pen_in_d[el] < pen_in_c[el]:
answer.append(1)
pen_in_d[T - el] += 1
else:
answer.append(0)
pen_in_c[T - el] += 1
print(' '.join(map(str, answer)))
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
t = int(input())
for _ in range(t):
n,T = map(int,input().split())
l1 = [int(x) for x in input().split()]
current = 0
for i in range(n):
if T%2==0 and l1[i]==T//2:
#print("HERE")
if current:
l1[i]=0
current = 0
else:
l1[i]=1
current = 1
else:
l1[i]=int(l1[i]>(T//2))
print(*l1)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
input=__import__('sys').stdin.readline
for _ in range(int(input())):
n,T=map(int,input().split())
s=list(map(int,input().split()))
ans=[0]*n
g={} # last ind with sum x
for i in range(n):
if T-s[i] in g:
ans[i]=1-ans[g[T-s[i]]]
g[s[i]]=i
print(*ans)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
for _ in range(int(input())):
n, t = list(map(int, input().split()))
*arr, = list(map(int, input().split()))
flip = 0
for i in range(n):
if 2 * arr[i] > t:
arr[i] = 1
elif 2 * arr[i] < t:
arr[i] = 0
else:
arr[i] = flip
flip = 1 - flip
print(*arr)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
from collections import defaultdict
def solve():
n, T = list(map(int, input().split()))
a = list(map(int, input().split()))
white = defaultdict(int)
black = defaultdict(int)
ans = [0]*n
for i, x in enumerate(a):
if white[T-x] > black[T-x]:
black[x] += 1
ans[i] = 1
else:
white[x] += 1
ans[i] = 0
print(*ans)
return
def main():
T = int(input())
for i in range(T):
solve()
return
def __starting_point():
main()
__starting_point()
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
t = int(input())
for case in range(t):
n, T = map(int, input().split())
a = [int(x) for x in input().split()]
halfTticker = False
halfT = T / 2
white = set()
for x in a:
if x == halfT:
print(int(halfTticker), end=' ')
halfTticker = not halfTticker
elif x in white:
print(0, end=' ')
elif T - x in white:
print(1, end=' ')
else:
white.add(x)
print(0, end=' ')
print()
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n,k = list(map(int, input().split()))
arr = list(map(int, input().split()))
alt = 0
ans = []
for i in range(len(arr)):
if k%2==1:
if arr[i] < k/2:
ans.append(0)
else:
ans.append(1)
else:
if arr[i] == k//2:
ans.append(alt%2)
alt += 1
elif arr[i] < k//2:
ans.append(0)
else:
ans.append(1)
print(*ans)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import math
import sys
class Read:
@staticmethod
def string():
return input()
@staticmethod
def int():
return int(input())
@staticmethod
def list(sep=' '):
return input().split(sep)
@staticmethod
def list_int(sep=' '):
return list(map(int, input().split(sep)))
def solve():
n, T = Read.list_int()
a = Read.list_int()
tmp = {}
res = []
for i in a:
v = T - i
r = '1'
if v in tmp:
if tmp[v] == '1':
r = '0'
tmp[i] = r
res.append(r)
print(' '.join(res))
# query_count = 1
query_count = Read.int()
while query_count:
query_count -= 1
solve()
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
for _ in range(int(input())):
n, t = tuple(map(int, input().split()))
arr = list(map(int, input().split()))
ans = [-1] * n
if t % 2 == 0:
x = t // 2
c = arr.count(x)
c2 = 0
for i in range(n):
if arr[i] != x:
continue
if c2 < c // 2:
ans[i] = 0
else:
ans[i] = 1
c2 += 1
for i in range(n):
if ans[i] != -1:
continue
if arr[i] <= t // 2:
ans[i] = 0
else:
ans[i] = 1
print(*ans)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
t = int(input())
for _ in range(t):
n, u = map(int, input().split())
a = list(map(int, input().split()))
k=u//2
p = [-1 for i in range(n)]
if u%2!=0:
for i in range(n):
if a[i]<=k:
p[i]=0
else:
p[i]=1
else:
x=0
for i in range(n):
if a[i]<k:
p[i]=0
elif a[i]>k:
p[i]=1
elif a[i]==k:
if x==0:
p[i]=0
x=1
else:
p[i]=1
x=0
print(*p)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import os
from sys import stdin, stdout
class Input:
def __init__(self):
self.lines = stdin.readlines()
self.idx = 0
def line(self):
try:
return self.lines[self.idx].strip()
finally:
self.idx += 1
def array(self, sep = ' ', cast = int):
return list(map(cast, self.line().split(sep = sep)))
def known_tests(self):
num_of_cases, = self.array()
for case in range(num_of_cases):
yield self
def unknown_tests(self):
while self.idx < len(self.lines):
yield self
def problem_solver():
'''
'''
def solver(inpt):
n, T = inpt.array()
a = inpt.array()
b = []
c = 0
for x in a:
if x * 2 > T:
b.append(1)
elif x * 2 == T:
b.append(c & 1)
c += 1
else:
b.append(0)
print(*b)
'''Returns solver'''
return solver
try:
solver = problem_solver()
for tc in Input().known_tests():
solver(tc)
except Exception as e:
import traceback
traceback.print_exc(file=stdout)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import sys
input = sys.stdin.readline
def main():
n, t = map(int, input().split())
alst = list(map(int, input().split()))
lst = [[i, a] for i, a in enumerate(alst)]
lst.sort()
if t % 2 == 0:
mid = t // 2
else:
mid = t / 2
ans = [-1 for _ in range(n)]
flg = False
for i, a in lst:
if a < mid:
ans[i] = 0
elif a > mid:
ans[i] = 1
elif flg:
flg = False
ans[i] = 0
else:
flg = True
ans[i] = 1
print(*ans)
for _ in range(int(input())):
main()
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import os
import sys
import io
# input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # η₯ε₯εΏ«θ―»οΌζ ζ³θΏθ‘θ°θ―
GANS = []
# def print(*args): # η₯ε₯εΏ«εοΌζεεΎεδΈos.write
# nonlocal GANS
# for i in args:
# GANS.append(f'{i}'.encode())
t = int(input())
for _ in range(t):
n,k = map(int,input().split())
li = [int(i) for i in input().split()]
d1 = {}
d2 = {}
col = []
for i in li:
if d1.get(k-i,0) > d2.get(k-i,0):
d2[i] = d2.get(i,0) + 1
col.append(1)
else:
d1[i] = d1.get(i,0) + 1
col.append(0)
print(*col)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import math
import collections
t=int(input())
for w in range(t):
n,k=(int(i) for i in input().split())
l=[int(i) for i in input().split()]
l1=[0]*n
c=0
for i in range(n):
if(l[i]>k/2):
l1[i]=1
elif(l[i]<k/2):
l1[i]=0
else:
if(c%2==0):
l1[i]=0
c+=1
else:
l1[i]=1
c+=1
print(*l1)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
for _ in range (int(input())):
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set()
c=0
b=[0]*n
for i in range (n):
if 2*a[i]==k:
b[i]=c
c=1-c
else:
if a[i] in s:
b[i]=1
else:
s.add(k-a[i])
print(*b)
|
RedDreamer has an array $a$ consisting of $n$ non-negative integers, and an unlucky integer $T$.
Let's denote the misfortune of array $b$ having length $m$ as $f(b)$ β the number of pairs of integers $(i, j)$ such that $1 \le i < j \le m$ and $b_i + b_j = T$. RedDreamer has to paint each element of $a$ into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays $c$ and $d$ so that all white elements belong to $c$, and all black elements belong to $d$ (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that $f(c) + f(d)$ is minimum possible.
For example:
if $n = 6$, $T = 7$ and $a = [1, 2, 3, 4, 5, 6]$, it is possible to paint the $1$-st, the $4$-th and the $5$-th elements white, and all other elements black. So $c = [1, 4, 5]$, $d = [2, 3, 6]$, and $f(c) + f(d) = 0 + 0 = 0$; if $n = 3$, $T = 6$ and $a = [3, 3, 3]$, it is possible to paint the $1$-st element white, and all other elements black. So $c = [3]$, $d = [3, 3]$, and $f(c) + f(d) = 0 + 1 = 1$.
Help RedDreamer to paint the array optimally!
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains two integers $n$ and $T$ ($1 \le n \le 10^5$, $0 \le T \le 10^9$) β the number of elements in the array and the unlucky integer, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
The sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case print $n$ integers: $p_1$, $p_2$, ..., $p_n$ (each $p_i$ is either $0$ or $1$) denoting the colors. If $p_i$ is $0$, then $a_i$ is white and belongs to the array $c$, otherwise it is black and belongs to the array $d$.
If there are multiple answers that minimize the value of $f(c) + f(d)$, print any of them.
-----Example-----
Input
2
6 7
1 2 3 4 5 6
3 6
3 3 3
Output
1 0 0 1 1 0
1 0 0
|
import random
def gcd(a, b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a, b):
return (a * b) / gcd(a, b)
for _ in range(int(input())):
#n = int(input())
n,t= map(int, input().split())
a = list(map(int, input().split()))
d={}
for i in range(n):
if a[i] in d:
d[a[i]].append(i)
else:
d[a[i]]=[i]
ans=[-1]*n
for i in d.keys():
if ans[d[i][0]]==-1:
if i==t//2:
for j in range(len(d[i])//2):
ans[d[i][j]]=0
for j in range(len(d[i])//2,len(d[i])):
ans[d[i][j]] = 1
else:
for j in range(len(d[i])):
ans[d[i][j]]=0
if t-i in d:
for j in range(len(d[t-i])):
ans[d[t-i][j]]=1
for i in ans:
print(i,end=' ')
print('')
|
You are given a integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, $s$ is not divisible by any of it's digits.
-----Input-----
The input consists of multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 400$), the number of test cases. The next $t$ lines each describe a test case.
Each test case contains one positive integer $n$ ($1 \leq n \leq 10^5$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.
-----Output-----
For each test case, print an integer $s$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $s$, print any solution.
-----Example-----
Input
4
1
2
3
4
Output
-1
57
239
6789
-----Note-----
In the first test case, there are no possible solutions for $s$ consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: $23$, $27$, $29$, $34$, $37$, $38$, $43$, $46$, $47$, $49$, $53$, $54$, $56$, $57$, $58$, $59$, $67$, $68$, $69$, $73$, $74$, $76$, $78$, $79$, $83$, $86$, $87$, $89$, $94$, $97$, and $98$.
For the third test case, one possible solution is $239$ because $239$ is not divisible by $2$, $3$ or $9$ and has three digits (none of which equals zero).
|
#JMD
#Nagendra Jha-4096
import sys
import math
#import fractions
#import numpy
###File Operations###
fileoperation=0
if(fileoperation):
orig_stdout = sys.stdout
orig_stdin = sys.stdin
inputfile = open('W:/Competitive Programming/input.txt', 'r')
outputfile = open('W:/Competitive Programming/output.txt', 'w')
sys.stdin = inputfile
sys.stdout = outputfile
###Defines...###
mod=1000000007
###FUF's...###
def nospace(l):
ans=''.join(str(i) for i in l)
return ans
##### Main ####
t=int(input())
for tt in range(t):
n=int(input())
if n==1:
print(-1)
else:
s="2"
for i in range(n-1):
s+='3'
print(s)
#n,k,s= map(int, sys.stdin.readline().split(' '))
#a=list(map(int,sys.stdin.readline().split(' ')))
#####File Operations#####
if(fileoperation):
sys.stdout = orig_stdout
sys.stdin = orig_stdin
inputfile.close()
outputfile.close()
|
You are given a integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, $s$ is not divisible by any of it's digits.
-----Input-----
The input consists of multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 400$), the number of test cases. The next $t$ lines each describe a test case.
Each test case contains one positive integer $n$ ($1 \leq n \leq 10^5$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.
-----Output-----
For each test case, print an integer $s$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $s$, print any solution.
-----Example-----
Input
4
1
2
3
4
Output
-1
57
239
6789
-----Note-----
In the first test case, there are no possible solutions for $s$ consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: $23$, $27$, $29$, $34$, $37$, $38$, $43$, $46$, $47$, $49$, $53$, $54$, $56$, $57$, $58$, $59$, $67$, $68$, $69$, $73$, $74$, $76$, $78$, $79$, $83$, $86$, $87$, $89$, $94$, $97$, and $98$.
For the third test case, one possible solution is $239$ because $239$ is not divisible by $2$, $3$ or $9$ and has three digits (none of which equals zero).
|
for _ in range(int(input())):
n = int(input())
if n == 1:
print(-1)
else:
print("2"+"3"*(n-1))
|
You are given a integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, $s$ is not divisible by any of it's digits.
-----Input-----
The input consists of multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 400$), the number of test cases. The next $t$ lines each describe a test case.
Each test case contains one positive integer $n$ ($1 \leq n \leq 10^5$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.
-----Output-----
For each test case, print an integer $s$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $s$, print any solution.
-----Example-----
Input
4
1
2
3
4
Output
-1
57
239
6789
-----Note-----
In the first test case, there are no possible solutions for $s$ consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: $23$, $27$, $29$, $34$, $37$, $38$, $43$, $46$, $47$, $49$, $53$, $54$, $56$, $57$, $58$, $59$, $67$, $68$, $69$, $73$, $74$, $76$, $78$, $79$, $83$, $86$, $87$, $89$, $94$, $97$, and $98$.
For the third test case, one possible solution is $239$ because $239$ is not divisible by $2$, $3$ or $9$ and has three digits (none of which equals zero).
|
def main():
import sys
input = sys.stdin.readline
for _ in range(int(input())):
N = int(input())
if N == 1:
print(-1)
else:
ans = ['3'] * N
ans[0] = '2'
print(''.join(ans))
def __starting_point():
main()
__starting_point()
|
You are given a integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, $s$ is not divisible by any of it's digits.
-----Input-----
The input consists of multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 400$), the number of test cases. The next $t$ lines each describe a test case.
Each test case contains one positive integer $n$ ($1 \leq n \leq 10^5$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.
-----Output-----
For each test case, print an integer $s$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $s$, print any solution.
-----Example-----
Input
4
1
2
3
4
Output
-1
57
239
6789
-----Note-----
In the first test case, there are no possible solutions for $s$ consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: $23$, $27$, $29$, $34$, $37$, $38$, $43$, $46$, $47$, $49$, $53$, $54$, $56$, $57$, $58$, $59$, $67$, $68$, $69$, $73$, $74$, $76$, $78$, $79$, $83$, $86$, $87$, $89$, $94$, $97$, and $98$.
For the third test case, one possible solution is $239$ because $239$ is not divisible by $2$, $3$ or $9$ and has three digits (none of which equals zero).
|
tc = int(input())
for _ in range(tc):
n = int(input())
if n > 1:
print("2" + "3" * (n-1))
else:
print(-1)
|
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.
The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive.
All problems are divided into two types: easy problems β Petya takes exactly $a$ minutes to solve any easy problem; hard problems β Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$.
For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved.
For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems.
Thus, the answer to this test is $2$.
Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
-----Input-----
The first line contains the integer $m$ ($1 \le m \le 10^4$)Β β the number of test cases in the test.
The next lines contain a description of $m$ test cases.
The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$)Β β the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.
The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard.
The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print the answers to $m$ test cases. For each set, print a single integerΒ β maximal number of points that he can receive, before leaving the exam.
-----Example-----
Input
10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
Output
3
2
1
0
1
4
0
1
2
1
|
import sys
from operator import itemgetter
def count(a, b, num_a, num_b, cur_time):
current_result = 0
#print('count time = ', cur_time, "num_a =", num_a, 'num_b = ', num_b)
if num_a * a + num_b * b <= cur_time and cur_time >= 0:
cur_time -= num_a * a + num_b * b
current_result = num_a + num_b
if num_a < total_a:
if (total_a - num_a) * a <= cur_time:
current_result += total_a - num_a
cur_time -= (total_a - num_a) * a
#print(1)
else:
current_result += cur_time // a
cur_time -= a *(cur_time // a)
#print(2)
if num_b < total_b:
if (total_b - num_b) * b <= cur_time:
current_result += total_b - num_b
#print(3)
else:
#print(4)
current_result += cur_time // b
#print('current_result = ', current_result)
return current_result
def solve(n, T, a, b, tasks, total_a, total_b):
tasks = sorted(tasks)
#print(tasks)
result = 0
num_a = 0
num_b = 0
for i in range(len(tasks)):
time, t = tasks[i]
#print(tasks[i])
cur_time = time - 1
#print('cur time = ', cur_time)
current_result = count(a, b, num_a, num_b, cur_time)
result = max(current_result, result)
if t == 0:
num_a += 1
else:
num_b += 1
if i == len(tasks) - 1 or tasks[i + 1][1] != tasks[i][1]:
result = max(result, count(a, b, num_a, num_b, cur_time))
#print("i =", i, "result = ", result)
result = max(result, count(a, b, total_a, total_b, T))
return result
q = int(input())
for i in range(q):
n, T, a, b = list(map(int, input().split()))
types = list(map(int, input().split()))
total_a, total_b = 0, 0
for t in types:
if t == 0:
total_a += 1
else:
total_b += 1
t = list(map(int, input().split()))
#print(t)
#print(types)
tasks = list(zip(t, types))
print(solve(n, T, a, b, tasks, total_a, total_b))
|
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.
The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive.
All problems are divided into two types: easy problems β Petya takes exactly $a$ minutes to solve any easy problem; hard problems β Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$.
For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved.
For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems.
Thus, the answer to this test is $2$.
Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
-----Input-----
The first line contains the integer $m$ ($1 \le m \le 10^4$)Β β the number of test cases in the test.
The next lines contain a description of $m$ test cases.
The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$)Β β the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.
The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard.
The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print the answers to $m$ test cases. For each set, print a single integerΒ β maximal number of points that he can receive, before leaving the exam.
-----Example-----
Input
10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
Output
3
2
1
0
1
4
0
1
2
1
|
import sys
input = sys.stdin.readline
t=int(input())
for testcases in range(t):
n,T,a,b=list(map(int,input().split()))
A=list(map(int,input().split()))
L=list(map(int,input().split()))
LCAN=[T]
EASY=[]
HARD=[]
for i in range(n):
if A[i]==0:
EASY.append(L[i])
else:
HARD.append(L[i])
if L[i]>1:
LCAN.append(L[i]-1)
LCAN=sorted(set(LCAN))
EASY.sort()
HARD.sort()
#print(LCAN,a,b)
#print(EASY)
#print(HARD)
#print()
eind=0
hind=0
LENE=len(EASY)
LENH=len(HARD)
needtime=0
ANS=0
for time in LCAN:
while eind<LENE and EASY[eind]<=time:
needtime+=a
eind+=1
while hind<LENH and HARD[hind]<=time:
needtime+=b
hind+=1
if time<needtime:
continue
else:
rest=time-needtime
score=eind+hind
if (LENE-eind)*a>=rest:
score+=rest//a
else:
score=LENE+hind
rest-=(LENE-eind)*a
score+=min(LENH-hind,rest//b)
ANS=max(ANS,score)
print(ANS)
|
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.
The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive.
All problems are divided into two types: easy problems β Petya takes exactly $a$ minutes to solve any easy problem; hard problems β Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$.
For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved.
For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems.
Thus, the answer to this test is $2$.
Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
-----Input-----
The first line contains the integer $m$ ($1 \le m \le 10^4$)Β β the number of test cases in the test.
The next lines contain a description of $m$ test cases.
The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$)Β β the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.
The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard.
The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print the answers to $m$ test cases. For each set, print a single integerΒ β maximal number of points that he can receive, before leaving the exam.
-----Example-----
Input
10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
Output
3
2
1
0
1
4
0
1
2
1
|
m = int(input())
for ii in range(m):
n, T, a, b = list(map(int, input().split()))
score = [a,b]
d = list(map(int, input().split()))
t = list(map(int, input().split()))
easy = 0
for d1 in d:
if d1 == 0:
easy += 1
diff = list(zip(t,d))
diff = sorted(diff) # from least to greatest
cnt = 0
cur = 0
ans = 0
for i in range(n):
t,d = diff[i]
# print('----',i, cur, cnt)
if cur < t and cur <= T:
# can leave
ans = max(cnt, ans)
# try easy problems as much as possible
tmp = (t - 1 - cur) // a
tmp = min(tmp, easy)
ans = max(ans, cnt + tmp)
# force this one
cnt += 1
cur += score[d]
if d==0:
easy -= 1
if cur <= T:
ans = max(cnt, ans)
print(ans)
|
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.
The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive.
All problems are divided into two types: easy problems β Petya takes exactly $a$ minutes to solve any easy problem; hard problems β Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$.
For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved.
For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems.
Thus, the answer to this test is $2$.
Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
-----Input-----
The first line contains the integer $m$ ($1 \le m \le 10^4$)Β β the number of test cases in the test.
The next lines contain a description of $m$ test cases.
The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$)Β β the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.
The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard.
The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print the answers to $m$ test cases. For each set, print a single integerΒ β maximal number of points that he can receive, before leaving the exam.
-----Example-----
Input
10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
Output
3
2
1
0
1
4
0
1
2
1
|
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return list(map(int,minp().split()))
def solve():
n, T, a, b = mints()
h = list(mints())
c = [0, 0]
for i in h:
c[i] += 1
i = 0
t = [None]*n
for j in mints():
t[i] = (j, i)
i += 1
t.sort()
tt = 0
tmust = 0
cmust = 0
r = 0
for ii in range(len(t)):
tn, i = t[ii]
if tt < tn - 1:
tt = tn - 1
left = tt - tmust
if left >= 0:
ac = min(left//a, c[0])
bc = min((left - ac*a)//b, c[1])
#print(tt, tmust, left, cmust, ac, bc)
r = max(r, cmust + ac + bc)
if h[i]:
tmust += b
c[1] -= 1
else:
tmust += a
c[0] -= 1
#print("tmust", tmust)
cmust += 1
if tt < T:
tt = T
left = tt - tmust
if left >= 0:
ac = min(left//a, c[0])
bc = min((left - ac*a)//b, c[1])
r = max(r, cmust + ac + bc)
return r
for i in range(mint()):
print(solve())
|
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.
The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive.
All problems are divided into two types: easy problems β Petya takes exactly $a$ minutes to solve any easy problem; hard problems β Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$.
For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved.
For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems.
Thus, the answer to this test is $2$.
Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
-----Input-----
The first line contains the integer $m$ ($1 \le m \le 10^4$)Β β the number of test cases in the test.
The next lines contain a description of $m$ test cases.
The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$)Β β the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.
The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard.
The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print the answers to $m$ test cases. For each set, print a single integerΒ β maximal number of points that he can receive, before leaving the exam.
-----Example-----
Input
10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
Output
3
2
1
0
1
4
0
1
2
1
|
m = int(input())
for i in range(m):
n, T, a, b = list(map(int, input().split()))
is_hard = list(map(int, input().split()))
total_hard = sum(is_hard)
total_easy = n - total_hard
time_mandatory = list(map(int, input().split()))
mandatory_times = sorted([(time_mandatory[i], i)
for i in range(len(time_mandatory))])
mandatory_times.append((T, -1))
maximal_points = 0
min_easy = 0
min_hard = 0
for (i, (time, problem_no)) in enumerate(mandatory_times):
bad = False
if i != len(mandatory_times) - 1 and mandatory_times[i + 1][0] == time:
bad = True
remaining_easy = total_easy - min_easy
remaining_hard = total_hard - min_hard
remaining_time = time - 1 - min_easy * a - min_hard * b
if remaining_time >= 0:
if remaining_time >= a * remaining_easy:
maximal_points = max(maximal_points,
min_easy + min_hard + remaining_easy +
min((remaining_time - a * remaining_easy) // b,
remaining_hard))
else:
maximal_points = max(maximal_points,
min_easy + min_hard + remaining_time // a)
if problem_no == -1:
min_easy = min_easy
elif is_hard[problem_no] == 1:
min_hard += 1
else:
min_easy += 1
if bad:
continue
remaining_easy = total_easy - min_easy
remaining_hard = total_hard - min_hard
remaining_time = time - min_easy * a - min_hard * b
if remaining_time >= 0:
if remaining_time >= a * remaining_easy:
maximal_points = max(maximal_points,
min_easy + min_hard + remaining_easy +
min((remaining_time - a * remaining_easy) // b,
remaining_hard))
else:
maximal_points = max(maximal_points,
min_easy + min_hard + remaining_time // a)
print(maximal_points)
|
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.
The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive.
All problems are divided into two types: easy problems β Petya takes exactly $a$ minutes to solve any easy problem; hard problems β Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem.
Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$.
For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved.
For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems.
Thus, the answer to this test is $2$.
Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
-----Input-----
The first line contains the integer $m$ ($1 \le m \le 10^4$)Β β the number of test cases in the test.
The next lines contain a description of $m$ test cases.
The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$)Β β the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.
The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard.
The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$.
-----Output-----
Print the answers to $m$ test cases. For each set, print a single integerΒ β maximal number of points that he can receive, before leaving the exam.
-----Example-----
Input
10
3 5 1 3
0 0 1
2 1 4
2 5 2 3
1 0
3 2
1 20 2 4
0
16
6 20 2 5
1 1 0 1 0 0
0 8 2 9 11 6
4 16 3 6
1 0 1 1
8 3 5 6
6 20 3 6
0 1 0 0 1 0
20 11 3 20 16 17
7 17 1 6
1 1 0 1 0 0 0
1 7 0 11 10 15 10
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12
5 17 2 5
1 1 1 1 0
17 11 10 6 4
1 1 1 2
0
1
Output
3
2
1
0
1
4
0
1
2
1
|
t = int(input())
for _ in range(t):
n, T, a, b = list(map(int, input().split(' ')))
task_t = list(map(int, input().split(' ')))
ness = list(map(int, input().split(' ')))
perm = sorted(list(range(n)), key=lambda i: ness[i])
score = 0
tot_hard = sum(task_t)
tot_easy = n - tot_hard
must_easy = 0
must_hard = 0
for i in range(n):
if i > 0 and ness[perm[i]] == ness[perm[i - 1]]:
if task_t[perm[i]] == 0:
must_easy += 1
else:
must_hard += 1
continue
tm = ness[perm[i]] - 1
req_time = must_easy * a + must_hard * b
if req_time > tm:
if task_t[perm[i]] == 0:
must_easy += 1
else:
must_hard += 1
continue
extra_time = tm - req_time
extra_easy = min(extra_time // a, tot_easy - must_easy)
extra_time -= a * extra_easy
extra_hard = min(extra_time // b, tot_hard - must_hard)
#print(tm, extra_easy, extra_hard, must_easy, must_hard)
score = max(score, extra_easy + extra_hard + must_easy + must_hard)
if task_t[perm[i]] == 0:
must_easy += 1
else:
must_hard += 1
if tot_easy * a + tot_hard * b <= T:
score = n
print(score)
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def solve():
s,c = input().split()
# i,jγ§jγθ€ζ°γγγ¨γ
n = len(s)
for i in range(n-1):
prev = s[i]
pos = i
for j in range(i+1, n):
if s[j]<prev:
prev = s[j]
pos = j
elif s[j] == prev:
pos = j
if prev == s[i]:
continue
t = list(s)
t[i], t[pos] = prev, s[i]
s = "".join(t)
break
if s<c:
print(s)
else:
print("---")
t = int(input())
for i in range(t):
solve()
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(list(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())
def li2():return [i for i in input().rstrip('\n').split(' ')]
n = val()
for _ in range(n):
s1,s2 = li2()
fin = sorted(s1)
if fin[0]>s2[0]:
print('---')
continue
for i in range(len(s1)):
if s1[i] != fin[i]:
for j in range(len(s1)-1,-1,-1):
if s1[j] == fin[i]:
s1 = list(s1)
s1[j] = s1[i]
s1[i] = fin[i]
s1 = ''.join(s1)
break
break
print('---' if s1 >= s2 else s1)
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
def main():
TT = int(input())
for _ in range(TT):
w, t = input().strip().split(' ')
w = list(w)
sf = [len(w) - 1 for _ in range(len(w))]
for i in range(len(w) - 2, -1, -1):
if w[i] < w[sf[i+1]]:
sf[i] = i
else:
sf[i] = sf[i + 1]
for i in range(len(w)):
if sf[i] != i and w[sf[i]] != w[i]:
w[i], w[sf[i]] = w[sf[i]], w[i]
break
w = ''.join(w)
if w < t:
print(w)
else:
print('---')
def __starting_point():
main()
__starting_point()
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
q=int(input())
for i in range(q):
ok=[x for x in input().split()]
r=ok[0]
s=ok[1]
w = [(ord(r[i])) for i in range(len(r))]
w.sort()
w=[chr(w[i]) for i in range(len(r))]
first=True
at=-1
for j in range(len(r)):
if w[j]!=r[j]:
first=False
at=j
break
if first==False:
t=r[::-1].find(w[at])
r=r[:at]+w[at]+r[at+1:len(r)-1-t]+r[at]+r[len(r)-t:]
if r<s:
print(r)
else:
print("---")
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
def gets(a):
i = 0
a = list(a)
b = [0]*100
for j in a:
b[ord(j)-ord('A')]+=1
r = -1
t = -1
while(b[i]==0 and i<26):
i+=1
for k in range(0,len(a)):
if r==-1 and (ord(a[k]) - ord('A'))== i:
b[i]-=1
while(b[i]==0 and i<26):
i+=1
elif r==-1:
t = k
r = 0
elif ord(a[k]) - ord('A') == i:
r = k
if r!=-1 and t!=-1:
a[t] , a[r] = a[r] , a[t]
return ''.join(a)
for _ in range(int(input())):
a,b = input().split()
a = gets(a)
if a<b:
print(a)
else:
print("---")
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
for _ in range(int(input())):
a, b = input().split()
a = list(a)
for i in range(len(a)-1):
j = min((i for i in range(i+1, len(a))), key=lambda x: (a[x], -x))
if a[i] > a[j]:
a[i], a[j] = a[j], a[i]
break
a = ''.join(a)
if a < b:
print(a)
else:
print('---')
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
import math
def better(a, b):
for i in range(min(len(a), len(b))):
if ord(a[i]) < ord(b[i]):
return True
elif ord(a[i]) > ord(b[i]):
return False
return len(a) < len(b)
def optimize(a):
occ = [0] * 26
for i in range(len(a)):
occ[ord(a[i]) - ord('A')] += 1
p1 = -1
p2 = -1
t = 0
for i in range(len(a)):
if p1 < 0:
occ[ord(a[i]) - ord('A')] -= 1
for j in range(ord(a[i]) - ord('A')):
if occ[j] > 0:
p1 = i
t = j
break
else:
if ord(a[i]) - ord('A') == t:
p2 = i
if p1 >= 0 and p2 >= 0:
return a[:p1] + a[p2] + a[p1+1:p2] + a[p1] + a[p2+1:]
return a
def main():
t = int(input())
for i in range(t):
line = str(input())
p = line.split()
mine = p[0]
yours = p[1]
new = optimize(mine)
if better(new, yours):
print(new)
else:
print('---')
def __starting_point():
main()
__starting_point()
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
for _ in range(int(input())):
a,c=input().split()
a=list(a)
b=sorted(a)
if a!=b:
for i,x in enumerate(b):
if a[i]!=x:
tmp=a[i]
a[i]=x
break
for i in range(len(a)-1,-1,-1):
if a[i]==x:
a[i]=tmp
break
a=''.join(a)
if a<c:
print(a)
else:
print('---')
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
import sys
input = sys.stdin.readline
Q = int(input())
Query = [list(map(str, input().rstrip().split())) for _ in range(Q)]
for S, T in Query:
L = len(S)
update = False
A = list(S)
for i in range(L-1):
tmp = S[i]
for j in range(i+1, L):
if update and tmp == S[j]:
ind = j
if tmp > S[j]:
tmp = S[j]
update = True
ind = j
if update:
A[ind] = S[i]
A[i] = S[ind]
break
A_str = "".join(A)
if A_str < T:
print(A_str)
else:
print("---")
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
from string import ascii_uppercase
a = ascii_uppercase
N = int(input())
for i in range(N):
me, comp = input().split(' ')
# Want to maximize the lexicographic swap
best = ''.join(sorted(me))
# print(best)
mismatch = -1
for index, pair in enumerate(zip(best, me)):
i, j = pair
if i != j:
mismatch = index
break
if mismatch != -1:
# Want to swap mismatch (index) with last occurence after mismatch
swaploc = len(me) - me[mismatch+1:][::-1].find(best[mismatch]) - 1
swap1 = me[:mismatch] + me[swaploc] + me[mismatch+1:swaploc] + me[mismatch] + me[swaploc+1:]
else:
swap1 = me
if swap1 < comp:
print(swap1)
else:
print('---')
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
n = int(input())
for i in range(n):
s, t = list(map(str, input().split()))
if len(s) == 1:
if s < t:
print (s)
else:
print ("---")
continue
mas = [['ZZ', -1]]
for j in range(len(s) - 1, -1, -1):
if mas[-1][0] > s[j]:
mas.append([s[j], j])
else:
mas.append(mas[-1])
mas = mas[::-1]
#print (*mas)
flag = True
for j in range(len(s)):
#print (j)
if s[j] > mas[j][0]:
s = s[:j] + mas[j][0] + s[j + 1:mas[j][1]] + s[j] + s[mas[j][1] + 1:]
if (s >= t):
print ("---")
else:
print (s)
flag = False
break
if flag:
if s < t:
print (s)
else:
print ("---")
|
Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.
After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!
To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.
Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!
Given the string $s$ representing Jeff's product name and the string $c$ representing his competitor's product name, find a way to swap at most one pair of characters in $s$ (that is, find two distinct indices $i$ and $j$ and swap $s_i$ and $s_j$) such that the resulting new name becomes strictly lexicographically smaller than $c$, or determine that it is impossible.
Note: String $a$ is strictly lexicographically smaller than string $b$ if and only if one of the following holds: $a$ is a proper prefix of $b$, that is, $a$ is a prefix of $b$ such that $a \neq b$; There exists an integer $1 \le i \le \min{(|a|, |b|)}$ such that $a_i < b_i$ and $a_j = b_j$ for $1 \le j < i$.
-----Input-----
The first line of input contains a single integer $t$ ($1 \le t \le 1500$) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing two space-separated strings $s$ and $c$ ($2 \le |s| \le 5000, 1 \le |c| \le 5000$). The strings $s$ and $c$ consists of uppercase English letters.
It is guaranteed that the sum of $|s|$ in the input is at most $5000$ and the sum of the $|c|$ in the input is at most $5000$.
-----Output-----
For each test case, output a single line containing a single string, which is either the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $c$. In case there are many possible such strings, you can output any of them; three dashes (the string "---" without quotes) if it is impossible.
-----Example-----
Input
3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA
Output
AMAZON
---
APPLE
-----Note-----
In the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".
It is impossible to improve the product's name in the second test case and satisfy all conditions.
In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".
|
import sys
input = sys.stdin.readline
def getInt(): return int(input())
def getVars(): return list(map(int, input().split()))
def getList(): return list(map(int, input().split()))
def getStr(): return input().strip()
## -------------------------------
n = getInt()
for i in range(n):
s, c = getStr().split()
p = False
for i in range(len(s)-1):
ch = i
for j in range(i+1,len(s)):
if s[j] <= s[ch]:
ch = j
if s[ch] < s[i]:
s = s[:i] + s[ch] + s[i+1:ch] + s[i] + s[ch+1:]
break
if s < c: print(s)
else: print('---')
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
q = int(input())
for _ in range(q):
c, m, x = list(map(int, input().split()))
print(min([c, m, (c + m + x) // 3]))
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
q = int(input())
for i in range(q):
c, m, x = map(int, input().split())
ans = min(c, m, x)
c -= ans
m -= ans
x -= ans
ans += min(c, m, (c + m) // 3)
print(ans)
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
for i in range(int(input())):
c,m,x=map(int,input().split())
print(min((c+m+x)//3,c,m))
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
t = int(input())
for i in range(t):
c,m,x = map(int,input().split())
ans1 = min(c,m)
ans2 = (c+m+x)//3
print(min(ans1,ans2))
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
for _ in range(int(input())):
c, m, x = map(int, input().split())
print(min((c + m + x) // 3, min(c, m)))
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
q = int(input())
info=[[int(i) for i in input().split()] for k in range(q)]
for inf in info:
c,m,x = inf
print(min([c,m,int((c+m+x)/3)]))
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
Q = int(input())
for q in range(Q):
c, m, x = tuple(map(int, input().split()))
ans = min(c, m)
#m = ans
c -= ans
m -= ans
if c + m + x >= ans:
print(ans)
continue
delta = (ans - (c + m + x)) * 2
ans = c + m + x
ans += min(delta // 3, delta // 2)
print(ans)
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
t=int(input())
while t:
t=t-1
c,m,x=[int(x) for x in input().split(" ")]
y=min(c,m)
c=c-y
m=m-y
#print("y",y)
if y<=c+m+x:
print(y)
else:
print((c+m+x+y*2)//3)
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
n = int(input())
while n:
a,b,c=map(int,input().split())
if a<b:
a,b=b,a
print(min(b,(a+b+c)//3))
n-=1
|
You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.
So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.
You are a coach at a very large university and you know that $c$ of your students are coders, $m$ are mathematicians and $x$ have no specialization.
What is the maximum number of full perfect teams you can distribute them into?
Note that some students can be left without a team and each student can be a part of no more than one team.
You are also asked to answer $q$ independent queries.
-----Input-----
The first line contains a single integer $q$ ($1 \le q \le 10^4$) β the number of queries.
Each of the next $q$ lines contains three integers $c$, $m$ and $x$ ($0 \le c, m, x \le 10^8$) β the number of coders, mathematicians and students without any specialization in the university, respectively.
Note that the no student is both coder and mathematician at the same time.
-----Output-----
Print $q$ integers β the $i$-th of them should be the answer to the $i$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.
-----Example-----
Input
6
1 1 1
3 6 0
0 0 0
0 1 1
10 1 10
4 4 1
Output
1
3
0
0
1
3
-----Note-----
In the first example here are how teams are formed: the only team of 1 coder, 1 mathematician and 1 without specialization; all three teams consist of 1 coder and 2 mathematicians; no teams can be formed; no teams can be formed; one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team; one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.
|
n=int(input())
while n:
n-=1
l=list(map(int,input().split()))
if sum(l)//3 < min(l[0],l[1]):
print(sum(l)//3)
else:
print(min(l[0],l[1]))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.