Accidental Victory (Codeforces 1490E)
Tags: Greedy, binary search

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. 


Sample Inputs:
2
4
1 2 4 3
5
1 1 1 1 1

Sample Outputs:
3
2 3 4 
5
1 2 3 4 5

Solution (Python):
x = int(input())
for _ in range(x):
    num_player = int(input())
    coins = input().split()
    for x in range(len(coins)):
        coins[x] = int(coins[x])
    sorted_coins = sorted(coins)
    start = 0
    first = sorted_coins[0]
    for x in range(1, len(coins)):
        if first < sorted_coins[x]:
            start = x
        first += sorted_coins[x]
    winner = []
    win_count = 0
    for x in range(len(coins)):
        if coins[x] >= sorted_coins[start]:
            winner.append(str(x + 1))
            win_count += 1
    print(win_count)
    print(' '.join(winner))

Solution (Python):
from collections import defaultdict

def main():
    n = int(input())
    l = list(map(int, input().split()))
    d = defaultdict(int)
    d2 = defaultdict(int)
    for i in range(n):
        d[l[i]] += 1
    lst = sorted(d.keys())
    s = d[lst[0]] * lst[0]
    d2[lst[len(lst) - 1]] = 1
    for i in range(1, len(lst)):
        if s >= lst[i]:
            d2[lst[i - 1]] = 1
        s += d[lst[i]] * lst[i]
    ind = 0
    for i in range(len(lst) - 1, -1, -1):
        if d2[lst[i]] == 0:
            ind = lst[i]
            break
    c = 0
    s = ''
    for i in range(n):
        if l[i] > ind:
            c += 1
            s += f'{i + 1} '
    print(c)
    print(s)
for i in range(int(input())):
    main()

Solution (Python):
import sys
input = sys.stdin.readline
t = 1
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    l = sorted(a, reverse=True)
    s = sum(a)
    val = 0
    for i in range(n):
        s -= l[i]
        if s < l[i]:
            val = l[i]
            break
    ans = []
    for i in range(n):
        if a[i] >= val:
            ans.append(i + 1)
    print(len(ans))
    print(*ans)