結果

問題 No.2161 Black Market
ユーザー 👑 kakel-sankakel-san
提出日時 2023-02-05 13:36:33
言語 C#
(.NET 8.0.203)
結果
RE  
実行時間 -
コード長 3,264 bytes
コンパイル時間 8,876 ms
コンパイル使用メモリ 144,112 KB
実行使用メモリ 172,112 KB
最終ジャッジ日時 2023-09-17 06:11:50
合計ジャッジ時間 21,401 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
29,900 KB
testcase_01 AC 64 ms
29,616 KB
testcase_02 AC 65 ms
29,868 KB
testcase_03 AC 62 ms
29,776 KB
testcase_04 AC 60 ms
29,700 KB
testcase_05 AC 60 ms
29,664 KB
testcase_06 AC 65 ms
29,544 KB
testcase_07 AC 65 ms
29,676 KB
testcase_08 AC 68 ms
29,896 KB
testcase_09 AC 68 ms
30,060 KB
testcase_10 AC 72 ms
32,236 KB
testcase_11 AC 68 ms
30,068 KB
testcase_12 AC 70 ms
32,104 KB
testcase_13 AC 66 ms
29,648 KB
testcase_14 AC 65 ms
30,044 KB
testcase_15 AC 65 ms
30,200 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 69 ms
31,860 KB
testcase_19 AC 70 ms
30,016 KB
testcase_20 AC 180 ms
39,556 KB
testcase_21 AC 189 ms
43,588 KB
testcase_22 RE -
testcase_23 AC 198 ms
43,708 KB
testcase_24 AC 414 ms
90,784 KB
testcase_25 AC 291 ms
90,756 KB
testcase_26 AC 354 ms
87,244 KB
testcase_27 AC 265 ms
90,852 KB
testcase_28 AC 263 ms
92,720 KB
testcase_29 AC 114 ms
43,152 KB
testcase_30 AC 87 ms
32,408 KB
testcase_31 AC 261 ms
73,452 KB
testcase_32 AC 272 ms
90,908 KB
testcase_33 AC 90 ms
37,124 KB
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 118 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, k, l, p) = (c[0], c[1], c[2], c[3]);
        var map = NArr(n);
        WriteLine(Market(n, k, l, p, map));
    }
    static long Market(int n, int k, int l, int p, int[][] map)
    {
        var leftcnt = n / 2;
        var llist = BitList(map.Take(leftcnt).ToList());
        var rlist = BitList(map.Skip(leftcnt).ToList());
        llist.Sort((l, r) => l.val.CompareTo(r.val));
        rlist.Sort((l, r) => r.val.CompareTo(l.val));

        var cset = new HashSet<long>();
        foreach (var li in llist) if (l - li.cost > 0) cset.Add(l - li.cost);
        foreach (var ri in rlist) cset.Add(ri.cost);
        var clist = new List<long>(cset);
        clist.Sort();
        var cdic = new Dictionary<long, int>();
        for (var i = 0; i < clist.Count; ++i) cdic[clist[i]] = i;

        var rightcnt = n - n / 2;
        var ftlist = new FenwickTree[rightcnt + 1];
        for (var i = 0; i < ftlist.Length; ++i) ftlist[i] = new FenwickTree(clist.Count);

        var ans = 0L;
        var cur = 0;
        foreach (var li in llist)
        {
            while (cur < rlist.Count && li.val + rlist[cur].val >= p)
            {
                ftlist[rlist[cur].cnt].Add(cdic[rlist[cur].cost], 1);
                ++cur;
            }
            for (var i = 0; i < ftlist.Length && i + li.cnt <= k; ++i)
            {
                ans += ftlist[i].Sum(cdic[l - li.cost]);
            }
        }
        return ans;
    }
    static List<(int cnt, long cost, long val)> BitList(List<int[]> map)
    {
        var ans = new List<(int cnt, long cost, long val)>();
        var bitmax = 1 << map.Count;
        for (var b = 0; b < bitmax; ++b)
        {
            var tmp = b;
            var cnt = 0;
            var cost = 0L;
            var val = 0L;
            for (var i = 0; i < map.Count; ++i)
            {
                if (tmp % 2 == 1)
                {
                    ++cnt;
                    cost += map[i][0];
                    val += map[i][1];
                }
                tmp >>= 1;
            }
            ans.Add((cnt, cost, val));
        }
        return ans;
    }

    class FenwickTree
    {
        int size;
        long[] tree;
        public FenwickTree(int size)
        {
            this.size = size;
            tree = new long[size + 2];
        }
        public void Add(int index, int value)
        {
            ++index;
            for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
        }
        /// <summary>先頭からindexまでの和(include index)</summary>
        public long Sum(int index)
        {
            ++index;
            var sum = 0L;
            for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
            return sum;
        }
    }
}
0