description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
This problem is actually a subproblem of problem G from the same contest.
There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
You have to answer $q$ independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Each query is represented by two lines.
The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of candies.
The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$.
-----Output-----
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
-----Example-----
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
-----Note-----
In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies.
Note that this is not the only possible solution — taking two candies of type $4$ and one candy of type $6$ is also valid.
|
for _ in range(int(input())):
n = int(input())
lis = [0] * (n + 2)
li = [*map(int, input().split())]
for i in li:
lis[i] += 1
lis.sort(reverse=True)
k = lis[0]
s = k
i = 1
while k > 0:
k = min(lis[i], k - 1)
s += k
i += 1
print(s)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This problem is actually a subproblem of problem G from the same contest.
There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
You have to answer $q$ independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Each query is represented by two lines.
The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of candies.
The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$.
-----Output-----
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
-----Example-----
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
-----Note-----
In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies.
Note that this is not the only possible solution — taking two candies of type $4$ and one candy of type $6$ is also valid.
|
import sys
t = int(input())
for _ in range(t):
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
d = {}
for i in a:
d[i] = d.get(i, 0) + 1
b = sorted(list(d.values()))
ans = b[-1]
ans1 = b[-1]
for i in range(len(b) - 2, -1, -1):
ans1 = min(ans1 - 1, b[i])
if ans1 > 0:
ans += ans1
else:
break
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
This problem is actually a subproblem of problem G from the same contest.
There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
You have to answer $q$ independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Each query is represented by two lines.
The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of candies.
The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$.
-----Output-----
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
-----Example-----
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
-----Note-----
In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies.
Note that this is not the only possible solution — taking two candies of type $4$ and one candy of type $6$ is also valid.
|
for i in range(int(input())):
mmm = {}
n = int(input())
for a in map(int, input().split()):
if a not in mmm:
mmm[a] = 1
else:
mmm[a] += 1
s = 0
items = sorted(mmm.items(), key=lambda x: -x[1])
_max = 99999999999999999999999
for i in items:
if _max == 1:
break
_max = min(i[1], _max - 1)
s += _max
print(s)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
This problem is actually a subproblem of problem G from the same contest.
There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$).
You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad).
It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.
Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.
You have to answer $q$ independent queries.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
-----Input-----
The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries. Each query is represented by two lines.
The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of candies.
The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box.
It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$.
-----Output-----
For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.
-----Example-----
Input
3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7
Output
3
10
9
-----Note-----
In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies.
Note that this is not the only possible solution — taking two candies of type $4$ and one candy of type $6$ is also valid.
|
q = int(input())
for _ in range(q):
freq = {}
n = int(input())
arr = [int(i) for i in input().strip().split(" ")]
for i in arr:
if freq.get(i) == None:
freq[i] = 1
else:
freq[i] += 1
k = sorted(list(freq.values()), reverse=True)
m = 90000000000000
ans = 0
for i in range(len(k)):
if m <= 0:
break
if k[i] >= m:
m = m - 1
ans = ans + m
else:
m = k[i]
ans = ans + m
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for T in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
P = 0
RQ = A[0] * N - sum(A)
AV = 0
MK = 0
if RQ > 0:
for i in range(1, N):
RQ -= (N - i) * (A[i - 1] - A[i])
AV += A[i - 1]
MK += 1
if AV >= RQ:
break
print(MK)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if len(set(a)) == 1:
print("0")
else:
add = sum(a)
a.sort()
big = 0
ans = n
while True:
big += a.pop()
ans -= 1
if add - big >= a[-1] * ans - big:
print(n - ans)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE NUMBER VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
tc = int(input())
for TC in range(tc):
n = int(input())
l = list(map(int, input().split()))
l.sort()
dup = [0]
for i in range(1, n):
dup.append(dup[i - 1] + (l[i] - l[i - 1]) * i)
prev = 0
ans = 0
if dup[-1] == 0:
ans = 0
else:
prev = l[-1]
ans += 1
for i in range(n - 2, -1, -1):
if dup[i] <= prev:
break
ans += 1
prev += l[i]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for i in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ps = []
ss = []
s = 0
for i in a:
s += i
ps.append(s)
s1 = 0
for i in a[::-1]:
s1 += i
ss.append(s1)
ss = ss[::-1]
r = set(a)
if len(r) == 1:
print(0)
else:
for i in range(n - 2, -1, -1):
if ps[i] < (i + 1) * a[i]:
if ps[i] + ss[i + 1] >= (i + 1) * a[i]:
print(n - 1 - i)
break
else:
print(n - 1 - i)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
initial_pop = int(input())
coins = [int(num) for num in input().split()]
coins.sort()
coins_sum = sum(coins)
for current_pop in range(initial_pop, 0, -1):
coins_current = coins[current_pop - 1]
if coins_current == coins[0] or coins_current <= coins_sum // current_pop:
print(initial_pop - current_pop)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
l = sorted(list(map(int, input().split())), reverse=True)
if l[0] == l[n - 1]:
print(0)
else:
b, a = sum(l), 0
for i in range(n):
a += l[i]
b -= l[i]
if l[i + 1] * (n - 1 - i) <= b + a:
print(i + 1)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
m = int(input())
r = [0] * m
for i in range(m):
n = int(input())
a = list(map(int, input().split()))
a.sort()
b = [0] * n
for j in range(1, n):
b[j] = (a[j] - a[j - 1]) * j + b[j - 1]
t = 0
j = n - 1
while t < b[j]:
t = t + a[j]
j = j - 1
r[i] = n - 1 - j
print(r[i])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
rs = lambda: input()
def solve():
n = ri()
a = rl()
s = sum(a)
a.sort(reverse=True)
ans = 0
for i in range(n):
if s >= a[i] * (n - i):
ans = i
break
return ans
def main():
for _ in range(ri()):
print(solve())
main()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
def check(l, s, n):
if l[n - 1] * n == s:
return True
return False
for _ in range(int(input())):
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
s = sum(l)
ss = 0
for i in range(n):
if check(l, s, n - i):
print(i)
break
if ss >= l[n - i - 1] * (n - i) - s:
print(i)
break
ss += l[n - i - 1]
s -= l[n - i - 1]
|
FUNC_DEF IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
import sys
input = sys.stdin.readline
M = int(1000000000.0) + 7
def solve():
n = int(input())
a = sorted(map(int, input().split()))
pre = [0] * n
for i in range(1, n):
pre[i] = pre[i - 1] + a[i - 1]
sm = 0
for i in range(n - 1, -1, -1):
if i * a[i] <= sm + pre[i]:
return n - 1 - i
sm += a[i]
return n
for _ in range(int(input())):
print(solve())
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
ans = []
for _ in range(int(input())):
N = int(input())
A = sorted([int(x) for x in input().split()], reverse=True)
sm = sum(A)
for i in range(N):
if A[i] * (N - i) <= sm:
break
ans.append(i)
print(*ans, sep="\n")
|
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
def solve():
n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[0] == a[n - 1]:
return 0
presum = [a[0]]
for i in range(1, n):
presum.append(a[i] + presum[i - 1])
c = 0
dons = 0
while True:
c += 1
dons += a.pop()
presum.pop()
if dons >= a[-1] * len(presum) - presum[-1]:
return c
for _ in range(int(input())):
print(solve())
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().strip().split()]
a.sort()
if a[0] == a[-1]:
print(0)
else:
b = []
b.append(0)
for i in range(1, n - 1):
b.append((a[i] - a[i - 1]) * i + b[i - 1])
sum = a[n - 1]
for i in range(n - 2, -1, -1):
if sum >= b[i]:
print(n - 1 - i)
break
sum = sum + a[i]
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
prefix = [a[0]]
for index in range(1, n):
prefix.append(a[index] + prefix[-1])
ans = 0
added = 0
for index in range(n - 1, 0, -1):
val = a[index]
total = prefix[index - 1]
if index * val - added <= total:
break
added += val
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
a = int(input())
for i in range(a):
N = int(input())
A = list(map(int, input().split()))
A.sort()
q = sum(A)
x = 1
p = 0
if A.count(A[0]) == N:
x = 0
else:
for j in range(N - 1, 0, -1):
p += A[j]
if p >= A[j - 1] * j - (q - p):
break
else:
x += 1
print(x)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for test in range(t):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
A.sort(reverse=True)
for i in range(N):
if total >= A[i] * (N - i):
print(i)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
n = int(input())
a = sorted(list(map(int, input().split())))
sum_, b, bsum = 0, [0] * n, [0] * n
bsum[-1] = a[-1]
for i in range(n):
sum_ += a[i]
b[i] = (i + 1) * a[i] - sum_
for i in range(n - 2, -1, -1):
bsum[i] = bsum[i + 1] + a[i]
count, sum_ = 0, 0
for i in range(n - 1, -1, -1):
if sum_ >= b[i]:
print(count)
break
count += 1
sum_ += a[i]
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)[::-1]
if 1 == len(set(l)):
print(0)
else:
mid = n // 2
le = 0
ri = n
while mid != le and mid != ri:
if sum(l[:mid]) >= (n - mid) * l[mid] - sum(l[mid:n]):
ri = mid
mid = (mid + le) // 2
else:
le = mid
mid = (mid + ri) // 2
if sum(l[:mid]) >= (n - mid) * l[mid] - sum(l[mid:n]):
print(mid)
else:
print(mid + 1)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for i in range(int(input())):
n = int(input())
k = list(map(int, input().split()))
k1 = list(k)
k1.sort(reverse=True)
sk = 0
for j in k1:
sk += j
sk2 = sk
if k1[0] == k1[-1]:
print(0)
continue
b = k1[0]
for j in range(0, n - 1):
b += k1[j + 1]
if b >= (n - j - 1) * k1[j + 1] - (sk - b):
print(j + 1)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
lst = sorted(list(map(int, input().split())))
total = 0
for l in lst:
total += l
ans = 0
for c in range(n):
if total / (n - c) >= lst[n - c - 1]:
ans = c
break
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
n = int(input())
lst = list(map(int, input().split()))
lst.sort(reverse=True)
reqd, ans, avail = 0, 0, 0
for i in range(n):
reqd += lst[0] - lst[i]
if reqd == 0:
ans = 0
else:
for i in range(1, n):
avail += lst[i - 1]
reqd -= (n - i) * (lst[i - 1] - lst[i])
if avail >= reqd:
ans = i
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
T = int(input())
while T > 0:
n = input()
a = [int(x) for x in input().split()]
t = 1
suma = 0
a = sorted(a)
for i in a:
suma += i
suma -= a[-1]
if a[0] == a[-1]:
print(0)
else:
while (len(a) - 1) * a[-2] - suma > a[-1]:
suma -= a[-2]
sa = a.pop(-1)
a[-1] += sa
t += 1
print(t)
T -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for i in range(t):
l = int(input())
arr = list(map(int, input().split()))
if l == 1:
print(0)
else:
arr.sort()
prev = [(0) for i in range(l)]
prev[l - 1] = 0
for i in range(l - 2, -1, -1):
prev[i] = arr[i + 1] + prev[i + 1]
total = 0
for i in range(1, l):
if arr[i] != arr[i - 1]:
total += i * (arr[i] - arr[i - 1])
if total >= prev[i]:
break
if total == 0:
print(0)
elif total == prev[i]:
print(l - i - 1)
else:
print(l - i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
while t:
t -= 1
n = int(input())
if n == 1 or n == 0:
coins = input()
print(0)
continue
coins = [int(i) for i in input().split()]
coins.sort(reverse=True)
if coins[0] == coins[n - 1]:
print(0)
continue
if n == 2:
print(1)
continue
if n == 3:
print(1)
continue
summ = sum(coins)
summ -= coins[0]
req = (n - 1) * coins[1] - summ
monk_sum = coins[0]
count = 1
broke = 0
i = 0
for i in range(1, n - 1):
if req <= monk_sum:
break
else:
count += 1
monk_sum += coins[i]
summ -= coins[i]
req = (n - i - 1) * coins[i + 1] - summ
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
c = False
count = 0
k = 0
a.sort()
if a[0] == a[-1]:
print(0)
else:
b = [0] * (n + 1)
for i in range(0, n):
b[i + 1] = b[i] + a[i]
while c == False:
k += a[-1]
a.pop()
b.pop()
count += 1
if k >= (n - count) * a[-1] - b[-1]:
c = True
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
rs = lambda: input()
def solve():
N = ri()
A = rl()
ans = N - 1
s = sum(A)
A.sort()
if A[0] == A[N - 1]:
return 0
cur = 0
for i in range(N):
cur += A[i]
x = s - cur
if x >= A[i] * (i + 1) - cur:
ans = min(ans, N - i - 1)
return ans
def main():
for _ in range(ri()):
print(solve())
main()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for i in range(t):
l = int(input())
arr = list(map(int, input().split()))
if l == 1:
print(0)
else:
arr.sort()
s = sum(arr)
target = 0
for i in range(l - 1, -1, -1):
temp = (i + 1) * arr[i] - s
if target >= temp:
break
target += arr[i]
s -= arr[i]
print(l - i - 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for i in range(t):
n = int(input())
l = list(map(int, input().split()))
s = sum(l)
l.sort(reverse=True)
if l[0] == l[-1]:
print("0")
else:
for i in range(0, n - 1):
if l[i + 1] * (n - 1 - i) - s <= 0:
print(i + 1)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
def main():
T = int(input())
for i in range(T):
ans = 0
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
need = 0
avail = a[0]
if a[0] == a[-1]:
print("0")
continue
if a[0] != a[-1] and n == 2:
print("1")
continue
for i in range(1, n):
need += a[1] - a[i]
ans += 1
if need <= avail:
print("1")
continue
for i in range(1, n - 1):
ans += 1
avail += a[i]
need += a[i + 1] * (n - i - 1) - a[i] * (n - i) + a[i]
if avail >= need:
break
print(ans)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
while t > 0:
n = int(input())
a = [int(i) for i in input().split()]
if len(set(a)) == 1:
print("0")
else:
add = sum(a)
a.sort()
ans = n
while True:
a.pop()
ans -= 1
if add >= a[-1] * ans:
print(n - ans)
break
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for M in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
s = sum(arr)
if max(arr) == min(arr):
print(0)
continue
for i in range(n - 1):
if s >= (n - 1 - i) * arr[i + 1]:
print(i + 1)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
N = int(input())
A = [int(x) for x in input().split(" ")]
if A.count(max(A)) == len(A):
print(0)
continue
sm = sum(A)
A.sort()
curr, ans = 0, N
for i in range(N):
curr += A[i]
x = sm - curr
if x >= A[i] * (i + 1) - curr:
ans = min(ans, N - i - 1)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
T = int(input())
for t in range(T):
n = int(input())
arr = [int(_) for _ in input().split()]
arr.sort(reverse=True)
prefix = [arr[0]]
for i in range(1, n):
prefix.append(prefix[i - 1] + arr[i])
suffix = [(0) for i in range(n)]
suffix[-1] = arr[-1]
for i in range(n - 2, -1, -1):
suffix[i] = suffix[i + 1] + arr[i]
reqd = arr[0] * n
avail = prefix[-1]
if avail >= reqd:
print(0)
continue
for i in range(1, n):
reqd = arr[i] * (n - i) - suffix[i]
avail = prefix[i - 1]
if avail >= reqd:
print(i)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
def fun(n, a):
a.sort(reverse=True)
if a[0] == a[n - 1]:
return 0
i = 1
s = sum(a)
x = 0
while i < n:
s -= a[i - 1]
r = a[i] * (n - i) - s
x += a[i - 1]
if x >= r:
return i
i += 1
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
print(fun(n, a))
|
FUNC_DEF EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
if n == 1:
print(0)
continue
narr = arr.copy()
narr.sort(reverse=True)
pref_sum = 0
suf_sum = [(0) for i in range(n)]
cs = 0
for i in range(n - 1, -1, -1):
cs += narr[i]
suf_sum[i] = cs
for i in range(n):
if pref_sum >= narr[i] * (n - i) - suf_sum[i]:
print(i)
break
pref_sum += narr[i]
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
a.sort()
summ = sum(a)
for i in range(n - 1, -1, -1):
if a[i] * (i + 1) <= summ:
print(n - (i + 1))
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
import sys
input = sys.stdin.readline
def solve():
for i in range(int(input())):
n = int(input())
lst = list(map(int, input().split()))
sm = sum(lst)
lst.sort()
if lst[n - 1] == lst[0]:
print(0)
else:
curr = 0
ans = n
for i in range(n):
curr += lst[i]
x = sm - curr
if x >= lst[i] * (i + 1) - curr:
ans = min(ans, n - i - 1)
print(ans)
solve()
|
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
def qsort(inlist):
if inlist == []:
return []
else:
pivot = inlist[0]
lesser = qsort([x for x in inlist[1:] if x < pivot])
greater = qsort([x for x in inlist[1:] if x >= pivot])
return lesser + [pivot] + greater
for t in range(int(input())):
n = int(input())
s = [int(x) for x in input().split()]
a = s[0]
b = s.count(a)
c = 1
if b == n:
print(0)
elif b == n - 1:
print(1)
else:
m = 0
s.sort(reverse=True)
i = 0
p = sum(s)
while 1:
m += s[0]
p -= s[0]
s.remove(s[0])
n -= 1
i += 1
if m >= n * s[0] - p:
break
else:
c += 1
print(c)
t -= 1
|
FUNC_DEF IF VAR LIST RETURN LIST ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
if len(a) == 1:
print(0)
continue
s = sum(a)
c = 0
ans = -1
for i in range(0, n):
c += a[i]
if a[i] * (i + 1) - c <= s - c:
ans = i
print(n - ans - 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
T = int(input())
for i in range(T):
n = int(input())
li = list(map(int, input().split()))
li.sort(reverse=True)
total_sum = sum(li)
b_sum = 0
c_sum = total_sum
flag = 0
for j in range(1, n):
if li[j - 1] != li[j]:
flag = 1
break
if flag == 0:
print(0)
else:
for j in range(1, n):
b_sum += li[j - 1]
c_sum -= li[j - 1]
temp = li[j] * (n - j) - c_sum
if temp <= b_sum:
print(j)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
diff = x = c = 0
for i in range(n):
diff += arr[-1] - arr[i]
for i in range(n - 1, 0, -1):
if x >= diff:
break
diff -= (arr[i] - arr[i - 1]) * i
x += arr[i]
c += 1
print(c)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
a = int(input())
for i in range(a):
c = int(input())
b = list(map(int, input().split()))
b.sort()
b.reverse()
if len(set(b)) == 1:
print("0")
else:
j = sum(b)
k = len(b)
l = 0
for i in range(len(b)):
j -= b[i]
k -= 1
l += b[i]
if b[i + 1] * k - j <= l:
print(len(b) - k)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
if A[0] == A[-1]:
print(0)
continue
total = sum(A)
for i, a in enumerate(A[:-1]):
if A[i + 1] == A[-1]:
print(i + 1)
break
if total / (N - i - 1) >= A[i + 1]:
print(i + 1)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
def checker(arr, k):
t = sum(arr[0:k])
i = k
req = 0
target = arr[i]
while i < len(arr):
req += target - arr[i]
i += 1
if req <= t:
return 1
else:
return 0
def ans(arr):
start = 0
end = len(arr)
temp = sorted(arr, reverse=True)
c = 0
while start <= end:
mid = (start + end) // 2
if checker(temp, mid) == 1:
c = mid
end = mid - 1
else:
start = mid + 1
return c
test_cases = int(input())
while test_cases != 0:
d_ = input()
d = list(map(int, input().split()))
print(ans(d))
test_cases -= 1
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
s = sum(a)
for i in range(n):
if a[i] * (n - i) <= s:
print(i)
break
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
T = int(input())
Coins = list()
Res = list()
for i in range(T):
N = int(input())
Coins = input().split(" ")
A = list()
n = 0
while n < N:
A = list()
A.append(int(Coins[n]))
n = n + 1
Coins = [int(j) for j in Coins]
Coins.sort()
SumOfCoins = sum(Coins)
n = N
res = 0
for k in range(n):
if Coins[k] * (k + 1) <= SumOfCoins:
res = n - k - 1
Res.append(res)
for i in Res:
print(i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
t = int(input())
for _ in range(t):
n = int(input())
arr = sorted(list(map(int, input().split())), reverse=True)
x = sum(arr)
ans = 0
d = 0
for i in range(n):
if arr[i] * (n - i) > x + d:
ans += 1
d += arr[i]
x -= arr[i]
else:
break
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
There is a town with N people and initially, the i^{th} person has A_{i} coins. However, some people of the town decide to become *monks*. If the i^{th} person becomes a monk, then:
He leaves the town thereby reducing the number of people in the town by 1.
He distributes X (0 ≤ X ≤ A_{i}) coins to the remaining people of the town (not necessarily equally). Note that each monk can freely choose his value of X, and different monks may choose different values of X.
He takes the remaining A_{i} - X coins with him.
For example, initially, if A = [1, 3, 4, 5] and 4^{th} person decides to become a monk then he can leave the town and can give 2 coins to the 1^{st} person, 1 coin to the 2^{nd} person, no coins to the 3^{rd} person and take 2 coins along with him while going. Now A becomes [3, 4, 4].
Determine the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the number of people in the town.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} denoting the initial number of coins of everyone in the town.
------ Output Format ------
For each test case, output the minimum number of people who have to become monks, so that in the end, everyone remaining in the town has an equal number of coins.
------ Constraints ------
$1 ≤T ≤10^{5}$
$1 ≤N ≤10^{5}$
$1 ≤A_{i} ≤10^{9}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
6 6 6 6
3
5 1 4
5
2 1 2 1 1
----- Sample Output 1 ------
0
1
2
----- explanation 1 ------
Test case $1$: All the people already have an equal number of coins.
Test case $2$: The $2^{nd}$ person can become a monk and give his $1$ coin to the person with $4$ coins. After this, both the remaining people will have $5$ coins.
Test case $3$: One way of two people becoming monks is as follows:
- The $2^{nd}$ person becomes a monk, and takes his $1$ coin with him
- The $3^{rd}$ person becomes a monk, and gives one coin each to the $4^{th}$ and $5^{th}$ people
Everyone remaining in the town now has $2$ coins.
|
try:
T = int(input())
for k in range(T):
N = int(input())
A = list(map(int, input().split(" ")))
A.sort()
sum = 0
for j in range(N):
sum += A[j]
if A[0] == A[N - 1]:
print(0)
continue
ans = N
C = 0
for i in range(N):
C += A[i]
x = sum - C
if x >= A[i] * (i + 1) - C:
ans = min(ans, N - i - 1)
print(ans)
except:
pass
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class doubly_linked_list:
def __init__(self):
self.head = None
def push(self, NewVal):
NewNode = Node(NewVal)
NewNode.next = self.head
if self.head is not None:
self.head.prev = NewNode
self.head = NewNode
def remove(self, node):
if node.prev:
prev = node.prev
next = node.next
prev.next = node.next
if next:
next.prev = node.prev
else:
self.head = node.next
if self.head:
self.head.prev = None
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
amount_map = {}
for i in a:
amount_map[i] = amount_map.get(i, 0) + 1
data = sorted(list(amount_map.values()) + [1], reverse=True)
result = 0
ijected = doubly_linked_list()
circle = False
result += len(data)
n = len(data)
for i in range(len(data)):
data[i] -= n
n -= 1
data = [x for x in data if x > 0]
while len(data):
max_i = 0
for i in range(len(data)):
if data[i] > data[max_i]:
max_i = i
data[max_i] -= 1
data = [(x - 1) for x in data if x - 1 > 0]
result += 1
print(result)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
def calcula_t():
n_vertices = int(input())
grau = [0] * n_vertices
lista_p = list(map(int, input().split()))
for p in lista_p:
grau[p - 1] += 1
a = [1]
for i in range(n_vertices):
if grau[i] > 0:
a.append(grau[i])
a.sort(reverse=True)
inicio = len(a)
fim = n_vertices
tempo = 0
while inicio < fim:
tempo = (inicio + fim) // 2
soma = 0
for i in range(len(a)):
soma += max(0, a[i] - tempo + i)
if soma + len(a) <= tempo:
fim = tempo
else:
inicio = tempo + 1
print(fim)
pass
n_testes = int(input())
for i in range(n_testes):
calcula_t()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
class Tree:
def __init__(self, n, p):
self.groups = [1]
self.time = 0
self.n = n
p.sort()
s = p[0]
k = 0
for t in p:
if t == s:
k += 1
else:
self.groups.append(k)
s = t
k = 1
self.groups.append(k)
self.groups.sort(reverse=True)
self.k = len(self.groups)
def afterksec(self):
self.time += self.k
for i in range(self.k):
self.groups[i] -= self.k - i
self.groups.sort(reverse=True)
self.groups = list(filter(lambda x: x > 0, self.groups))
def bin_cond(self, t):
s = 0
for b in self.groups:
s += max(b - t, 0)
return s <= t
def bin_search(self):
if self.groups == []:
return 0
l = 0
r = self.n
while l + 1 != r and l != r:
m = (l + r) // 2
if self.bin_cond(m):
r = m
else:
l = m
self.time += r
self.groups = []
def solve(self):
self.afterksec()
self.bin_search()
return self.time
t = int(input())
for i in range(t):
n = int(input())
p = list(map(int, input().split()))
tree = Tree(n, p)
print(tree.solve())
|
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR LIST RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
for _ in [0] * int(input()):
n = int(input())
a = list(map(int, input().split()))
c = [0] * n + [1]
for i in a:
c[i - 1] += 1
c = sorted(c, reverse=True)
ans = sum(i > 0 for i in c)
for i, j in enumerate(c):
if j > 0:
c[i] = i + j - ans
c = sorted([i for i in c if i > 0], reverse=True)
while c:
ans += 1
for i, j in enumerate(c):
if j > 0:
c[i] = j - 1 - (i == 0)
c = sorted([i for i in c if i > 0], reverse=True)
print(ans)
|
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
for i in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
d = {}
for j in l:
d[j] = 0
for j in l:
d[j] += 1
l1 = []
for j in d:
l1.append(d[j])
l1.append(1)
l1.sort()
x = len(l1)
t = 0
for j in range(x):
t += 1
if l1[j] > j + 1:
l1[j] -= j + 1
else:
l1[j] = 0
c = sum(l1)
l1.sort()
ch = False
for j in range(x):
if l1[j] > 0:
l1 = l1[j:]
ch = True
break
if ch:
l2 = [l1[0]]
x = len(l1)
for j in range(1, x):
l2.append(l1[j] - l1[j - 1])
x1 = 0
while c - l2[x1] * (x - x1) >= l1[x1]:
if x1 < x:
c -= l2[x1] * (x - x1)
x1 += 1
else:
break
c -= l2[x1] * (x - x1)
x2 = l1[x1] - c
t += c
r = x2 % (x - x1 + 1)
d = x2 // (x - x1 + 1)
t += (x - x1) * d + r
print(t)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
for _ in range(int(input())):
n = int(input())
arr = [(0) for i in range(n + 1)]
arr[0] = 1
for i in list(map(int, input().split())):
arr[i] += 1
arr.sort()
arr = [i for i in arr if i != 0]
n = len(arr)
ans = n
arr2 = [(arr[i] - i - 1) for i in range(n) if arr[i] - i - 1 > 0]
n = len(arr2)
if n == 0:
print(ans)
continue
arr3 = [(0) for i in range(max(arr2))]
n = len(arr3)
for i in arr2:
arr3[i - 1] += 1
for i in range(n - 2, -1, -1):
arr3[i] += arr3[i + 1]
l = 0
r = n - 1
while l <= r:
if arr3[l] <= 0:
break
ans += 1
arr3[l] = 0
if l == r:
break
arr3[r] -= 1
if arr3[r] == 0:
r -= 1
l += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
for _ in range(int(input())):
n = int(input())
a = [0] + list(map(int, input().split()))
branch = {}
for i in a:
branch[i] = branch.get(i, 0) + 1
res = sorted(branch.values())[::-1]
cnt = 0
j = len(res)
for i in range(len(res)):
res[i] -= j
j -= 1
cnt += 1
res = sorted(res)[::-1]
while res[0] > 0:
for i in range(len(res)):
if i == 0:
res[i] -= 2
cnt += 1
elif a[i] > 0:
res[i] -= 1
res = sorted(res)[::-1]
print(cnt)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
import time
for _ in range(int(input())):
n = int(input())
a = [0] * (n + 1)
a[0] = 1
for i in list(map(int, input().split())):
a[i] += 1
try:
a = [x for x in a if x > 0]
a.sort(reverse=True)
ans = len(a)
for i in range(len(a)):
a[i] -= len(a) - i
a = [x for x in a if x > 0]
while len(a) != 0:
a.sort(reverse=True)
a = [(x - 1) for x in a]
a[0] -= 1
ans += 1
a = [x for x in a if x > 0]
print(ans)
except Exception as e:
print("Was exception: ")
print(e)
exit(0)
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $v$ (different from root) is the previous to $v$ vertex on the shortest path from the root to the vertex $v$. Children of the vertex $v$ are all vertices for which $v$ is the parent.
You are given a rooted tree with $n$ vertices. The vertex $1$ is the root. Initially, all vertices are healthy.
Each second you do two operations, the spreading operation and, after that, the injection operation:
Spreading: for each vertex $v$, if at least one child of $v$ is infected, you can spread the disease by infecting at most one other child of $v$ of your choice.
Injection: you can choose any healthy vertex and infect it.
This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of the vertices in the given tree.
The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the ancestor of the $i$-th vertex in the tree.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.
-----Examples-----
Input
5
7
1 1 1 2 2 4
5
5 5 1 4
2
1
3
3 1
6
1 1 1 1 1
Output
4
4
2
3
4
-----Note-----
The image depicts the tree from the first test case during each second.
A vertex is black if it is not infected. A vertex is blue if it is infected by injection during the previous second. A vertex is green if it is infected by spreading during the previous second. A vertex is red if it is infected earlier than the previous second.
Note that you are able to choose which vertices are infected by spreading and by injections.
|
t = int(input())
rr = ""
for _t in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr)
new_arr = []
nlist = []
prev = None
for a in arr:
if a == prev or not nlist:
nlist.append(a)
else:
new_arr.append(nlist)
nlist = [a]
prev = a
if nlist:
new_arr.append(nlist)
arr = sorted(new_arr, key=lambda x: len(x), reverse=True)
arr.append([0])
def spread(l: list):
nl = []
for i in range(len(l)):
if l[i] != 0:
nl.append(l[i] - 1)
if not nl:
nl = [0]
return nl
inf_count = []
seconds = 0
for i in range(len(arr)):
seconds += 1
inf_count = spread(inf_count)
inf_count.append(len(arr[i]) - 1)
while True:
inf_count = sorted(inf_count, reverse=True)
if inf_count[0] == 0:
break
else:
seconds += 1
inf_count = spread(inf_count)
if inf_count[0] != 0:
inf_count[0] -= 1
rr += str(seconds) + "\n"
print(rr)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER FUNC_DEF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR LIST NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
n = int(input())
arr = []
for i in range(n):
a, b = list(map(str, input().split()))
b = int(b)
arr.append([b, a])
arr.sort()
ans = []
for i in arr:
if i[0] > len(ans):
print(-1)
exit()
ans.insert(i[0], (i[1], n))
n -= 1
for i in ans:
print(i[0], i[1])
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
import sys
n = int(input())
person = []
for i in range(n):
person.append(input().split())
person.sort(key=lambda x: int(x[1]))
high = 10**9
low = 1
cntHigh = 0
for i in range(n):
dif = int(person[i][1]) - cntHigh
for j in range(i - 1, -1, -1):
if dif == 0:
break
if person[j][2] < high:
person[j][2] = high
high -= 1
cntHigh += 1
dif -= 1
if dif > 0:
print("-1")
sys.exit(0)
person[i] = [person[i][0], person[i][1], low]
low += 1
for p in person:
print(p[0], p[2])
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
n = int(input())
arr = [input().split() for _ in range(n)]
arr = [[int(arr[i][1]), arr[i][0]] for i in range(n)]
arr.sort()
res = []
for i in range(n):
res.append(i - arr[i][0])
if res[i] < 0:
print(-1)
exit()
for j in range(i):
if res[j] >= res[i]:
res[j] += 1
for i in range(n):
print(arr[i][1], res[i] + 1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
ans, pr = list(), list()
for _ in range(int(input())):
a, b = input().split()
ans.append([int(b), a])
ans.sort()
v = len(ans)
for x in ans:
if x[0] > len(pr):
print(-1)
break
pr.insert(x[0], (x[1], v))
v -= 1
else:
for _ in pr:
print(_[0], _[1])
|
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
n = int(input())
a = sorted(
[
list(
map(lambda x: x if x[0] >= "a" and x[0] <= "z" else int(x), input().split())
)[::-1]
for i in range(n)
]
)
fl = 1
ans = []
for i in range(len(a)):
v, name = a[i]
if i == 0:
if v > 0:
fl = 0
break
else:
ans.append((name, n))
else:
if v > i:
fl = 0
break
ans.insert(v, (name, n - i))
if not fl:
print(-1)
else:
for i in ans:
print(*i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER STRING VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
def solve(arr, n):
arr.sort()
names = [x[1] for x in arr]
counts = [x[0] for x in arr]
segments = []
curr_count = counts[0]
curr_len = 0
L = 0
for i in range(n):
if counts[i] == curr_count:
curr_len += 1
else:
segments.append((L, i - 1))
L = i
curr_count = counts[i]
curr_len = 1
segments.append((L, n - 1))
h = [(1) for i in range(n)]
tall_dudes = 0
for j in range(len(segments)):
segment = segments[j]
L, R = segment
for i in range(L - 1, -1, -1):
if tall_dudes < counts[L] and h[i] == 1:
h[i] = 10**9 - j
tall_dudes += 1
if tall_dudes < counts[L]:
print(-1)
return
for i in range(n):
print(names[i], h[i])
n = int(input())
a = []
for i in range(n):
x = input().split()
a.append((int(x[1]), x[0]))
solve(a, n)
|
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
n = int(input())
a = []
for _ in range(n):
name, num = input().split()
a.append([name, int(num)])
a = sorted(a, key=lambda x: x[1])
h = []
def solve(a):
h = []
for name, num in a:
if num > len(h):
return False, -1
if num == 0:
if len(h) == 0:
h.append([name, 1])
else:
h.append([name, h[-1][1] + 1])
else:
h = h[:-num] + [[name, h[-num][1]]] + h[-num:]
for h_ in h[-num:]:
h_[1] += 1
h = {name: h_ for name, h_ in h}
for x in a:
x[1] = h[x[0]]
return True, a
flg, ans = solve(a)
if flg == False:
print(ans)
else:
for name, h in ans:
print(name + " " + str(h))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST LIST VAR VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
n = int(input())
def func(s):
name, a = s.split()
return [int(a), name]
arr = [func(input()) for i in range(n)]
arr.sort()
flag = True
for i in range(n):
if i < arr[i][0]:
flag = False
break
if flag:
h = [0] * n
for i in range(n // 2):
hi = h[i] = 20000 * (i + 1 - arr[i][0])
for x in range(i):
if h[x] >= hi:
h[x] += 20000
for i in range(n // 2, n):
hi = h[i] = i + 1 - arr[i][0]
for x in range(n // 2, i):
if h[x] >= hi:
h[x] += 1
hh = sorted([[h[i], i] for i in range(n // 2, n)])
hh = sorted([[hh[i][1], i + 1] for i in range(n - n // 2)])
for i in range(n // 2, n):
h[i] += 20000 * (h[i] - hh[i - n // 2][1])
for i in range(n):
print(arr[i][1], h[i])
else:
print(-1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR RETURN LIST FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
def question3():
people = int(input())
front_more_height = []
for i in range(people):
name, front = input().split()
front = int(front)
front_more_height.append([name, front])
front_more_height.sort(key=lambda x: x[1])
max1 = people**2
n = people
ans = []
for i in range(people):
if front_more_height[i][1] <= i:
ans.insert(front_more_height[i][1], [front_more_height[i][0], n])
else:
return [-1, []]
n -= 1
return [1, ans]
remained_test_cases = 1
while remained_test_cases > 0:
ans = question3()
if ans[0] == -1:
print(-1)
else:
for i in range(len(ans[1])):
print(ans[1][i][0], ans[1][i][1])
remained_test_cases -= 1
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER LIST VAR VAR NUMBER VAR RETURN LIST NUMBER LIST VAR NUMBER RETURN LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER
|
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai — how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi — the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n — the number of people in the queue (1 ≤ n ≤ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 ≤ ai ≤ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
|
n = int(input())
a = []
for i in range(n):
s, c = input().split()
a.append((s, int(c)))
a.sort(key=lambda x: x[1])
ans = []
for i in range(n):
if len(ans) < a[i][1]:
print(-1)
exit(0)
ans.insert(a[i][1], (a[i][0], n - i))
for s, a in ans:
print(s, a)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
c1 = collections.Counter(s1)
c2 = collections.Counter(s2)
t1, t2 = 0, 0
f1, f2 = 1, 1
for i in range(26):
z = chr(97 + i)
t1 += c1[z] - c2[z]
if t1 < 0:
f1 = 0
t2 += c2[z] - c1[z]
if t2 < 0:
f2 = 0
return f1 or f2
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = list(s1)
s2 = list(s2)
s1.sort(reverse=True)
s2.sort(reverse=True)
match = all(x >= y for x, y in zip(s1, s2))
if match:
return True
s1.sort()
s2.sort()
match = all(y >= x for x, y in zip(s1, s2))
return match
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
def check(s1, s2):
c = 0
n = len(s1)
for i in range(n):
if s1[i] >= s2[i]:
c = c + 1
return c == n
return check(s2, s1) or check(s1, s2)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1sorted = "".join(sorted(s1))
s2sorted = "".join(sorted(s2))
res1 = True
res2 = True
for i in range(len(s1)):
if ord(s1sorted[i]) > ord(s2sorted[i]):
res1 = False
for i in range(len(s1)):
if ord(s1sorted[i]) < ord(s2sorted[i]):
res2 = False
return res1 or res2
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
l1 = list(s1)
l2 = list(s2)
l1.sort()
l2.sort()
count = 0
count1 = 0
for i in range(len(l1)):
if l1[i] >= l2[i]:
count += 1
if count == len(l1):
return True
for i in range(len(l2)):
if l2[i] >= l1[i]:
count1 += 1
if count1 == len(l1):
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
t1, t2 = sorted(list(s1)), sorted(list(s2))
for i in range(len(t1)):
if t1[i] <= t2[i]:
continue
else:
break
else:
return True
for i in range(len(t1)):
if t1[i] >= t2[i]:
continue
else:
break
else:
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
l1 = [char for char in s1]
l1.sort()
l2 = [char for char in s2]
l2.sort()
strlen = len(s1)
flag = 0
for i in range(strlen):
if l1[i] >= l2[i]:
continue
else:
flag = 1
break
if flag:
for i in range(strlen):
if l1[i] <= l2[i]:
continue
else:
return False
return True
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
indices = [i for i, (a, b) in enumerate(zip(s1, s2)) if a != b]
comparisons = [(s1[i] > s2[i]) for i in indices]
return all(comparisons) or not any(comparisons)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
l1 = list()
l2 = list()
n = len(s1)
for i in range(0, n):
l1.append(s1[i : i + 1])
l1.sort()
for i in range(0, n):
l2.append(s2[i : i + 1])
l2.sort()
indicator = False
l1_test = list()
for i in range(0, n):
if l1[i] >= l2[i]:
l1_test.append(1)
if len(l1_test) == len(l1):
indicator = True
l2_test = list()
for i in range(0, n):
if l2[i] >= l1[i]:
l2_test.append(1)
if len(l2_test) == len(l2):
indicator = True
return indicator
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1_arr = [ord(x) for x in s1]
s2_arr = [ord(x) for x in s2]
s1_arr.sort()
s2_arr.sort()
s1_counter = 0
s2_counter = 0
for i in range(len(s1_arr)):
if s1_arr[i] > s2_arr[i]:
s1_counter += 1
elif s1_arr[i] == s2_arr[i]:
s1_counter += 1
s2_counter += 1
else:
s2_counter += 1
print((s1_counter, s2_counter))
if s1_counter == len(s1) or s2_counter == len(s1):
return True
else:
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
s1_bigger = False
s2_bigger = False
for i in range(len(s1)):
if s1_bigger and s1[i] < s2[i]:
return False
if s2_bigger and s1[i] > s2[i]:
return False
if s1[i] < s2[i]:
s2_bigger = True
if s1[i] > s2[i]:
s1_bigger = True
return True
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = list(s1)
s2 = list(s2)
s1.sort()
s2.sort()
s1token = True
for i in range(len(s1)):
if s1[i] > s2[i]:
s1token = False
break
if s1token == True:
return True
s2token = True
for i in range(len(s2)):
if s2[i] > s1[i]:
s2token = False
break
if s2token == True:
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1, reverse=True)
s2 = sorted(s2, reverse=True)
resultA = []
resultB = []
for ch1, ch2 in zip(s1, s2):
if ch1 >= ch2:
resultA.append(1)
else:
resultA.append(0)
if ch2 >= ch1:
resultB.append(1)
else:
resultB.append(0)
if all(resultA) == 1 or all(resultB):
return True
else:
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(list(s1))
s2 = sorted(list(s2))
i = 0
while s1[i] <= s2[i]:
if i == len(s1) - 1:
return True
i += 1
i = 0
while s2[i] <= s1[i]:
if i == len(s1) - 1:
return True
i += 1
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted([c for c in s1])
s2 = sorted([c for c in s2])
return all(char1 <= char2 for char1, char2 in zip(s1, s2)) or all(
char1 >= char2 for char1, char2 in zip(s1, s2)
)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def check(self, d1, d2):
for x, y in zip(d1, d2):
if x <= y:
continue
return False
return True
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
d1 = sorted(s1)
d2 = sorted(s2)
return self.check(d1, d2) | self.check(d2, d1)
|
CLASS_DEF FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
ss1 = sorted(s1)
ss2 = sorted(s2)
f = True
r = True
for x, y in zip(ss1, ss2):
f = f and x >= y
r = r and x <= y
return f or r
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
return all(ch1 <= ch2 for ch1, ch2 in zip(sorted(s1), sorted(s2))) or all(
ch1 <= ch2 for ch1, ch2 in zip(sorted(s2), sorted(s1))
)
|
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
count1 = 0
count2 = 0
for i in range(len(s1)):
if s1[i] <= s2[i]:
count1 += 1
if s1[i] >= s2[i]:
count2 += 1
if count1 == len(s1) or count2 == len(s1):
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = "".join(sorted(s1))
s2 = "".join(sorted(s2))
cnt1 = 0
cnt2 = 0
for i in range(len(s1)):
if s1[i] >= s2[i]:
cnt1 += 1
if s2[i] >= s1[i]:
cnt2 += 1
print((cnt1, cnt2))
return cnt1 == len(s1) or cnt2 == len(s2)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
comparisons = [(a >= b) for a, b in zip(sorted(s1), sorted(s2))]
all_true = all(comparisons)
if not all_true:
comparisons = [(a <= b) for a, b in zip(sorted(s1), sorted(s2))]
all_true = all(comparisons)
return all_true
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
left = True
determined = False
for i, c in enumerate(s1):
if c > s2[i]:
if not left and determined:
return False
determined = True
elif c < s2[i]:
if left and determined:
return False
left = False
determined = True
return True
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
ans1 = [(a <= b) for a, b in zip(sorted(s1), sorted(s2))]
ans2 = [(a >= b) for a, b in zip(sorted(s1), sorted(s2))]
if len(set(ans1)) == 1 or len(set(ans2)) == 1:
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
def helper(d1, d2):
s = 0
for c in "abcdefghijklmnopqrstuvwxyz":
s += d1[c] - d2[c]
if s < 0:
return False
return True
c1 = collections.Counter(s1)
c2 = collections.Counter(s2)
return helper(c1, c2) or helper(c2, c1)
|
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR STRING VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1list = list(s1)
s2list = list(s2)
s1list.sort()
s2list.sort()
count1 = 0
count2 = 0
for i in range(len(s1)):
count1 += int(s1list[i] >= s2list[i])
for i in range(len(s1)):
count2 += int(s2list[i] >= s1list[i])
return not count1 % len(s1) or not count2 % len(s1)
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
a1 = [(s1[i] >= s2[i]) for i in range(len(s1))]
a2 = [(s1[i] <= s2[i]) for i in range(len(s1))]
return False not in a1 or False not in a2
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
ans1 = True
ans2 = True
for i in range(len(s1)):
if s1[i] < s2[i]:
ans1 = False
if s2[i] < s1[i]:
ans2 = False
return ans1 or ans2
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
s1 = sorted(s1)
s2 = sorted(s2)
size_t = len(s1)
for idx in range(size_t):
if s1[idx] == s2[idx]:
continue
elif s1[idx] < s2[idx]:
ans = set(map(lambda x, y: x <= y, s1, s2))
break
else:
ans = set(map(lambda x, y: x >= y, s1, s2))
break
if len(ans) > 1:
return False
return True
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
|
Given two strings: s1 and s2 with the same size, check if some permutation of string s1 can break some permutation of string s2 or vice-versa (in other words s2 can break s1).
A string x can break string y (both of size n) if x[i] >= y[i] (in alphabetical order) for all i between 0 and n-1.
Example 1:
Input: s1 = "abc", s2 = "xya"
Output: true
Explanation: "ayx" is a permutation of s2="xya" which can break to string "abc" which is a permutation of s1="abc".
Example 2:
Input: s1 = "abe", s2 = "acd"
Output: false
Explanation: All permutations for s1="abe" are: "abe", "aeb", "bae", "bea", "eab" and "eba" and all permutation for s2="acd" are: "acd", "adc", "cad", "cda", "dac" and "dca". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.
Example 3:
Input: s1 = "leetcodee", s2 = "interview"
Output: true
Constraints:
s1.length == n
s2.length == n
1 <= n <= 10^5
All strings consist of lowercase English letters.
|
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
arr1 = list(s1)
arr2 = list(s2)
arr1.sort()
arr2.sort()
n = len(arr1)
flag = True
flag1 = True
for i in range(n):
if arr1[i] < arr2[i]:
flag1 = False
flag2 = True
for i in range(n):
if arr1[i] > arr2[i]:
flag2 = False
if flag1 or flag2:
return True
return False
|
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.