結果

問題 No.332 数列をプレゼントに
ユーザー noriocnorioc
提出日時 2016-02-15 14:51:20
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 376 ms / 2,000 ms
コード長 3,300 bytes
コンパイル時間 5,594 ms
コンパイル使用メモリ 111,072 KB
実行使用メモリ 24,004 KB
最終ジャッジ日時 2023-08-25 21:51:07
合計ジャッジ時間 12,811 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,876 KB
testcase_01 AC 60 ms
21,888 KB
testcase_02 AC 63 ms
23,896 KB
testcase_03 AC 61 ms
19,804 KB
testcase_04 AC 63 ms
21,848 KB
testcase_05 AC 62 ms
21,864 KB
testcase_06 AC 61 ms
21,852 KB
testcase_07 AC 218 ms
21,896 KB
testcase_08 AC 64 ms
21,900 KB
testcase_09 AC 61 ms
21,900 KB
testcase_10 AC 84 ms
21,948 KB
testcase_11 AC 65 ms
21,880 KB
testcase_12 AC 63 ms
21,972 KB
testcase_13 AC 63 ms
23,868 KB
testcase_14 AC 65 ms
21,972 KB
testcase_15 AC 71 ms
19,784 KB
testcase_16 AC 68 ms
21,924 KB
testcase_17 AC 64 ms
23,856 KB
testcase_18 AC 81 ms
21,928 KB
testcase_19 AC 65 ms
21,968 KB
testcase_20 AC 68 ms
22,296 KB
testcase_21 AC 67 ms
19,848 KB
testcase_22 AC 67 ms
24,004 KB
testcase_23 AC 96 ms
21,920 KB
testcase_24 AC 321 ms
23,836 KB
testcase_25 AC 76 ms
21,900 KB
testcase_26 AC 142 ms
21,756 KB
testcase_27 AC 63 ms
21,812 KB
testcase_28 AC 79 ms
23,924 KB
testcase_29 AC 85 ms
23,852 KB
testcase_30 AC 68 ms
21,744 KB
testcase_31 AC 82 ms
21,768 KB
testcase_32 AC 96 ms
19,804 KB
testcase_33 AC 293 ms
23,884 KB
testcase_34 AC 360 ms
21,808 KB
testcase_35 AC 336 ms
23,792 KB
testcase_36 AC 63 ms
21,956 KB
testcase_37 AC 321 ms
23,860 KB
testcase_38 AC 330 ms
23,744 KB
testcase_39 AC 194 ms
21,876 KB
testcase_40 AC 352 ms
21,896 KB
testcase_41 AC 85 ms
21,880 KB
testcase_42 AC 370 ms
23,784 KB
testcase_43 AC 376 ms
21,908 KB
testcase_44 AC 67 ms
23,960 KB
testcase_45 AC 341 ms
21,920 KB
testcase_46 AC 342 ms
21,800 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, long x) {
        long sum = 0;
        for (int i = 0; i < ar.Length; i++) {
            int p = es.IndexOf(ar[i]);
            if (p == -1) {
                Console.Write("x");
            }
            else {
                sum += ar[i];
                Console.Write("o");
                es.RemoveAt(p);
            }
        }
        Console.WriteLine();

        if (sum != x) {
            Console.WriteLine("sum が一致しません");
            Console.WriteLine("x  : {0}", x);
            Console.WriteLine("sum: {0}", sum);
        }
        if (es.Count > 0) {
            Console.WriteLine("es が空ではありません");
            Console.WriteLine(string.Join(" ", es));
        }
    }

    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), x);
                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, x);
                    return;
                }
            }
        }

        Console.WriteLine("No");
    }

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

        Calc(x, xs);
    }
}
0