結果

問題 No.332 数列をプレゼントに
ユーザー noriocnorioc
提出日時 2016-02-15 14:15:55
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,910 bytes
コンパイル時間 3,268 ms
コンパイル使用メモリ 112,444 KB
実行使用メモリ 25,816 KB
最終ジャッジ日時 2023-10-22 05:47:39
合計ジャッジ時間 11,345 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
25,692 KB
testcase_01 AC 34 ms
25,672 KB
testcase_02 AC 34 ms
25,600 KB
testcase_03 AC 32 ms
25,604 KB
testcase_04 AC 36 ms
25,684 KB
testcase_05 WA -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 AC 35 ms
25,672 KB
testcase_10 RE -
testcase_11 WA -
testcase_12 RE -
testcase_13 RE -
testcase_14 WA -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 WA -
testcase_21 RE -
testcase_22 WA -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 WA -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 AC 34 ms
25,600 KB
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 AC 55 ms
25,672 KB
testcase_42 AC 299 ms
25,672 KB
testcase_43 AC 287 ms
25,672 KB
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

class Program {
    static string ReadLine() { return Console.ReadLine(); }
    static int ReadInt() { return int.Parse(ReadLine()); }
    static int[] ReadInts() { return ReadLine().Split().Select(int.Parse).ToArray(); }
    static string[] ReadStrings() { return ReadLine().Split(); }

    static void Output(int[] ar, List<long> es) {
        for (int i = 0; i < ar.Length; i++) {
            int p = es.IndexOf(ar[i]);
            if (p == -1) {
                Console.Write("x");
            }
            else {
                Console.Write("o");
                es.RemoveAt(p);
            }
        }
        Console.WriteLine();
    }

    static void Calc(long x, int[] ar) {
        var xs = ar.Select(e => (long)e).ToList();
        xs.Sort();

        var ys = new List<long>();
        for (int i = 0; i < 20; i++) {
            if (xs.Count == 0) break;
            ys.Add(xs[xs.Count-1]);
            xs.RemoveAt(xs.Count-1);
        }

        long sum = xs.Sum();
        var dp = new bool[xs.Count, sum + 1];
        if (xs.Count > 0)
            dp[0, xs[0]] = true;
        for (int i = 1; i < xs.Count; i++) {
            for (long j = sum - xs[i]; j > 0; j--) {
                if (dp[i-1, j]) {
                    dp[i, j] = true;
                    dp[i, j + xs[i]] = true;
                }
            }
        }

        var es = new List<long>(ys); // ys の選択要素
        for (int i = 1; i < (1 << ys.Count); i++) {
            int p = 0;
            long t = 0;
            for (int j = 0; j < ys.Count; j++) {
                if ((i & (1 << j)) > 0) {
                    t += ys[j];
                    es[p++] = ys[j];
                    if (t > x) break;
                }
            }

            if (t == x) {
                Output(ar, es.GetRange(0, p));
                return;
            }
            else if (t < x) {
                if (xs.Count > 0 && dp[xs.Count-1, x - t]) {
                    Console.WriteLine("found");

                    var es2 = es.GetRange(0, p);
                    long rest = x - t;
                    for (int j = xs.Count-1; j > 0; j--) {
                        if (rest == 0) break;
                        if (j == 0) {
                            if (rest != xs[0]) throw new Exception();
                            es2.Add(rest);
                        }
                        else if (!dp[j-1, rest]) {
                            es2.Add(xs[j]);
                            rest -= xs[j];
                        }
                    }
                    Output(ar, es2);
                    return;
                }
            }
        }

        Console.WriteLine("No");
    }

    static void Main() {
        var ss = ReadStrings();
        long x = long.Parse(ss[1]);
        var xs = ReadInts();

        Calc(x, xs);
    }
}
0