結果

問題 No.545 ママの大事な二人の子供
ユーザー RisenRisen
提出日時 2017-07-16 00:27:32
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 2,465 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 107,904 KB
実行使用メモリ 22,784 KB
最終ジャッジ日時 2024-04-16 21:55:36
合計ジャッジ時間 3,587 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
20,736 KB
testcase_01 AC 36 ms
20,608 KB
testcase_02 AC 36 ms
20,736 KB
testcase_03 AC 37 ms
20,864 KB
testcase_04 AC 29 ms
20,096 KB
testcase_05 AC 36 ms
20,352 KB
testcase_06 AC 37 ms
20,480 KB
testcase_07 AC 36 ms
20,608 KB
testcase_08 AC 37 ms
20,736 KB
testcase_09 AC 36 ms
20,608 KB
testcase_10 AC 37 ms
20,864 KB
testcase_11 AC 37 ms
20,480 KB
testcase_12 AC 36 ms
20,736 KB
testcase_13 AC 37 ms
20,864 KB
testcase_14 AC 37 ms
20,736 KB
testcase_15 AC 37 ms
20,608 KB
testcase_16 AC 37 ms
20,736 KB
testcase_17 AC 37 ms
20,608 KB
testcase_18 AC 38 ms
20,864 KB
testcase_19 AC 39 ms
20,684 KB
testcase_20 AC 42 ms
20,744 KB
testcase_21 AC 29 ms
20,480 KB
testcase_22 AC 67 ms
21,376 KB
testcase_23 AC 101 ms
22,784 KB
testcase_24 AC 61 ms
21,376 KB
testcase_25 AC 96 ms
22,784 KB
testcase_26 AC 100 ms
22,784 KB
testcase_27 AC 91 ms
22,656 KB
testcase_28 AC 102 ms
22,400 KB
testcase_29 AC 96 ms
22,784 KB
testcase_30 AC 94 ms
22,784 KB
testcase_31 AC 94 ms
22,784 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.Specialized;
using System.Linq;

class Solution
{
    static long GetBestValue(long v, long[] array)
    {
        // 2分探索で-vに最も近いものを探す
        int minRange = 0;
        int maxRange = array.Count() - 1;
        while (minRange != maxRange)
        {
            var center = (minRange + maxRange) / 2;
            if (array[center] < -v)
            {
                minRange = center + 1;
            }
            else
            {
                maxRange = center;
            }
        }

        // array[minRange]は-vか-vを超える最小の値
        var best = Math.Abs(v + array[minRange]);
        if (minRange > 0)
        {
            var t = Math.Abs(v + array[minRange - 1]);
            best = Math.Min(best, t);
        }
        return best;
    }

    static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var a = new long[n];
        var b = new long[n];
        for (int i = 0; i < n; i++)
        {
            var vals = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
            a[i] = vals[0];
            b[i] = vals[1];
        }

        if (n == 1)
        {
            Console.WriteLine($"{Math.Min(a[0], b[0])}");
            return;
        }

        // 半分全列挙
        var halfLength = new int[] { n / 2, n - (n / 2) };
        var halfPatterns = halfLength.Select(i => 1 << i).ToArray();
        var half = halfPatterns.Select(i => new long[i]).ToArray();

        for (int i = 0; i < 2; i++)
        {
            int start = i * halfLength[0];
            for (int pattern = 0; pattern < halfPatterns[i]; pattern++)
            {
                var bits = new BitVector32(pattern);
                for (int j = 0; j < halfLength[i]; j++)
                {
                    if (bits[1 << j])
                    {
                        half[i][pattern] += a[start + j];
                    }
                    else
                    {
                        half[i][pattern] -= b[start + j];
                    }
                }
            }
        }

        half[1] = half[1].OrderBy(i => i).ToArray();
        var best = long.MaxValue;
        for (int pattern = 0; pattern < halfPatterns[0]; pattern++)
        {
            var v = half[0][pattern];
            var t = GetBestValue(v, half[1]);
            best = Math.Min(best, t);
        }

        Console.WriteLine(best);
    }
}
0