n個人が川を渡る問題

1336 ワード

ソリューション:
環境:python 3.6+pycharm
コード:
class Solution:
    def __init__(self):
        pass
    def calculate_time(self):
        import numpy as np
        n = int(input("       :"))
        assert (n > 2 & n <= 100000)
        river_time_list = input('          ,    :')
        river_time_list = river_time_list.split(' ')
        for i in range(len(river_time_list)):
            river_time_list[i] = int(river_time_list[i])
            assert (river_time_list[i] > 0 & river_time_list[i] <= 100000)
        print('            =', river_time_list)
        total_time = 0.0
        min = river_time_list[np.argmin(river_time_list)]
        len_ = len([it > 0 for it in river_time_list])
        while len_ >= 4:
            i = np.argmax(river_time_list)
            total_time += river_time_list[i]
            river_time_list[i] = -1
            j = np.argmax(river_time_list)
            river_time_list[j] = -1
            total_time += min
            len_ = len([it for it in river_time_list if it > 0])
        i = np.argmax(river_time_list)
        total_time += river_time_list[i]
        print('      :{}'.format(total_time))
        return total_time

if __name__ == '__main__':
    Solution().calculate_time()