結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
25,684 KB
testcase_01 AC 33 ms
25,656 KB
testcase_02 AC 33 ms
25,592 KB
testcase_03 AC 31 ms
25,600 KB
testcase_04 AC 34 ms
25,680 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 185 ms
25,656 KB
testcase_08 WA -
testcase_09 AC 33 ms
25,656 KB
testcase_10 AC 56 ms
25,656 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 36 ms
25,688 KB
testcase_17 AC 36 ms
25,688 KB
testcase_18 WA -
testcase_19 AC 36 ms
25,688 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 322 ms
25,656 KB
testcase_25 AC 56 ms
25,656 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 57 ms
25,656 KB
testcase_29 AC 60 ms
25,656 KB
testcase_30 WA -
testcase_31 AC 58 ms
25,656 KB
testcase_32 AC 73 ms
25,656 KB
testcase_33 AC 248 ms
25,656 KB
testcase_34 AC 319 ms
25,656 KB
testcase_35 AC 321 ms
25,656 KB
testcase_36 AC 33 ms
25,592 KB
testcase_37 WA -
testcase_38 AC 323 ms
25,656 KB
testcase_39 WA -
testcase_40 AC 329 ms
25,656 KB
testcase_41 AC 56 ms
25,656 KB
testcase_42 AC 325 ms
25,656 KB
testcase_43 AC 312 ms
25,656 KB
testcase_44 WA -
testcase_45 AC 335 ms
25,656 KB
testcase_46 AC 322 ms
25,656 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;

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 = 0; 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 && x - t < dp.GetLength(1) && dp[xs.Count-1, x - t]) {
                    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