結果

問題 No.15 カタログショッピング
ユーザー mbanmban
提出日時 2017-07-20 14:34:52
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 3,350 bytes
コンパイル時間 993 ms
コンパイル使用メモリ 116,568 KB
実行使用メモリ 40,916 KB
最終ジャッジ日時 2024-04-17 05:52:07
合計ジャッジ時間 3,077 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
19,200 KB
testcase_01 AC 30 ms
19,072 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 32 ms
19,456 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;

class Program
{
    static void Main()
    {
        new Magatro().Solve();
    }
}

struct S
{
    public bool[] Use;
    public long Price;
    public S(long p, bool[] u)
    {
        Price = p;
        Use = u;
    }
}

class Magatro
{
    private int N;
    private long S;
    private long[] P;

    private List<S> A = new List<S>(), B = new List<S>();
    private StringBuilder Anser = new StringBuilder();

    private void Scan()
    {
        var ns = Console.ReadLine().Split(' ');
        N = int.Parse(ns[0]);
        S = long.Parse(ns[1]);
        P = new long[N];
        for (int i = 0; i < N; i++)
        {
            P[i] = long.Parse(Console.ReadLine());
        }
    }

    private void D(int left, int right, long sum, bool[] use, List<S> t)
    {
        if (left >= right)
        {
            t.Add(new S(sum, use.ToArray()));
            return;
        }
        D(left + 1, right, sum, use, t);
        if (sum + P[left] <= S)
        {
            use[left] = true;
            D(left + 1, right, sum + P[left], use, t);
            use[left] = false;
        }
    }

    private void Search(S s)
    {
        int left1 = 0, right1 = B.Count;
        while (right1 - left1 > 1)
        {
            int mid = (left1 + right1) / 2;
            if (B[mid].Price > S - s.Price)
            {
                right1 = mid;
            }
            else
            {
                left1 = mid;
            }
        }
        int left2 = -1, right2 = B.Count - 1;
        while (right2 - left2 > 1)
        {
            int mid = (left2 + right2) / 2;
            if (B[mid].Price >= S - s.Price)
            {
                right2 = mid;
            }
            else
            {
                left2 = mid;
            }
        }
        if (B[left1].Price == S - s.Price)
        {
            //Console.WriteLine("{2} {0} {1}", left2 + 1, left1,s.Price);
            for (int i = left2 + 1; i <= left1; i++)
            {
                List<int> ans = new List<int>();
                for (int j = 0; j < N; j++)
                {
                    if (s.Use[j] || B[i].Use[j])
                    {
                        ans.Add(j + 1);
                    }
                }
                Anser.AppendLine(string.Join(" ", ans));
            }
        }
    }

    public void Solve()
    {
        Scan();
        D(0, N / 2, 0, new bool[N], A);
        D(N / 2, N, 0, new bool[N], B);
        A.Sort((a, b) =>
        {
            for (int i = 0; i < N; i++)
            {
                if (a.Use[i])
                {
                    return -1;
                }
                if (b.Use[i])
                {
                    return 1;
                }
            }
            return 0;
        });
        B.Sort((a, b) =>
        {
            if (a.Price != b.Price)
            {
                return a.Price.CompareTo(b.Price);
            }
            return Array.IndexOf(a.Use, true).CompareTo(Array.IndexOf(b.Use, true));
        });

        foreach (var s in A)
        {
            Search(s);
        }
        Console.Write(Anser.ToString());
    }
}
0