ねむーの日記~AtCoderな日々~

福岡に住むプログラミング好きのブログです!

AtCoder Beginner Contest 123 C - Five Transportations

研究室の後輩のPCの環境構築で一日終わってしまいました、ねむーです。

今回はAtCoder(https://atcoder.jp/contests/abc123)にて開催されました、AtCoder Beginner Contest 123 C問題「C - Five Transportations」の問題と僕との戦闘記です。

0.はじめに

今回も、プログラミング言語C#を使用しています。

1.問題文

AtCoder 社は成長し、2028 年になってついに 6 つの都市 (都市 1 , 2 , 3 , 4 , 5 , 6 ) からなる AtCoder 帝国を作りました!

AtCoder 帝国には 5 つの交通機関があります。

  • 電車:都市 1 から 2 まで 1 分で移動する。 1 つの電車には A 人まで乗ることができる。
  • バス:都市 2 から 3 まで 1 分で移動する。 1 つのバスには B 人まで乗ることができる。
  • タクシー:都市 3 から 4 まで 1 分で移動する。 1 つのタクシーには C 人まで乗ることができる。
  • 飛行機:都市 4 から 5 まで 1 分で移動する。 1 つの飛行機には D 人まで乗ることができる。
  • 船:都市 5 から 6 までを 1 分で移動する。 1 つの船には E 人まで乗ることができる。

それぞれの交通機関は、各整数時刻 ( 0 , 1 , 2 , 3 , . . . ) に、都市から出発します。 いま、 N 人のグループが都市 1 におり、全員都市 6 まで移動したいです。全員が都市 6 に到着するまでに最短で何分かかるでしょうか? なお、乗り継ぎにかかる時間を考える必要はありません

2.制約

  • 1 ≤ N , A , B , C , D , E ≤ 1015
  • 入力中の値はすべて整数である。

3.入力例

  • 入力
10000000007
2
3
5
7
11
  • 出力
5000000008

4.初見の感想

→総移動人数÷最も運搬人数の少ない場所の移動人数で大まかな時間が分かる

→上の割り算で余りがある場合は切り上げの必要がある( 最後の一人を通す回数が必要なため)

  • 型に注意する必要がある

→解答はO(1016)になる可能性があるのでlong型を使う必要があります!(これを見落としていて一回WAになりました…)

5.コードの簡単な解説

        string input = Console.ReadLine();
        long n = long.Parse(input);
        long[] capacity = new long[5];
        input = Console.ReadLine();
        capacity[0] = long.Parse(input);
        input = Console.ReadLine();
        capacity[1] = long.Parse(input);
        input = Console.ReadLine();
        capacity[2] = long.Parse(input);
        input = Console.ReadLine();
        capacity[3] = long.Parse(input);
        input = Console.ReadLine();
        capacity[4] = long.Parse(input);
        Array.Sort(capacity);
        if (n <= capacity[0]) { Console.WriteLine("5"); }
        else { if (n % capacity[0] != 0) { Console.WriteLine(5 + ((n-n % capacity[0]) / capacity[0])); }
        else { Console.WriteLine(4 + (n / capacity[0])); }

6.全コード

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        string input = Console.ReadLine();
        long n = long.Parse(input);
        long[] capacity = new long[5];
        input = Console.ReadLine();
        capacity[0] = long.Parse(input);
        input = Console.ReadLine();
        capacity[1] = long.Parse(input);
        input = Console.ReadLine();
        capacity[2] = long.Parse(input);
        input = Console.ReadLine();
        capacity[3] = long.Parse(input);
        input = Console.ReadLine();
        capacity[4] = long.Parse(input);
        Array.Sort(capacity);
        if (n <= capacity[0]) { Console.WriteLine("5"); }
        else { if (n % capacity[0] != 0) { Console.WriteLine(5 + ((n-n % capacity[0]) / capacity[0])); }
        else { Console.WriteLine(4 + (n / capacity[0])); }
        }
    }
}

7.最後に

初めてコンテスト出場時間内にC問題が解けたので幸せです(^^)