Ejemplo n.º 1
0
    for i in range(N):
        sum = 0
        for j in range(M):
            sum += buildings[i][j]
        rowsSum.append(sum)
    row, rowError = None, None
    for i in range(N):
        currentRowError = 0
        for j in range(N):
            currentRowError += abs(i - j) * rowsSum[j]
        if row is None or currentRowError < rowError:
            row = i
            rowError = currentRowError
    # print(row, column)
    return rowError + columnError


def task(input, output):
    N, M = [int(x) for x in input.readline().split(' ')]
    assert N > 0 and M > 0
    buildings = []
    for i in range(N):
        row = [int(x) for x in input.readline().split(' ')]
        assert len(row) == M
        buildings.append(row)
    output.write(str(best_distance(M, N, buildings)) + '\n')


launch(task, 'D-lehky.txt')
# launch(task, None)
Ejemplo n.º 2
0
def calculate_exchanges(satellites):
    L = len(satellites)
    requiredShifts = [satellites[i] - i for i in range(L)]
    while True:
        # measured to be fast
        minIndex, maxIndex = requiredShifts.index(
            (min(requiredShifts))), requiredShifts.index((max(requiredShifts)))
        if requiredShifts[maxIndex] - requiredShifts[minIndex] <= L:
            break
        # this doesn't make the solution invalid & provides a better solution
        requiredShifts[minIndex] += L
        requiredShifts[maxIndex] -= L
    return shifts_to_swaps(requiredShifts)


def task(input, output):
    N = int(input.readline())
    assert N > 0
    satellites = [int(x) - 1 for x in input.readline().split(' ')]
    assert len(satellites) == N
    exchanges = calculate_exchanges(satellites)
    output.write(str(len(exchanges)) + '\n')
    for swap in exchanges:
        output.write(str(swap[0] + 1) + ' ' + str(swap[1] + 1) + '\n')
    # print(f'Done {len(exchanges)}')


# launch(task, 'H-tezky.txt')
launch(task, None)
Ejemplo n.º 3
0
            sessionsDoneBefore = sessionsDone

            for team in teams:
                if waitingForSession + team > sessionCapacity:
                    sessionsDone += 1
                    participants += waitingForSession
                    waitingForSession = 0
                    if sessionsDone == sessionsCount:
                        return participants
                waitingForSession += team
            if sessionsDoneBefore == sessionsDone or waitingForSession == sessionCapacity:
                sessionsDone += 1
                participants += waitingForSession
                waitingForSession = 0
                if sessionsDone == sessionsCount:
                    return participants
            memo[waitingForSessionBefore] = (participantsBefore,
                                             sessionsDoneBefore)


def task(input, output):
    R, K, N = [int(x) for x in input.readline().split(' ')]
    assert N > 0 and R > 0 and K > 0
    teams = [int(x) for x in input.readline().split(' ')]
    assert len(teams) == N
    output.write(str(count_participants(R, K, teams)) + '\n')
    print('Done')


launch(task, 'G-lehky.txt')
Ejemplo n.º 4
0
from math import copysign

from ops import launch


def steps(N, plants, greater):
    if N <= 3: return 0
    change_occurred = False
    changes = 0
    for i in range(N):
        if (plants[i] < plants[(i + 1) % N]) != greater:
            if change_occurred:
                return -1
            changes = min(i + 1, N - i - 1)
            change_occurred = True
    return changes


def task(input, output):
    N = int(input.readline())
    plants = [int(x) for x in input.readline().split(' ')]
    assert N == len(plants)
    s = steps(N, plants, True)
    if s == -1:
        s = steps(N, plants, False)
    output.write(str(s) + '\n')


launch(task, 'B-lehky.txt')
Ejemplo n.º 5
0
        if i + 1 == len(diff):
            flip = flip and (not diff[i])
        else:
            flip = flip and diff[i] != diff[i + 1]
        if flip:
            diff[i] = not diff[i]
            dist += 1
    blocks = 1
    for i in range(1, len(diff)):
        if diff[i] != diff[i - 1]:
            blocks += 1
    dist += blocks
    if diff[len(diff) - 1]:
        dist -= 1
    return dist


def task(input, output):
    N = int(input.readline())
    assert N > 0
    firstString = list(input.readline()[:-1])
    secondString = list(input.readline()[:-1])
    # assert len(firstString) == len(secondString) == N

    diff = [firstString[i] == secondString[i] for i in range(len(firstString))]
    output.write(str(distance(diff)) + '\n')


launch(task, 'E-lehky.txt')

Ejemplo n.º 6
0
    seats = [0] * N
    queue = []
    i = 0
    for party in parties:
        heappush(queue, [-party, 1, i])
        i += 1

    for i in range(M):
        try:
            wonSeat = heappop(queue)
        except IndexError:
            print('wut?')
            break
        seats[wonSeat[2]] += 1
        wonSeat[0] = (wonSeat[0] * wonSeat[1]) / (wonSeat[1] + 1)
        wonSeat[1] += 1
        heappush(queue, wonSeat)
    return seats


def task(input, output):
    N, M = [int(x) for x in input.readline().split(' ')]
    assert N > 0 and M > 0
    votes = [int(x) for x in input.readline().split(' ')]
    assert len(votes) == N
    output.write(' '.join([str(x)
                           for x in calculate_seats(N, M, votes)]) + '\n')


launch(task, 'F-lehky.txt')
Ejemplo n.º 7
0
from ops import launch


def dist(planes, a, b):
    return abs(planes[a] - planes[b]) * 20


def find_letter(N, planes):
    if N <= 2: raise Exception()
    letter = None
    letter_save = None
    for i in range(1, N - 1):
        distance_saved = (dist(planes, i - 1, i) +
                          dist(planes, i, i + 1)) - dist(planes, i - 1, i + 1)
        if letter is None or distance_saved > letter_save:
            letter = i
            letter_save = distance_saved
    return letter


def task(input, output):
    N = int(input.readline())
    planes = [int(x) for x in input.readline().split(' ')]
    assert N == len(planes)
    output.write(str(find_letter(N, planes)) + '\n')


launch(task, 'C-lehky.txt')
# launch(task, None)