結果

問題 No.2161 Black Market
ユーザー 👑 kakel-sankakel-san
提出日時 2023-02-05 13:39:13
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 456 ms / 7,000 ms
コード長 3,309 bytes
コンパイル時間 7,052 ms
コンパイル使用メモリ 144,176 KB
実行使用メモリ 165,532 KB
最終ジャッジ日時 2023-09-17 06:13:54
合計ジャッジ時間 14,558 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
29,680 KB
testcase_01 AC 75 ms
29,664 KB
testcase_02 AC 78 ms
29,980 KB
testcase_03 AC 74 ms
29,788 KB
testcase_04 AC 74 ms
29,936 KB
testcase_05 AC 73 ms
29,856 KB
testcase_06 AC 78 ms
29,904 KB
testcase_07 AC 77 ms
29,736 KB
testcase_08 AC 78 ms
31,744 KB
testcase_09 AC 78 ms
29,868 KB
testcase_10 AC 79 ms
32,332 KB
testcase_11 AC 78 ms
30,244 KB
testcase_12 AC 78 ms
30,004 KB
testcase_13 AC 77 ms
29,908 KB
testcase_14 AC 77 ms
29,972 KB
testcase_15 AC 79 ms
29,924 KB
testcase_16 AC 79 ms
30,140 KB
testcase_17 AC 78 ms
29,740 KB
testcase_18 AC 73 ms
29,624 KB
testcase_19 AC 78 ms
29,984 KB
testcase_20 AC 200 ms
41,640 KB
testcase_21 AC 200 ms
41,560 KB
testcase_22 AC 214 ms
41,836 KB
testcase_23 AC 215 ms
41,876 KB
testcase_24 AC 454 ms
76,256 KB
testcase_25 AC 316 ms
88,876 KB
testcase_26 AC 456 ms
88,864 KB
testcase_27 AC 288 ms
91,084 KB
testcase_28 AC 284 ms
88,992 KB
testcase_29 AC 127 ms
45,516 KB
testcase_30 AC 87 ms
32,288 KB
testcase_31 AC 288 ms
73,588 KB
testcase_32 AC 276 ms
90,892 KB
testcase_33 AC 98 ms
37,084 KB
testcase_34 AC 110 ms
39,352 KB
testcase_35 AC 115 ms
37,976 KB
testcase_36 AC 96 ms
35,592 KB
testcase_37 AC 85 ms
32,228 KB
testcase_38 AC 143 ms
46,188 KB
testcase_39 AC 86 ms
165,532 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 121 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;
            }
            if (l - li.cost < 0) continue;
            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