0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

p49 Fence Repair(C#)

Posted at

自分(正解)


using System;

public class Program
{
    public static void Main()
    {
        int N = 3;
        int[] L = { 8, 5, 8 };
        int first_back;
        int second_back;
        int answer = 0;
        List<int> L_list = new List<int>(L);

        while (L_list.Count > 1)
        {
            L_list.Sort();
            L_list.Reverse();

            first_back = L_list[L_list.Count - 1];
            second_back = L_list[L_list.Count - 2];

            answer += first_back + second_back;

            L_list.RemoveAt(L_list.Count - 1);
            L_list.RemoveAt(L_list.Count - 1);
            L_list.Add(answer);
        }
        Console.WriteLine("答え:{0}", answer);
    }
}

解答

今回は合っていたが、貪欲法を考えるときにいかに早くルールに気付けるかが鍵になってきそう。
今回のを例でいうと、「一番短い板の節点とその次に短い板の節点は兄弟といてよい」にあたる。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?