description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def codeforces(arr): dist = {} ind = 1 for i in arr: try: dist[i].append(ind) except: dist[i] = [ind] ind += 1 arr.sort() ind = -1 res = 0 start = 0 for i in arr: if res < i: start = ind + 1 res += i ind += 1 ans = [] for i in range(start, len(arr)): ans.append(dist[arr[i]].pop()) ans.sort() print(len(ans)) s = " ".join(map(str, ans)) print(s) t = int(input()) for test in range(t): n = int(input()) arr = [int(i) for i in input().split()] codeforces(arr)
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys def read_int(): return int(sys.stdin.readline().strip()) def read_ints(): return list(map(int, sys.stdin.readline().split())) for _ in "." * read_int(): n = read_int() a = read_ints() req = 0 b = sorted(a) if n == 1: print("1\n1") continue s = [b[0]] for i in range(1, n): s.append(b[i] + s[i - 1]) if s[-2] < b[-1]: print("1") print(a.index(b[-1]) + 1) continue for j in range(n - 1): if s[j] < b[j + 1]: continue for x in range(j + 1, n - 1): if s[x] < b[x + 1]: break else: req = b[j] break print(n - j) for p in range(n): if a[p] >= req: print(p + 1, end=" ") print()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING 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 IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) li = [] for i in range(n): li.append([ar[i], i]) li.sort(key=lambda x: x[0]) br = [li[0][0]] for i in range(1, n): br.append(br[-1] + li[i][0]) ans = [li[-1][1] + 1] for i in range(n - 2, -1, -1): if br[i] < li[i + 1][0]: break else: ans.append(li[i][1] + 1) ans.sort() print(len(ans)) print(*ans)
IMPORT ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a2 = sorted(zip(a, range(n))) start = -1 base = 0 for i, xx in enumerate(a2): d = xx[0] if base < d: start = i base = base + d print(n - start) print(" ".join(map(str, sorted(x[1] + 1 for x in a2[start:]))))
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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] player = [[a[i - 1], i] for i in range(1, n + 1)] player = sorted(player, key=lambda x: x[0]) currPlayer = player[-1][0] tokenCount = sum(a) - currPlayer winners = [player[-1][-1]] i = 2 while i <= n and tokenCount >= currPlayer: currPlayer = player[-i][0] winners.append(player[-i][-1]) tokenCount -= currPlayer i += 1 print(len(winners)) for p in sorted(winners): print(p, end=" ") print()
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 LIST VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) get = list(map(int, input().split())) tokens = [(get[i], i) for i in range(n)] srt = sorted(tokens) i = 0 x = 0 front = 0 while i < n: num = srt[i][0] howmany = 1 for k in range(i + 1, n): if num == srt[k][0]: howmany += 1 else: break if i + howmany < n and front + num * howmany < srt[i + howmany][0]: x = srt[i][0] front += num * howmany i += howmany ans = [] for i in range(n): if tokens[i][0] > x: ans.append(tokens[i][1] + 1) print(len(ans)) print(" ".join(str(i) for i in ans))
IMPORT ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def solve(arr, n, ans): total = sum(arr) indices = [] for i in range(n): indices.append((arr[i], i)) indices.sort() dp = [False] * n dp[-1] = True total -= indices[-1][0] ans_arr = [indices[-1][1] + 1] for i in range(n - 2, -1, -1): if total >= indices[i + 1][0] and dp[i + 1]: dp[i] = True ans_arr.append(indices[i][1] + 1) total -= indices[i][0] ans_arr.sort() for i in range(len(ans_arr)): ans_arr[i] = str(ans_arr[i]) ans.append(str(len(ans_arr))) ans.append(" ".join(ans_arr)) def main(): t = int(input()) ans = [] for i in range(t): n = int(input()) arr = list(map(int, input().split())) solve(arr, n, ans) print("\n".join(ans)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) arr = list(map(int, stdin.readline().split())) a = sorted(arr) check = [] ans = [] s = sum(a) c = 0 m = a[-1] for j, v in enumerate(a[::-1]): if s >= m: c += 1 s -= v m = v check.append(v) else: break for i in range(n): if arr[i] in check: ans.append(i + 1) stdout.write(str(c) + "\n") stdout.write(" ".join(map(str, ans)) + "\n")
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 ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] array = sorted(arr, reverse=True) sm = sum(arr) k = 0 for element in array: sm -= element k += 1 if sm < element: break print(k) j = 1 myarray = [] for ele in arr: if ele >= element: myarray.append(str(j)) j += 1 print(" ".join(myarray))
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 FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
from sys import stdin nii = lambda: map(int, stdin.readline().split()) lnii = lambda: list(map(int, stdin.readline().split())) t = int(input()) for tt in range(t): n = int(input()) a = lnii() a_s = sorted(a) s = sum(a) for i in range(n - 1, -1, -1): s -= a_s[i] if s < a_s[i]: num = a_s[i] break ans = [(i + 1) for i in range(n) if a[i] >= num] print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) for i in range(n): a[i] = a[i], i a.sort() l, r = -1, n - 1 while r - l > 1: m = (l + r) // 2 t = 0 for i in range(m + 1): t += a[i][0] for i in range(m + 1, n): if t < a[i][0]: l = m break t += a[i][0] else: r = m res = [] for i in range(r, n): res.append(a[i][1] + 1) res.sort() print(len(res)) print(*res)
IMPORT ASSIGN VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): input() arr = map(int, input().split()) u = sorted((a, i) for i, a in enumerate(arr, 1)) cur = 0 res = [] for a, i in u: if cur < a: res.clear() res.append(i) cur += a print(len(res)) print(*sorted(res))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = list(enumerate(map(int, input().split()))) a.sort(key=lambda x: x[1]) p = [a[0][1]] for i in range(1, n): p.append(p[i - 1] + a[i][1]) ans = [] a.append([0, 0]) l = -1 for i in range(n): if p[i] < a[i + 1][1]: l = i for i in range(l + 1, n): ans.append(a[i][0] + 1) print(len(ans)) print(*sorted(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 EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for s in [*open(0)][2::2]: S = 0 z = [] for x, i in sorted((x, i + 1) for i, x in enumerate(map(int, s.split()))): if S < x: z = [] S += x z += [i] print(len(z), "\n", *sorted(z))
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR LIST VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for u in range(int(input())): n = int(input()) l = [int(w) for w in input().split()] l = [[l[i], i + 1] for i in range(n)] l.sort(key=lambda x: x[0]) s = l[0][0] x = l[-1][0] p = 0 for i in range(1, n): if l[i][0] > s: p = i s = s + l[i][0] continue s += l[i][0] ans = [] t = 0 for i in range(p, n): ans.append(l[i][1]) t += 1 print(t) print(*sorted(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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a_set = sorted(list(set(a))) win_dict = {}.fromkeys(a_set, 0) count_dict = {}.fromkeys(a_set, 0) for num in a: count_dict[num] += 1 prefix_sum_dict = {}.fromkeys(a_set, 0) if len(a_set) == 1: print(n) ans_str = "" for i in range(1, n + 1): ans_str += str(i) + " " print(ans_str[:-1]) else: prefix_sum = 0 for i in range(len(a_set)): prefix_sum += count_dict[a_set[i]] * a_set[i] prefix_sum_dict[a_set[i]] = prefix_sum for i in reversed(range(len(a_set) - 1)): if prefix_sum_dict[a_set[i]] >= a_set[i + 1]: win_dict[a_set[i]] = 1 else: break win_dict[a_set[-1]] = 1 ans = 0 ans_str = "" for index in range(len(a)): if win_dict[a[index]] == 1: ans += 1 ans_str += str(index + 1) + " " print(ans) print(ans_str[:-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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL DICT VAR NUMBER ASSIGN VAR FUNC_CALL DICT VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL DICT VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def answer(): ind, val = 0, all[0][0] for i in range(1, n): if all[i][0] > val: ind = i val += all[i][0] winners = [] for i in range(ind, n): winners.append(all[i][1] + 1) print(len(winners)) print(*sorted(winners)) for T in range(int(input())): n = int(input()) a = list(map(int, input().split())) all = [[a[i], i] for i in range(n)] all.sort(key=lambda x: x[0]) answer()
FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for ad in range(int(input())): n = int(input()) l = list(map(int, input().split())) s = sum(l) x = [] for i in range(n): x.append([l[i], i]) x = sorted(x, key=lambda a: a[0], reverse=True) ans = [] ans.append(x[0][1] + 1) for i in range(1, n): s -= x[i - 1][0] if s >= x[i - 1][0] and ans[-1] == x[i - 1][1] + 1: ans.append(x[i][1] + 1) ans.sort() print(len(ans)) 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline f_inf = float("inf") mod = 10**9 + 7 def solve(): def meguru_bisect(ok, ng): while abs(ok - ng) > 1: mid = (ok + ng) // 2 if is_ok(mid): ng = mid else: ok = mid return ng def is_ok(x): now = sum(B[i][0] for i in range(x + 1)) for i in range(x + 1, n): if now >= B[i][0]: now += B[i][0] else: return False return True n = int(input()) A = list(map(int, input().split())) B = [[A[i], i] for i in range(n)] B.sort() limit = meguru_bisect(-1, n) res = [(B[i][1] + 1) for i in range(n) if i >= limit] res.sort() print(len(res)) print(*res) def resolve(): t = int(input()) for _ in range(t): solve() resolve()
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF FUNC_DEF WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) l1 = [] for i in range(n): l1.append([l[i], i + 1]) l1.sort() f = [0] * n f[n - 1] = 1 p = [0] * n p[0] = l1[0][0] for i in range(1, n): p[i] = p[i - 1] + l1[i][0] l3 = [l1[-1][1]] c = 1 for i in range(n - 2, -1, -1): if p[i] >= l1[i + 1][0] and f[i + 1] == 1: f[i] = 1 c += 1 l3.append(l1[i][1]) else: break l3.sort() print(c) print(*l3)
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def take_first(elem): return elem[0] q = int(input()) while q > 0: n = int(input()) a = input().split() b = [] for i in range(n): p = [] p.append(int(a[i])) p.append(i) b.append(p) sorted_list = sorted(b, key=take_first) sorted_list[0].append(sorted_list[0][0]) for i in range(1, n): sorted_list[i].append(sorted_list[i - 1][2] + sorted_list[i][0]) ans = [] ans.append(sorted_list[n - 1][1] + 1) i = n - 2 while i >= 0: if sorted_list[i][2] < sorted_list[i + 1][0]: break ans.append(sorted_list[i][1] + 1) i = i - 1 ans.sort() print(len(ans)) for i in ans: print(i, end=" ") print() q = q - 1
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = sorted([a[i] for i in range(n)]) l = 0 r = n - 1 m = 0 good = n while l <= r: m = l + r >> 1 s = b[m] fl = True for i in range(n): if i == m: continue elif s >= b[i]: s += b[i] else: s = -1 if s >= 0: good = m r = m - 1 else: l = m + 1 ans = [] for i in range(n): if a[i] >= b[good]: ans.append(i + 1) print(len(ans)) for val in ans: print(val, end=" ") print()
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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def solve(): n = int(input()) a = list(map(int, input().split())) p = [(a[i], i + 1) for i in range(n)] p.sort() k, m = 0, p[0][0] for i in range(1, n): if p[i][0] > m: k = i m += p[i][0] s = [p[i][1] for i in range(k, n)] s.sort() print(len(s)) print(" ".join(map(str, s))) tests = int(input()) for test in range(tests): 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 ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys YES = "YES" NO = "NO" MOD = 1000000007 def input_int(): return int(input()) def input_list(): return input().split(" ") def input_list_int(): return list(map(int, input_list())) def solve(): n = input_int() a = input_list_int() ad = {} for i in range(0, n): ad[i] = a[i] ad = sorted(ad.items(), key=lambda x: x[1]) s = [0] * n s[0] = ad[0][1] for i in range(1, n): s[i] = s[i - 1] + ad[i][1] res = [] for i in range(n - 2, -1, -1): if s[i] >= ad[i + 1][1]: res.append(ad[i][0] + 1) else: break res.append(ad[n - 1][0] + 1) res.sort() print(len(res)) for i in res: print(i, end=" ") print() query_count = input_int() while query_count: query_count -= 1 solve()
IMPORT ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) temp = a[:] ar = [] if n == 1: print(1) print(1) continue a.sort() ans = 2 if a[0] == a[1]: ans = 1 sum = a[0] for i in range(1, n): if sum < a[i]: ans = i + 1 sum += a[i] for i in range(n): if temp[i] >= a[ans - 1]: ar.append(i + 1) print(len(ar)) for i in ar: print(i, end=" ") print("")
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 VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) nums = list(enumerate(list(map(int, input().split())), 1)) nums.sort(key=lambda x: x[1]) sum = 0 pre = [] start = 0 for i in range(len(nums)): sum += nums[i][1] pre.append(sum) for i in range(0, len(pre) - 1): if pre[i] < nums[i + 1][1]: start = i + 1 lst = [] for j in range(start, len(nums)): lst.append(nums[j][0]) lst.sort() print(len(lst)) print(*lst)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [] for i in range(n): b.append((a[i], i)) b.sort() l = 0 c = [] temp = 0 for i in range(0, n - 1): temp += b[i][0] if temp >= b[i + 1][0]: c.append(b[i][1] + 1) l += 1 else: c.clear() l = 0 c.append(b[n - 1][1] + 1) c.sort() l += 1 print(l) 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys from itertools import accumulate input = sys.stdin.readline def read(): N = int(input().strip()) A = list(map(int, input().strip().split())) return N, A def argsort(seq): return sorted(range(len(seq)), key=seq.__getitem__) def solve(N, A): players = argsort(A) B = [a for a in sorted(A)] S = list(accumulate(B)) ans = [players[-1] + 1] lose = False for i in range(N - 2, -1, -1): if not lose and S[i] >= B[i + 1]: ans.append(players[i] + 1) else: lose = True ans.sort() print(len(ans)) print(*ans) T = int(input().strip()) for t in range(T): inputs = read() solve(*inputs)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = [[int(el), i + 1] for i, el in enumerate(input().split())] a.sort(key=lambda x: x[0]) suma = 0 for i in range(n - 1): suma += a[i][0] ans = [a[-1][1]] ansl = 1 for i in range(n - 2, -1, -1): if suma >= a[i + 1][0]: ans.append(a[i][1]) ansl += 1 suma -= a[i][0] else: break ans.sort() print(ansl) print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) l = [*map(int, input().split())] lis = [] for i in range(n): lis.append((l[i], i + 1)) lis.sort() lk = [] for i in range(n): lk.append(lis[i][1]) li = sorted(l) s = 0 b = -1 for i in range(n): if i >= 1: if li[i] > s: b = i s += li[i] print(min(n - b, n)) if b == -1: print(*sorted(lk)) else: print(*sorted(lk[b:]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def solve(arr): arr.sort() s = 0 sum_arr = [] for i in range(len(arr)): s += arr[i] sum_arr.append(s) ans = 0 for i in range(len(arr) - 2, -1, -1): if sum_arr[i] >= arr[i + 1]: ans = arr[i] else: break if ans: return ans else: return arr[-1] t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) ans = [] ans_temp = solve(arr.copy()) for i in range(len(arr)): if arr[i] >= ans_temp: ans.append(i + 1) print(len(ans)) print(" ".join([str(num) for num in sorted(ans)]))
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR RETURN VAR RETURN VAR NUMBER 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 LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): n, values = int(input()), [int(i) for i in input().split()] values = list(sorted([([values[i]] + [i]) for i in range(n)])) winner, psa, ans = ( [(False) for i in range(n - 1)] + [True], [0], [values[-1][1] + 1], ) for i in range(n): psa.append(psa[i] + values[i][0]) psa.pop(0) for i in range(n - 2, -1, -1): if psa[i] >= values[i + 1][0] and winner[i + 1]: winner[i] = True ans.append(values[i][1] + 1) print(len(ans)) print(*list(sorted(ans)))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST VAR VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST NUMBER LIST NUMBER LIST BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) while t != 0: n = int(input()) list1 = list(map(int, input().split())) for i in range(len(list1)): list1[i] = [list1[i], i] list1.sort() pref = [(0) for i in range(len(list1))] pref[0] = list1[0][0] for i in range(1, n): pref[i] = pref[i - 1] + list1[i][0] ans = [] ans.append(list1[-1][1] + 1) for i in range(len(pref) - 2, -1, -1): if pref[i] >= list1[i + 1][0]: ans.append(list1[i][1] + 1) else: break ans.sort() print(len(ans)) print(*ans) 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) lis = [] for i in range(n): lis.append([arr[i], i]) lis.sort() i = 0 j = 1 s = lis[0][0] while i < n and j < n: if s >= lis[j][0]: s += lis[j][0] j += 1 elif s < lis[j][0]: s += lis[j][0] i = j j += 1 print(n - i) ans = [] for k in range(i, n): ans.append(lis[k][1] + 1) ans.sort() 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) q = list(map(int, input().split())) f = list(sorted(q)) f.reverse() sumi = sum(f) number = 1 listo = [] for j in range(len(f)): if f[j] >= sumi // 2 + 1: number = f[j] break sumi = sumi - f[j] k = 0 for c in range(n): if q[c] >= number: listo.append(c + 1) k = k + 1 print(k) print(*listo, sep=" ")
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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys for _ in range(int(input())): n = int(input()) arr = list(map(int, sys.stdin.readline().split())) sorted_arr = list(sorted(arr)) smallest = 0 for i in range(0, n - 1): if sorted_arr[i] < sorted_arr[i + 1]: smallest = sorted_arr[i + 1] sorted_arr[i + 1] += sorted_arr[i] else: sorted_arr[i + 1] += sorted_arr[i] if sorted_arr[i + 1] > sorted_arr[-1]: break cnt = 0 lst = [] for i in range(n): if arr[i] >= smallest: cnt += 1 lst.append(i + 1) print(cnt) print(" ".join(map(str, lst)))
IMPORT 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def convertToDic(arr): thing = {} for x in arr: try: thing[x] += 1 except: thing[x] = 1 return thing def convertDicToProduct(thing): dic = {} for x in thing: dic[x] = thing[x] * x return dic def getResult(dic): result = {} indicies = getIndicies(dic) allFalse = {} for x in indicies: allFalse[x] = False sum = 0 for x in range(len(dic) - 1): sum += dic[indicies[x]] if sum < indicies[x + 1]: result = allFalse.copy() else: result[indicies[x]] = True result[indicies[len(indicies) - 1]] = True return result def getIndicies(dic): indicies = [] for x in dic: indicies.append(x) return indicies for _ in range(int(input())): bruh = int(input()) original = list(map(int, list(input().split(" ")))) arr = original.copy() arr.sort() thing = convertToDic(arr) dic = convertDicToProduct(thing) result = getResult(dic) players = 0 for x in result: if result[x]: players += thing[x] print(players) finals = "" for x in range(len(original)): if result[original[x]]: finals += str(x + 1) + " " print(finals)
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN 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 FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
from sys import stdin T = int(stdin.readline()) while T > 0: T -= 1 n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) minsum = 0 lostSet = set() newLoser = True losers = 0 aS = sorted(a) for i in range(1, len(aS)): if aS[i] - aS[i - 1] > 0: losers = i minsum = aS[i] aS[i] += aS[i - 1] print(n - losers) for i in range(len(a)): if a[i] < minsum: continue print(i + 1, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER 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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for k in range(t): arraySize = int(input()) array = [int(s) for s in input().split()] pairsArray = [[0, 0]] * arraySize for i in range(arraySize): pairsArray[i] = [array[i], i] pairsArray.sort() sumArray = [0] * arraySize sumArray[0] = pairsArray[0][0] for i in range(1, arraySize): sumArray[i] = sumArray[i - 1] + pairsArray[i][0] res = [0] * arraySize res[arraySize - 1] = 1 ans = [pairsArray[arraySize - 1][1] + 1] for i in range(arraySize - 2, -1, -1): if pairsArray[i + 1][0] <= sumArray[i]: res[i] = res[i + 1] ans.append(pairsArray[i][1] + 1) else: break print(len(ans)) ans.sort() for e in ans: print(e, end=" ") print()
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 BIN_OP LIST LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = [(k + 1, i) for k, i in enumerate(map(int, input().split()))] a.sort(key=lambda x: x[1]) aft, cnt, lelem = -1, 0, a[-1][1] for k, (__, v) in enumerate(a): cnt += v if cnt >= lelem or cnt >= a[k + 1][1]: continue aft = k print(n - (aft + 1)) b = sorted([k for k, v in a[aft + 1 :]]) for i in b: print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
from sys import stdin input = stdin.readline def check_win(index): z = b[index] for i in range(n): if i == index: continue if z >= b[i]: z += b[i] if z >= b[-1]: return True else: return False return True def binary_search(): l = 0 r = n while l < r: m = l + (r - l) // 2 if check_win(m): r = m z = m else: l = m + 1 return b[z] t = int(input()) for _ in range(t): n = int(input()) a = [int(x) for x in input().split()] b = sorted(a) x = binary_search() ans = [] for i in range(n): if a[i] >= x: ans.append(i + 1) print(len(ans)) print(*ans)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) ans = [] for _ in range(t): n = int(input()) a = [int(i) for i in input().split()] b = [] for i in range(n): b.append((a[i], i + 1)) b.sort(reverse=True) c = [] s = sum(a) c.append(b[0][1]) s -= b[0][0] for i in range(1, n): if s < b[i - 1][0]: ans.append(sorted(c)) break c.append(b[i][1]) s -= b[i][0] else: ans.append(sorted(c)) for i in ans: print(len(i)) print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) while t > 0: t -= 1 n = int(input()) args = list(map(int, input().split())) players = [(i + 1, args[i]) for i in range(n)] players.sort(key=lambda x: x[1]) s = 0 cur = 0 for i in range(n - 1): s += players[i][1] if s < players[i + 1][1]: cur = i + 1 ans = [] for i in range(cur, n): ans.append(players[i][0]) ans.sort() print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER 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 VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) x = [0] * n for i in range(n): x[i] = A[i] A.sort(reverse=True) l = 0 r = n - 1 B = [0] * n B[n - 1] = A[n - 1] for i in range(n - 2, -1, -1): B[i] = B[i + 1] + A[i] ans = [] s = 10**9 for __ in range(30): m = (l + r) // 2 z = B[m] fl = 1 for i in range(m - 1, -1, -1): if z >= A[i]: z += A[i] fl = 1 else: fl = 0 break if fl == 1: s = min(s, A[m]) l = m else: r = m for i in range(n): if x[i] >= s: ans.append(i + 1) print(len(ans)) 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP 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 LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = sys.stdin.readline def main(): n = int(input()) alst = list(map(int, input().split())) blst = sorted(alst) l = -1 r = n - 1 while r - l > 1: mid = (r + l) // 2 total = sum(blst[: mid + 1]) for i in range(mid + 1, n): if blst[i] > total: l = mid break total += blst[i] else: r = mid th = blst[r] ans = [] for i, a in enumerate(alst, 1): if a >= th: ans.append(i) print(len(ans)) print(*ans) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) l1 = [int(__) for __ in input().split()] l2 = [] for i in range(n): l2.append([i + 1, l1[i], 0]) l1.sort() l2.sort(key=lambda x: x[1]) for i in range(1, n): l2[i][1] += l2[i - 1][1] for i in range(n - 1): if l2[i][1] >= l1[i + 1]: l2[i][2] = 1 l2[-1][2] = 1 ans = [] for i in l2[::-1]: if i[2] == 1: ans.append(i[0]) else: break print(len(ans)) for i in sorted(ans): print(i, end=" ") print("")
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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split())) mod = 10**9 + 7 Mod = 998244353 INF = float("inf") sys.setrecursionlimit(2 * 10**5 + 100) tc = 1 (tc,) = inp() for _ in range(tc): (n,) = inp() a = inp() b = sorted(range(n), key=lambda x: a[x]) pre = [a[b[0]]] for i in range(1, n): pre.append(pre[-1] + a[b[i]]) win = [False] * n win[-1] = True for i in range(n - 2, -1, -1): if a[b[i + 1]] <= pre[i]: win[i] = win[i + 1] ans = [(b[i] + 1) for i in range(n) if win[i]] print(len(ans)) if ans: print(*sorted(ans))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = sorted(zip(map(int, input().split()), range(n))) l, r = 0, n - 1 while l < r: m = (l + r) // 2 s0 = sum(a[i][0] for i in range(m + 1)) flag = True for i in range(m + 1, n): if a[i][0] <= s0: s0 += a[i][0] else: flag = False break if flag: r = m else: l = m + 1 f = [] for i in range(l, n): f.append(a[i][1] + 1) print(len(f)) print(*sorted(f))
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 FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) b = enumerate(a) b = sorted(enumerate(a), key=lambda x: x[1]) if n == 1: print(1) print(1) continue s = 0 tot = [0] for j in range(n): s += b[j][1] tot.append(s) ans = [] for j in range(n - 1, -1, -1): if b[j][1] > tot[j]: ans.append(b[j][0] + 1) break else: ans.append(b[j][0] + 1) ans.sort() print(len(ans)) 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
def main(n, a): dicti = {} for i in range(n): if a[i] not in dicti: dicti[a[i]] = [i + 1] continue dicti[a[i]].append(i + 1) dp = [0] * n a.sort() dp[0] = a[0] for i in range(1, n): dp[i] = dp[i - 1] + a[i] scores = {} for i in range(n): if a[i] not in scores: scores[a[i]] = dp[i] scores[a[i]] = max(dp[i], scores[a[i]]) book = [] score = [] maxscore = [] for x, y in scores.items(): score.append(x) maxscore.append(y) book = [] for i in range(len(scores) - 1): if maxscore[i] >= score[i + 1]: book.append(score[i]) else: book = [] book.append(score[-1]) res = [] for i in range(len(book)): temp = dicti[book[i]] for t in temp: res.append(t) res.sort() return res k = int(input()) for _ in range(k): n = int(input()) a = list(map(int, input().split())) res = main(n, a) print(len(res)) print(*res, sep=" ")
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR 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 VAR EXPR FUNC_CALL VAR VAR STRING
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for t in range(int(input())): input() s = 0 z = [] for x, i in sorted((x, i + 1) for i, x in enumerate(map(int, input().split()))): if s < x: z = [] s += x z.append(i) print(len(z)) print(" ".join(map(str, sorted(z))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) get = list(map(int, input().split())) tokens = [(get[i], i) for i in range(n)] srt = sorted(tokens) cal = 0 lose_until = -1 for i in range(n - 1): cal += srt[i][0] if cal < srt[i + 1][0]: lose_until = i ans = srt[lose_until + 1 :] leng = len(ans) e = [] for i in range(leng): e.append(ans[i][1] + 1) print(leng) e.sort() print(*e)
IMPORT ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
from sys import stdin, stdout input = stdin.readline def im(): return map(int, input().split()) def ii(): return int(input()) def il(): return list(map(int, input().split())) def ins(): return input()[:-1] for _ in range(ii()): n = ii() lis = il() arr = [[lis[i], i + 1] for i in range(n)] arr.sort(key=lambda x: x[0]) pre = [] sm = 0 ans = [] for i in range(n): pre.append(sm + arr[i][0]) sm = sm + arr[i][0] for i in range(n - 1): if pre[i] >= arr[i + 1][0]: ans.append(arr[i][1]) else: ans = [] ans.append(arr[-1][1]) ans.sort() print(len(ans)) print(*ans)
ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for ti in range(t): n = int(input()) lst = input() lst = list(lst.split()) a = [] i = 1 for item in lst: a.append([int(item), i]) i += 1 if n == 1: print(1) print(1) continue a.sort(key=lambda item: item[0]) ans = [a[len(a) - 1][1]] i = len(a) - 2 while i >= 0: if i == 0: if a[i][0] == a[i + 1][0]: ans.append(a[i][1]) break if a[i][0] == a[i + 1][0]: ans.append(a[i][1]) else: j = i - 1 pot_item = a[i][0] while j >= 0 and pot_item < a[i + 1][0]: pot_item += a[j][0] j -= 1 if pot_item >= a[i + 1][0]: ans.append(a[i][1]) else: break i -= 1 ans.sort() print(len(ans)) print(" ".join([str(elem) for elem in ans])) 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
test_cases = int(input()) for test in range(test_cases): num_of_players = int(input()) power_level = list(map(int, input().split(" "))) players = [i for i in range(num_of_players)] players_ascending_power = sorted( players, key=lambda player_id: power_level[player_id] ) strongest_player = players_ascending_power[-1] highest_power = power_level[strongest_player] accumulated_power = power_level[players_ascending_power[0]] streak_start = 0 streak_size = 1 for i in range(1, num_of_players): next_opponent = players_ascending_power[i] difficulty = power_level[next_opponent] if accumulated_power >= difficulty: streak_size += 1 else: streak_start = i streak_size = 1 accumulated_power += difficulty possible_winners = sorted( [ (players_ascending_power[i] + 1) for i in range(streak_start, streak_start + streak_size) ] ) print(len(possible_winners)) print(" ".join(list(map(str, possible_winners))))
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 ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
t = int(input()) for _ in range(t): n = int(input()) arr = [(int(i), index) for index, i in enumerate(input().split(), 1)] arr.sort(reverse=True) ans_list = [arr[0][1]] s = sum([i[0] for i in arr]) - arr[0][0] for i in range(1, n): if s < arr[i - 1][0]: break s -= arr[i][0] ans_list.append(arr[i][1]) ans_list.sort() print(len(ans_list)) print(" ".join(str(i) for i in ans_list))
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 VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
A championship is held in Berland, in which $n$ players participate. The player with the number $i$ has $a_i$ ($a_i \ge 1$) tokens. The championship consists of $n-1$ games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship. All random decisions that are made during the championship are made equally probable and independently. For example, if $n=4$, $a = [1, 2, 4, 3]$, then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now $a = [0, 2, 4, 4]$; during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now $a = [0, 2, 8, 0]$; during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now $a = [0, 0, 10, 0]$; the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case consists of one positive integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of players in the championship. The second line of each test case contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of tokens the players have. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. -----Examples----- Input 2 4 1 2 4 3 5 1 1 1 1 1 Output 3 2 3 4 5 1 2 3 4 5 -----Note----- None
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split(" "))) aa = [[a[i], i] for i in range(n)] aa = sorted(aa, key=lambda aa: [aa[0]], reverse=True) s = sum(a) ind = 0 for i in range(n): s -= aa[i][0] if s < aa[i][0]: ind = i break print(ind + 1) aa = sorted(aa[: ind + 1], key=lambda aa: [aa[1]]) for ai in aa: print(ai[1] + 1, end=" ") print("")
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 STRING ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER LIST VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR STRING
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[1]) ans = [] ans.append(intervals[0]) for i in range(1, N): if ans[-1][1] > intervals[i][0]: continue else: ans.append(intervals[i]) return len(intervals) - len(ans)
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, ranges): size = len(ranges) rem = 0 ranges.sort() end = ranges[0][1] for i in range(1, size): if ranges[i][0] < end: rem += 1 end = min(ranges[i][1], end) else: end = ranges[i][1] return rem
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[0]) prev = intervals[0][1] count = 0 for i in intervals[1:]: if prev <= i[0]: prev = i[1] else: count += 1 prev = min(prev, i[1]) return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals = sorted(intervals, key=lambda x: x[0]) curr_interval = intervals[0] count = 0 for i in range(1, N): if curr_interval[1] <= intervals[i][0]: curr_interval = intervals[i] else: count += 1 if curr_interval[1] > intervals[i][1]: curr_interval = intervals[i] return count
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): count = 0 intervals.sort(key=lambda x: x[1]) s = intervals[0][0] e = intervals[0][1] for i, j in intervals[1:]: if i < e: count += 1 elif i >= e: s = i e = j return count
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[1]) endTime = float("-inf") c = 0 for x in intervals: if endTime <= x[0]: endTime = x[1] else: c += 1 return c
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: (x[1], x[0])) prev = intervals[0] ans = 0 for i in range(1, N): if prev[1] <= intervals[i][0]: prev = intervals[i] else: ans += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort() res = 0 lastEnd = intervals[0][1] for start, end in intervals[1:]: if start >= lastEnd: lastEnd = end else: res += 1 lastEnd = min(lastEnd, end) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, n, intervals): if len(intervals) == 0: return 0 intervals = sorted(intervals, key=lambda x: x[1]) removeNum, curBorder = 0, intervals[0][1] for interval in intervals[1:]: if interval[0] < curBorder: removeNum += 1 else: curBorder = interval[1] return removeNum
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals = sorted(intervals) l = 0 r = 1 count = 0 while r < len(intervals): if intervals[l][1] <= intervals[r][0]: l = r r += 1 elif intervals[l][1] <= intervals[r][1]: r += 1 count += 1 elif intervals[l][1] > intervals[r][1]: l = r r += 1 count += 1 return count
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): if N == 1: return 0 count = 0 intervals.sort(key=lambda i: i[1]) res = intervals[0][1] for s, t in intervals[1:]: if s >= res: res = t else: count += 1 return count
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[1]) count = 0 i = 0 for j in range(1, len(intervals)): if intervals[i][1] > intervals[j][0]: count += 1 else: i = j return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, n, arr): arr.sort(key=lambda x: x[1]) i = 0 j = 1 count = 1 while j != n: if arr[i][1] <= arr[j][0]: i = j j += 1 count += 1 else: j += 1 return n - count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, Intervals): intervals = sorted(Intervals, key=lambda x: x[0]) q = [intervals[0]] c = 0 for i in range(1, len(intervals)): if q[-1][1] > intervals[i][0]: a, b = q.pop() q.append([a, min(b, intervals[i][1])]) c += 1 else: q.append(intervals[i]) return c
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[1]) m = intervals[0][1] c = 0 for i in range(1, N): if intervals[i][0] >= m: m = intervals[i][1] else: c += 1 return c
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda i: i[1]) st = intervals[0][0] en = intervals[0][1] c = 0 s = 0 e = 0 i = 1 while i < N: s = intervals[i][0] e = intervals[i][1] if s < en: c += 1 else: st = s en = e i += 1 return c
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[1]) count = 0 time = float("-inf") for start, end in intervals: if start < time: count += 1 else: time = end return count
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): end, cnt = float("-inf"), 0 for s, e in sorted(intervals, key=lambda x: x[1]): if s >= end: end = e else: cnt += 1 return cnt
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, arr): arr.sort() stack = [] cnt = 0 for item in arr: currs, currend = item if len(stack) == 0: stack.append((currs, currend)) else: prevs, prevend = stack.pop() if currs >= prevend: stack.append((prevs, prevend)) stack.append((currs, currend)) else: if prevend < currend: stack.append((prevs, prevend)) else: stack.append((currs, currend)) cnt += 1 return cnt
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals = sorted(intervals, key=lambda x: x[1]) prev_s = intervals[0][0] prev_e = intervals[0][1] count = 0 for s, e in intervals: if s < prev_e: pass else: prev_s = s prev_e = e count += 1 return N - count - 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): if not intervals or N == 0: return 0 intervals.sort() prevEnd = intervals[0][1] cnt = 0 for i in range(1, N): if prevEnd > intervals[i][0]: cnt += 1 prevEnd = min(prevEnd, intervals[i][1]) else: prevEnd = intervals[i][1] return cnt
CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR
Given an 2D array intervals of size N representing intervals where intervals [ i ] = [start_{i} , end_{i }), return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Example 1: Input: N = 4 intervals [ ] = {{1, 2}, {2, 3}, {3, 4}, {1, 3}} Output: 1 Explanation: {1, 3} can be removed and the rest of the intervals are non-overlapping. Example 2: Input: N = 3 intervals [ ] = {{1, 3}, {1, 3}, {1, 3}} Output: 2 Explanation: You need to remove two {1, 3} to make the rest of the intervals non-overlapping. Your Task: You don't need to read input or print anything. Your task is to complete the function minRemoval() which takes the interger N and 2D integer array intervals [ ] as parameters and returns the minimum number of intervals needs to remove in order to make the remaining intervals non-overlapping. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(1) Constraints: 1 ≀ N ≀ 10^{5} |intervals_{i}| == 2 -5*10^{4 }≀ start_{i }< end_{i} <=5*10^{4}
class Solution: def minRemoval(self, N, intervals): intervals.sort(key=lambda x: x[0]) i = 1 ans = 0 end = intervals[0][1] while i < len(intervals): if intervals[i][0] < end: ans += 1 if end > intervals[i][1]: end = intervals[i][1] else: end = intervals[i][1] i += 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ— n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ— a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating β€” find Bob's first move, such that after it you can be sure that Alice cheated. -----Input----- The first line of the input contains three integers: n, k and a (1 ≀ n, k, a ≀ 2Β·10^5) β€” the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≀ m ≀ n) β€” the number of Bob's moves. The third line contains m distinct integers x_1, x_2, ..., x_{m}, where x_{i} is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n. -----Output----- Print a single integer β€” the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1". -----Examples----- Input 11 3 3 5 4 8 6 1 11 Output 3 Input 5 1 3 2 1 5 Output -1 Input 5 1 3 1 3 Output 1
n, k, a = list(map(int, input().split())) m = int(input()) + 1 x = list(map(int, input().split())) + [0] l, r = 0, m while r - l > 1: d = (l + r) // 2 y = sorted(x[:d]) if sum((q - p) // (a + 1) for p, q in zip([0] + y, y + [n + 1])) >= k: l = d else: r = d print(r % m - (r == m))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR BIN_OP VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ— n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ— a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating β€” find Bob's first move, such that after it you can be sure that Alice cheated. -----Input----- The first line of the input contains three integers: n, k and a (1 ≀ n, k, a ≀ 2Β·10^5) β€” the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≀ m ≀ n) β€” the number of Bob's moves. The third line contains m distinct integers x_1, x_2, ..., x_{m}, where x_{i} is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n. -----Output----- Print a single integer β€” the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1". -----Examples----- Input 11 3 3 5 4 8 6 1 11 Output 3 Input 5 1 3 2 1 5 Output -1 Input 5 1 3 1 3 Output 1
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n, k, a = mints() a += 1 m = mint() x = list(mints()) l = 0 r = m + 1 while r - l > 1: c = (l + r) // 2 b = x[:c] b.sort() last = 0 cnt = 0 for i in b: if i != last: cnt += (i - last) // a last = i cnt += (n + 1 - last) // a if cnt < k: r = c else: l = c if r == m + 1: r = -1 print(r) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ— n table). At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ— a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating β€” find Bob's first move, such that after it you can be sure that Alice cheated. -----Input----- The first line of the input contains three integers: n, k and a (1 ≀ n, k, a ≀ 2Β·10^5) β€” the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other. The second line contains integer m (1 ≀ m ≀ n) β€” the number of Bob's moves. The third line contains m distinct integers x_1, x_2, ..., x_{m}, where x_{i} is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n. -----Output----- Print a single integer β€” the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print "-1". -----Examples----- Input 11 3 3 5 4 8 6 1 11 Output 3 Input 5 1 3 2 1 5 Output -1 Input 5 1 3 1 3 Output 1
n, k, a = map(int, input().split()) m = int(input()) x = [int(y) for y in input().split()] def check(K): used = [0] * (n + 1) for i in range(K): used[x[i]] = 1 for i in range(1, n + 1): used[i] += used[i - 1] have = 0 i = a while i < n + 1: if used[i] - used[i - a] == 0: have += 1 i += a i += 1 return have >= k if check(m): print(-1) else: low = -1 high = m while high - low > 1: if check((low + high + 1) // 2): low = (low + high + 1) // 2 else: high = (low + high + 1) // 2 print(high)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def books(alice, bob, both, k, n): if len(alice) + len(both) < k or len(both) + len(bob) < k: return -1 alice = sorted(alice) bob = sorted(bob) for i in range(min(len(alice), len(bob))): both.append(alice[i] + bob[i]) return sum(sorted(both)[:k]) a, b, c = [], [], [] x, y = map(int, input().strip().split()) for i in range(x): t, alice, bob = map(int, input().strip().split()) if alice and bob: c.append(t) elif alice: a.append(t) elif bob: b.append(t) print(books(a, b, c, y, x))
FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) obaja = [] bob = [] alice = [] a = 0 b = 0 for i in range(n): t, a, b = map(int, input().split()) if a == 1: if b == 1: obaja.append(t) else: alice.append(t) elif b == 1: bob.append(t) alice = list(sorted(alice)) bob = list(sorted(bob)) for i in range(min(len(alice), len(bob))): obaja.append(alice[i] + bob[i]) obaja = list(sorted(obaja)) if len(obaja) < k: print(-1) else: ans = 0 for i in range(k): ans += obaja[i] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys def input(): return sys.stdin.readline().rstrip() def input_split(): return [int(i) for i in input().split()] def add_book(): global freqs, cf, cp while True: if cf < freqs[cp]: cf += 1 return cp else: cp += 1 cf = 0 def remove_book(): global freqs, cf, cp while True: if cf > 0: cf -= 1 return -cp else: cp -= 1 cf = freqs[cp] def make_available(b_time): global freqs, cf, cp freqs[b_time] += 1 if b_time < cp: return b_time + remove_book() else: return 0 def make_unavailable(b_time): global freqs, cf, cp if b_time < cp: freqs[b_time] -= 1 return -b_time + add_book() elif b_time == cp and cf == freqs[cp]: freqs[b_time] -= 1 cf -= 1 return -b_time + add_book() else: freqs[b_time] -= 1 return 0 n, m, k = input_split() times = [] alice_likes = [] bob_likes = [] for _ in range(n): t, a, b = input_split() times.append(t) alice_likes.append(a) bob_likes.append(b) times_both = [] times_alice = [] times_bob = [] times_none = [] for book in range(n): if alice_likes[book] == 1 and bob_likes[book] == 1: times_both.append((times[book], book)) elif alice_likes[book] == 1: times_alice.append((times[book], book)) elif bob_likes[book] == 1: times_bob.append((times[book], book)) else: times_none.append((times[book], book)) times_both.sort() times_alice.sort() times_bob.sort() started = False possibilities = [] for i in range(k + 1): both = i indi = k - i extra = m - (both + 2 * indi) if ( both > len(times_both) or indi > min(len(times_alice), len(times_bob)) or extra < 0 ): continue if not started: started = True ans = sum([i for i, j in times_both[:both]]) ans += sum([i for i, j in times_alice[:indi]]) ans += sum([i for i, j in times_bob[:indi]]) freqs = [(0) for i in range(10001)] pending = times_both[both:] + times_bob[indi:] + times_alice[indi:] + times_none for tim, _ in pending: freqs[tim] += 1 cp = 0 cf = 0 extra_cost = 0 count = 0 for _ in range(extra): extra_cost += add_book() ans += extra_cost possibilities.append((ans, both)) else: last_ans = possibilities[-1][0] ans = last_ans new_book1_time = times_alice[indi][0] new_book2_time = times_bob[indi][0] old_book_time = times_both[both - 1][0] ans += old_book_time ans -= new_book1_time ans -= new_book2_time ans += make_available(new_book1_time) ans += make_available(new_book2_time) ans += make_unavailable(old_book_time) ans += add_book() possibilities.append((ans, both)) if len(possibilities) == 0: print(-1) else: ans, both = min(possibilities) indi = k - both extra = m - (indi * 2 + both) books = [] books += [j for i, j in times_both[:both]] books += [j for i, j in times_alice[:indi]] books += [j for i, j in times_bob[:indi]] pending = times_both[both:] + times_bob[indi:] + times_alice[indi:] + times_none pending.sort() books += [j for i, j in pending[:extra]] books = [(b + 1) for b in books] print(ans) print(*books, sep=" ")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF WHILE NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR RETURN NUMBER FUNC_DEF IF VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = list(map(int, input().rstrip().split())) t = [] ta = [] tb = [] common = 0 cm = {} for i in range(n): a = list(map(int, input().rstrip().split())) if a[1] == 1 & a[2] == 1: t.append(a[0]) else: if a[1] == 1: ta.append(a[0]) if a[2] == 1: tb.append(a[0]) if len(ta) < k - len(t) or len(tb) < k - len(t): print(-1) else: ta.sort() tb.sort() t.sort() s = [] for i in range(min(len(ta), len(tb))): s.append(ta[i] + tb[i]) l = t[:k] + s[:k] l.sort() print(sum(l[:k]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
a, b = map(int, input().split()) m, n, o, ans = [], [], [], 0 for _ in range(a): q, w, e = map(int, input().split()) if w == e == 1: m.append(q) elif w != e: if e == 0: n.append(q) else: o.append(q) m.sort() n.sort() o.sort() v, t, u = [], min(len(n), len(o)), len(m) for i in range(t): v.append(n[i] + o[i]) if b > u + t: print(-1) else: i, j = 0, 0 while b > 0: if i < u and j < t: if m[i] <= v[j]: ans += m[i] i += 1 else: ans += v[j] j += 1 elif i <= u - 1: ans += m[i] i += 1 elif j <= t - 1: ans += v[j] j += 1 b -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST LIST LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = list(map(int, input().split())) times = [[], [], [], []] for _ in range(n): t, a, b = list(map(int, input().split())) times[2 * a + b].append(t) for i in range(1, 4): times[i] = sorted(times[i][:]) prefix_sums = [[], [], [], []] for i in range(1, 4): prefix_sums[i].append(0) for v in times[i]: prefix_sums[i].append(prefix_sums[i][-1] + v) mint = 2000000000.0 + 1 for i in range(min(k + 1, len(prefix_sums[3]))): needs = k - i if len(prefix_sums[1]) > needs and len(prefix_sums[2]) > needs: mint = min( mint, prefix_sums[3][i] + prefix_sums[1][needs] + prefix_sums[2][needs] ) if mint == 2000000000.0 + 1: mint = -1 print(mint)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
for _ in range(1): n, k = map(int, input().split()) al = [] bo = [] com = [] for i in range(n): t, a, b = map(int, input().split()) if a == b == 1: com.append(t) else: if a == 1: al.append(t) if b == 1: bo.append(t) com.sort(reverse=True) al.sort(reverse=True) bo.sort(reverse=True) ans = 0 f = 0 while k != 0: if len(al) > 0 and len(bo) > 0: c = al[-1] + bo[-1] if len(com) > 0 and c > com[-1]: ans += com[-1] com.pop() else: ans += c al.pop() bo.pop() elif len(com) > 0: ans += com[-1] com.pop() else: f = 1 k = 1 k -= 1 if f == 0: print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) a = [] b = [] s = [] ans = 0 for i in range(n): t, x, y = map(int, input().split()) if x == 1 and y == 1: s.append(t) elif x == 1: a.append(t) elif y == 1: b.append(t) s1 = len(s) s2 = len(a) s3 = len(b) if s1 + s3 < k: print(-1) elif s1 + s2 < k: print(-1) else: s.sort() a.sort() b.sort() i = 0 j = 0 v = 0 while k > 0: if s1 == 0: ans += a[i] + b[i] k -= 1 s2 -= 1 s3 -= 1 i += 1 j += 1 elif s2 == 0 or s3 == 0: ans += s[v] v += 1 k -= 1 s1 -= 1 else: k -= 1 if a[i] + b[j] <= s[v]: s2 -= 1 s3 -= 1 ans += a[i] + b[j] i += 1 j += 1 else: ans += s[v] v += 1 s1 -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) x = [] y = [] z = [] for _ in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 1: x.append(t) elif a == 1 and b == 0: y.append(t) elif a == 0 and b == 1: z.append(t) if len(x) >= k or k - len(x) <= len(y) and k - len(x) <= len(z): x.sort() y.sort() z.sort() i = 0 j = 0 ans = 0 while i + j < k: if i < len(x) and j < len(y) and j < len(z): if x[i] < y[j] + z[j]: ans = ans + x[i] i = i + 1 else: ans = ans + y[j] + z[j] j = j + 1 elif i < len(x): ans = ans + x[i] i = i + 1 elif j < len(y) and j < len(z): ans = ans + y[j] + z[j] j = j + 1 print(ans) else: print("-1")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
import sys input = sys.stdin.readline n, k = map(int, input().split()) o, a, b = [], [], [] for i in range(n): q, w, e = map(int, input().split()) if w == e and w: o.append(q) elif w and not e: a.append(q) elif w or e: b.append(q) i = j = l = 0 done = 0 ans = 0 o.sort() a.sort() b.sort() while (i < len(o) or j < len(a) and l < len(b)) and done < k: if i < len(o) and ( j >= len(a) or l >= len(b) or j < len(a) and l < len(b) and o[i] <= a[j] + b[l] ): ans += o[i] i += 1 done += 1 elif l < len(b) and j < len(a): ans += a[j] + b[l] j += 1 l += 1 done += 1 print(ans if done >= k else -1)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
from sys import stdin, stdout input = stdin.readline n, k = map(int, input().split()) com = [] a = [] b = [] for _ in range(n): t, ai, bi = map(int, input().split()) if ai == 1 == bi: com += [t] elif ai == 1: a += [t] elif bi == 1: b += [t] a.sort(reverse=True) b.sort(reverse=True) com.sort(reverse=True) ak, bk = 0, 0 ans = 0 while ak != k and bk != k: if a and b and com: if a[-1] + b[-1] <= com[-1]: ans += a[-1] + b[-1] a.pop() b.pop() else: ans += com[-1] com.pop() ak += 1 bk += 1 elif a and b: ans += a[-1] + b[-1] a.pop() b.pop() ak += 1 bk += 1 elif com: ans += com[-1] com.pop() ak += 1 bk += 1 else: print(-1) break else: print(ans)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR LIST VAR IF VAR NUMBER VAR LIST VAR IF VAR NUMBER VAR LIST VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
book, k = map(int, input().split()) both, alice, bob = [], [], [] for i in range(book): time, x, y = map(int, input().split()) if x == 1 and y == 1: both.append(time) elif x == 1 and y == 0: alice.append(time) elif x == 0 and y == 1: bob.append(time) if len(both) + len(alice) < k or len(both) + len(bob) < k: print(-1) else: both.sort() alice.sort() bob.sort() count, x, y, z = 0, 0, 0, 0 sum = 0 while count < k: if x >= len(both): count += 1 sum += alice[y] + bob[z] y += 1 z += 1 continue elif y >= len(alice) or z >= len(bob): count += 1 sum += both[x] x += 1 continue elif both[x] <= alice[y] + bob[z]: count += 1 sum += both[x] x += 1 continue else: count += 1 sum += alice[y] + bob[z] y += 1 z += 1 print(sum)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) books = [] booksA = [] booksB = [] for _ in range(n): t, a, b = map(int, input().split()) if a == 1 and b == 0: booksA.append(t) elif a == 0 and b == 1: booksB.append(t) elif a == 1 and b == 1: books.append(t) booksA.sort(reverse=True) booksB.sort(reverse=True) books.sort(reverse=True) t = 0 for _ in range(k): if not books: if not booksA or not booksB: t = -1 break else: t += booksA.pop() + booksB.pop() elif (not booksA or not booksB) or books[-1] < booksA[-1] + booksB[-1]: t += books.pop() else: t += booksA.pop() + booksB.pop() print(t)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def main(): n, k = map(int, input().split()) a, b, both = [], [], [] for i in range(n): nt, na, nb = map(int, input().split()) if na == nb == 1: both.append(nt) elif na == 1 and nb == 0: a.append(nt) elif na == 0 and nb == 1: b.append(nt) a.sort() b.sort() for i in range(min(len(a), len(b))): both.append(a[i] + b[i]) both.sort() if len(both) < k: print(-1) else: print(sum(both[:k])) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
def main(): n, k = map(int, input().split()) alice_counter = k bob_counter = k total_time = 0 both = [] alice = [] bob = [] for _ in range(n): t, a, b = map(int, input().split()) if a and b: both.append(t) elif a: alice.append(t) elif b: bob.append(t) both = sorted(both) alice = sorted(alice) bob = sorted(bob) pointer = [0] * 3 while True: if bob_counter == 0 and alice_counter == 0: return total_time if pointer[1] >= len(alice) or pointer[2] >= len(bob): while bob_counter > 0 or alice_counter > 0: if pointer[0] >= len(both): return -1 total_time += both[pointer[0]] pointer[0] += 1 bob_counter -= 1 alice_counter -= 1 return total_time elif pointer[0] >= len(both): while bob_counter > 0 or alice_counter > 0: if pointer[1] >= len(alice) or pointer[2] >= len(bob): return -1 total_time += alice[pointer[1]] + bob[pointer[2]] pointer[1] += 1 pointer[2] += 1 bob_counter -= 1 alice_counter -= 1 return total_time else: if both[pointer[0]] > alice[pointer[1]] + bob[pointer[2]]: total_time += alice[pointer[1]] + bob[pointer[2]] pointer[1] += 1 pointer[2] += 1 else: total_time += both[pointer[0]] pointer[0] += 1 bob_counter -= 1 alice_counter -= 1 print(main())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
ve1 = [] ve2 = [] ve3 = [] s = input() n, k = map(int, s.split(" ")) num1 = 0 num2 = 0 for i in range(0, n): s = input() t, a, b = map(int, s.split(" ")) if a & b: ve3.append(t) num1 = num1 + 1 num2 = num2 + 1 elif a == 0 and b == 1: ve2.append(t) num2 = num2 + 1 elif a == 1 and b == 0: ve1.append(t) num1 = num1 + 1 ve1.sort() ve2.sort() ve3.sort() if num1 < k or num2 < k: print(-1) exit() num = 0 num1 = 0 num2 = 0 Sum = 0 while num < k: if num1 < len(ve1) and num1 < len(ve2) and num2 < len(ve3): if ve1[num1] + ve2[num1] <= ve3[num2]: Sum = Sum + ve1[num1] + ve2[num1] num1 = num1 + 1 else: Sum = Sum + ve3[num2] num2 = num2 + 1 num = num + 1 else: break if num == k: print(Sum) exit() if num2 < len(ve3): while num < k: Sum = Sum + ve3[num2] num2 = num2 + 1 num = num + 1 else: while num < k: Sum = Sum + ve1[num1] + ve2[num1] num1 = num1 + 1 num = num + 1 print(Sum)
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) l1 = [] l2 = [] l3 = [] n1 = n2 = n3 = 0 for i in range(n): a, b, c = map(int, input().split()) if b == c == 1: l1.append(a) n1 += 1 elif b == 1: l2.append(a) n2 += 1 elif c == 1: l3.append(a) n3 += 1 if n1 + n2 < k or n1 + n3 < k: print(-1) else: l1.sort() l2.sort() l3.sort() s1 = s2 = s = 0 p = min(n2, n3) z1 = [] for i in range(p): s1 += l2[i] s1 += l3[i] z1.append(s1) s1 = 0 z2 = z1 + l1 z2.sort() if len(z2) < k: print(-1) else: for i in range(k): s += z2[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are $n$ books in the family library. The $i$-th book is described by three integers: $t_i$ β€” the amount of time Alice and Bob need to spend to read it, $a_i$ (equals $1$ if Alice likes the $i$-th book and $0$ if not), and $b_i$ (equals $1$ if Bob likes the $i$-th book and $0$ if not). So they need to choose some books from the given $n$ books in such a way that: Alice likes at least $k$ books from the chosen set and Bob likes at least $k$ books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of $t_i$ over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. -----Input----- The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$). The next $n$ lines contain descriptions of books, one description per line: the $i$-th line contains three integers $t_i$, $a_i$ and $b_i$ ($1 \le t_i \le 10^4$, $0 \le a_i, b_i \le 1$), where: $t_i$ β€” the amount of time required for reading the $i$-th book; $a_i$ equals $1$ if Alice likes the $i$-th book and $0$ otherwise; $b_i$ equals $1$ if Bob likes the $i$-th book and $0$ otherwise. -----Output----- If there is no solution, print only one integer -1. Otherwise print one integer $T$ β€” the minimum total reading time of the suitable set of books. -----Examples----- Input 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 Output 18 Input 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 Output 8 Input 5 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 Output -1
n, k = map(int, input().split()) both_like = [] alice_like = [] bob_like = [] for i in range(n): t, a, b = map(int, input().split()) if a and b: both_like.append((t, a, b)) elif a: alice_like.append((t, a, b)) elif b: bob_like.append((t, a, b)) alice_like.sort() bob_like.sort() for i in range(min(len(alice_like), len(bob_like))): both_like.append((alice_like[i][0] + bob_like[i][0], 1, 1)) both_like.sort() if len(both_like) < k: print(-1) else: ans = 0 for i in range(k): ans += both_like[i][0] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR