【Python】AtCoder abc123_c Five Transportations

atcoder.jp

qiita.com

こちらの記事が図入りでかなりわかりやすかった。

要するに下記の公式さえ導けば、それに当てはめるだけで解ける(まあ公式を発見することが大変なのだろうが……)。

最少定員位置までの道の数 + (総人数 / 最少定員) + (5 - (最少定員位置までの道の数 + 1))

(最少定員位置までの道の数 + 1) は最少定員位置までの道の数に最少定員の道の分を加えている。

テストコード

import unittest
import main


class MainTest(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_1(self):
        self.assertEqual(7, main.main(5, 3, 2, 4, 3, 5))

    def test_2(self):
        self.assertEqual(5, main.main(10, 123, 123, 123, 123, 123))


if __name__ == "__main__":
    unittest.main()

提出コード

from math import ceil


def main(N, A, B, C, D, E):
    // 各道の定員を配列にする
    loads = [A, B, C, D, E]
    // 定員の最小値
    min_val = min(loads)
    // 最小値(ボトルネック)の位置
    min_index = loads.index(min_val)

    // 公式に当てはめる
    res = min_index + ceil(N / min_val) + (5 - (min_index + 1))

    return res


if __name__ == "__main__":
    N = int(input())
    A = int(input())
    B = int(input())
    C = int(input())
    D = int(input())
    E = int(input())
    print(main(N, A, B, C, D, E))

配列検索は下記を参考。

techacademy.jp

小数の切り上げは下記を参考。

Pythonでの「四捨五入」「切り捨て」「切り上げ」 | エンジニア足立のコーディング日記