結果

問題 No.1701 half price
ユーザー bluemeganebluemegane
提出日時 2021-10-09 09:17:05
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,007 ms / 3,000 ms
コード長 1,887 bytes
コンパイル時間 2,021 ms
コンパイル使用メモリ 66,188 KB
実行使用メモリ 204,308 KB
最終ジャッジ日時 2023-10-01 04:09:33
合計ジャッジ時間 5,402 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
22,536 KB
testcase_01 AC 64 ms
22,440 KB
testcase_02 AC 63 ms
22,596 KB
testcase_03 AC 64 ms
22,476 KB
testcase_04 AC 64 ms
24,508 KB
testcase_05 AC 63 ms
22,580 KB
testcase_06 AC 64 ms
20,488 KB
testcase_07 AC 68 ms
24,668 KB
testcase_08 AC 62 ms
24,644 KB
testcase_09 AC 61 ms
24,344 KB
testcase_10 AC 59 ms
22,396 KB
testcase_11 AC 64 ms
22,576 KB
testcase_12 AC 63 ms
22,404 KB
testcase_13 AC 63 ms
22,632 KB
testcase_14 AC 63 ms
22,652 KB
testcase_15 AC 61 ms
22,544 KB
testcase_16 AC 61 ms
22,388 KB
testcase_17 AC 63 ms
22,828 KB
testcase_18 AC 59 ms
22,960 KB
testcase_19 AC 59 ms
22,792 KB
testcase_20 AC 59 ms
22,292 KB
testcase_21 AC 1,007 ms
204,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System.Collections.Generic;
using System.Linq;
using static System.Math;
using System;

public class P
{
    public long sum { get; set; }
    public string x { get; set; }
}

public class Hello
{
    static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var n = int.Parse(line[0]);
        var w = long.Parse(line[1]);
        line = Console.ReadLine().Trim().Split(' ');
        var a = new long[n];
        var b = new long[n];
        for (int i = 0; i < n; i++)
        {
            a[i] = long.Parse(line[i]);
            b[i] = a[i] / 2L;
        }
        getAns(n, w, a, b);
    }
    static string change(string t)
    {
        var res = "";
        foreach (var x in t)
        {
            if (x == '2') res += "1";
            else res += x;
        }
        return res;
    }
    static long w0(int n, long[] a)
    {
        var c = a.Count(x => x == 0);
        return (long)Pow(2, c) - 1L;
    }
    static void getAns(int n, long w, long[] a, long[] b)
    {
        if (w == 0) { Console.WriteLine(w0(n, a)); return; }
        var hs = new HashSet<string>();
        var q = new Queue<P>();
        q.Enqueue(new P { x = "2", sum = a[0] });
        q.Enqueue(new P { x = "1", sum = b[0] });
        q.Enqueue(new P { x = "0", sum = 0 });
        while (q.Count() > 0)
        {
            var t = q.Dequeue();
            if (t.sum > w) continue;
            var tn = t.x.Length;
            if (tn == n)
            {
                if (t.sum == w) hs.Add(change(t.x));
                continue;
            }
            if (t.sum + a[tn] <= w) q.Enqueue(new P { x = t.x + "2", sum = t.sum + a[tn] });
            if (t.sum + b[tn] <= w) q.Enqueue(new P { x = t.x + "1", sum = t.sum + b[tn] });
            q.Enqueue(new P { x = t.x + "0", sum = t.sum });
        }
        Console.WriteLine(hs.Count());
    }
}
0