結果

問題 No.545 ママの大事な二人の子供
ユーザー neko_the_shadowneko_the_shadow
提出日時 2018-06-15 20:20:17
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,073 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 108,364 KB
実行使用メモリ 38,408 KB
最終ジャッジ日時 2023-09-13 04:42:57
合計ジャッジ時間 6,860 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
24,868 KB
testcase_01 WA -
testcase_02 AC 73 ms
22,956 KB
testcase_03 WA -
testcase_04 AC 71 ms
22,788 KB
testcase_05 AC 72 ms
22,788 KB
testcase_06 AC 71 ms
22,860 KB
testcase_07 AC 74 ms
24,832 KB
testcase_08 AC 73 ms
22,856 KB
testcase_09 AC 74 ms
22,984 KB
testcase_10 AC 74 ms
22,952 KB
testcase_11 AC 75 ms
24,804 KB
testcase_12 WA -
testcase_13 AC 73 ms
20,776 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 76 ms
22,820 KB
testcase_19 AC 79 ms
23,440 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 141 ms
34,268 KB
testcase_24 AC 104 ms
26,348 KB
testcase_25 AC 138 ms
34,340 KB
testcase_26 AC 139 ms
34,400 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 139 ms
34,292 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

namespace _545
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var inputs = Enumerable.Range(0, n).Select(_ =>
            {
                var line = Console.ReadLine().Split().Select(long.Parse).ToList();
                return Tuple.Create(line[0], line[1]);
            }).ToList();

            // 入力の前半分と後半分で全列挙を行う。
            var len = inputs.Count;
            var diffs1 = CreateDiffs(inputs, 0, len / 2);
            var diffs2 = CreateDiffs(inputs, len / 2, len);

            long best = long.MaxValue;
            foreach (long diff1 in diffs1)
            {
                // diffs2のうち、-diff1に最も近いものを探す(2分探索)
                int low = 0, high = diffs2.Count - 1;
                while (low < high)
                {
                    int mid = (low + high) / 2;
                    if (diffs2[mid] < -diff1)
                    {
                        low = mid + 1;
                    }
                    else
                    {
                        high = mid;
                    }
                }

                long diff2 = diffs2[low];
                best = Math.Min(best, Math.Abs(diff1 + diff2));
            }

            Console.WriteLine(best);
        }

        static List<long> CreateDiffs(List<Tuple<long, long>> inputs, int start, int end)
        {
            var diffs = new HashSet<long>() { 0 };
            for (int i = start; i < end; i++)
            {
                var input = inputs[i];
                var nexts = new HashSet<long>();
                foreach (var diff in diffs)
                {
                    nexts.Add(diff + input.Item1);
                    nexts.Add(diff - input.Item2);
                }
                diffs = nexts;
            }

            var answer = diffs.ToList();
            answer.Sort();
            return answer;
        }

    }
}
0