Elections (Codeforces 1267E)
Tags: Greedy, greedy

Byteburg Senate elections are coming. Usually "United Byteland", the ruling Byteland party, takes all the seats in the Senate to ensure stability and sustainable development. But this year there is one opposition candidate in one of the constituencies. Even one opposition member can disturb the stability in the Senate, so the head of the Party asks you to ensure that the opposition candidate will not be elected.There are $$$n$$$ candidates, numbered from 1 to $$$n$$$. Candidate $$$n$$$ is the opposition candidate. There are $$$m$$$ polling stations in the constituency, numbered from 1 to $$$m$$$. You know the number of votes cast for each candidate at each polling station. The only thing you can do to prevent the election of the opposition candidate is to cancel the election results at some polling stations. The opposition candidate will be elected if the sum of the votes cast in their favor at all non-canceled stations will be strictly greater than the analogous sum for every other candidate. Your task is to prevent the election of the opposition candidate by canceling the election results at the minimal possible number of polling stations. Notice that solution always exists, because if you cancel the elections at all polling stations, the number of votes for each candidate will be 0, and the opposition candidate will not be elected.

Input: The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2\le n\le 100$$$; $$$1\le m \le 100$$$) — the number of candidates and the number of polling stations. The next $$$m$$$ lines contain the election results at each polling station with $$$n$$$ numbers on each line. In the $$$i$$$-th line the $$$j$$$-th number is $$$a_{i,j}$$$ — the number of votes cast for the candidate $$$j$$$ at the station $$$i$$$ ($$$0\le a_{i,j} \le 1\,000$$$).

Output: In the first line output integer $$$k$$$ — the minimal number of the polling stations in which you need to cancel the election results. In the second line output $$$k$$$ integers — the indices of canceled polling stations, in any order. If there are multiple ways to cancel results at $$$k$$$ stations, output any one of them.

Note: In the first example, the candidates from 1 to 5 received 14, 12, 13, 15, and 24 votes correspondingly. The opposition candidate has the most votes. However, if you cancel the election results at the first and the third polling stations, then only the result from the second polling station remains and the vote sums become 3, 7, 5, 6, and 7, without the opposition candidate being in the lead anymore. 

Sample Inputs:
5 3
6 3 4 2 8
3 7 5 6 7
5 2 4 7 9

2 1
1 1

3 3
2 3 8
4 2 9
3 1 7

Sample Outputs:
2
3 1

0

3
1 2 3

Solution (Python):
class Solution:

    def __init__(self, polling):
        self.polling = polling
        self.num_candidate = len(self.polling[0])
        self.num_polling = len(self.polling)

    def minimal_to_beat_opposite(self, c):
        sub_polling = []
        total_c = 0
        total_n = 0
        for i in range(self.num_polling):
            sub_polling.append((self.polling[i][self.num_candidate - 1] - self.polling[i][c], i + 1))
            total_c += self.polling[i][c]
            total_n += self.polling[i][self.num_candidate - 1]
        sub_polling.sort(reverse=True)
        removal = []
        pos = 0
        total_diff = total_n - total_c
        while total_diff > 0:
            (diff, station) = sub_polling[pos]
            total_diff -= diff
            pos += 1
            removal.append(station)
        return removal

    def solve(self):
        minimal_across_board = range(1, self.num_polling + 1)
        for i in range(self.num_candidate - 1):
            minimal = self.minimal_to_beat_opposite(i)
            if len(minimal) < len(minimal_across_board):
                minimal_across_board = minimal
        print(len(minimal_across_board))
        print(' '.join([str(item) for item in minimal_across_board]))
(n, m) = [int(item) for item in input().split()]
polling = []
for i in range(m):
    polling.append([int(item) for item in input().split()])
s = Solution(polling)
s.solve()

Solution (Python):
d = dict()
(n, m) = map(int, input().split())
for i in range(m):
    l = list(map(int, input().split()))
    for j in range(len(l) - 1):
        if j not in d:
            d[j] = [(l[-1] - l[j], i)]
        else:
            d[j].append((l[-1] - l[j], i))
ind = [i + 1 for i in range(m)]
minl = m
for j in range(n - 1):
    temp = []
    d[j].sort(reverse=True)
    s = 0
    for p in range(len(d[j])):
        s += d[j][p][0]
    k = -1
    while s > 0:
        k += 1
        s -= d[j][k][0]
        temp.append(d[j][k][1] + 1)
    if len(temp) < minl:
        ind = temp
        minl = len(temp)
print(len(ind))
print(*ind)

Solution (Python):
(n, m) = [int(x) for x in input().split()]
mat = []
for i in range(m):
    a = [int(x) for x in input().split()]
    a = [a[-1] - x for x in a]
    a[len(a) - 1] = i + 1
    mat.append(a)
minimum = [m, [x for x in range(1, m + 1)]]
prt = False
for i in range(n - 1):
    cnt = 0
    mat.sort(key=lambda x: x[i])
    sums = 0
    for j in range(m):
        sums += mat[j][i]
        if sums <= 0:
            cnt += 1
        elif minimum[0] > m - cnt:
            minimum[0] = m - cnt
            cand = []
            for k in range(j, m):
                cand.append(mat[k][-1])
            minimum[1] = cand
            break
    if cnt == m:
        prt = True
        break
if prt:
    print(0)
else:
    print(minimum[0])
    print(*minimum[1])