結果

問題 No.2686 商品券の使い道
ユーザー 👑 kakel-sankakel-san
提出日時 2024-03-23 20:14:34
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 2,965 ms / 3,000 ms
コード長 9,550 bytes
コンパイル時間 7,718 ms
コンパイル使用メモリ 158,308 KB
実行使用メモリ 45,988 KB
最終ジャッジ日時 2024-03-23 20:15:31
合計ジャッジ時間 54,438 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
33,444 KB
testcase_01 AC 80 ms
32,676 KB
testcase_02 AC 141 ms
39,972 KB
testcase_03 AC 144 ms
40,100 KB
testcase_04 AC 143 ms
40,100 KB
testcase_05 AC 142 ms
40,100 KB
testcase_06 AC 136 ms
40,100 KB
testcase_07 AC 135 ms
40,100 KB
testcase_08 AC 140 ms
39,972 KB
testcase_09 AC 142 ms
39,972 KB
testcase_10 AC 143 ms
39,972 KB
testcase_11 AC 612 ms
39,972 KB
testcase_12 AC 170 ms
40,100 KB
testcase_13 AC 612 ms
40,100 KB
testcase_14 AC 410 ms
40,100 KB
testcase_15 AC 241 ms
39,972 KB
testcase_16 AC 450 ms
39,972 KB
testcase_17 AC 207 ms
39,972 KB
testcase_18 AC 251 ms
39,972 KB
testcase_19 AC 249 ms
39,972 KB
testcase_20 AC 611 ms
39,972 KB
testcase_21 AC 600 ms
39,972 KB
testcase_22 AC 149 ms
39,972 KB
testcase_23 AC 143 ms
39,972 KB
testcase_24 AC 472 ms
39,972 KB
testcase_25 AC 139 ms
40,100 KB
testcase_26 AC 231 ms
40,100 KB
testcase_27 AC 575 ms
40,100 KB
testcase_28 AC 139 ms
40,100 KB
testcase_29 AC 309 ms
45,860 KB
testcase_30 AC 2,517 ms
45,860 KB
testcase_31 AC 2,300 ms
45,732 KB
testcase_32 AC 2,284 ms
45,732 KB
testcase_33 AC 2,921 ms
45,860 KB
testcase_34 AC 590 ms
45,476 KB
testcase_35 AC 2,491 ms
45,860 KB
testcase_36 AC 1,965 ms
45,476 KB
testcase_37 AC 2,043 ms
45,860 KB
testcase_38 AC 2,511 ms
45,860 KB
testcase_39 AC 2,437 ms
45,988 KB
testcase_40 AC 2,090 ms
45,732 KB
testcase_41 AC 240 ms
45,732 KB
testcase_42 AC 1,925 ms
45,732 KB
testcase_43 AC 2,044 ms
45,860 KB
testcase_44 AC 2,551 ms
45,988 KB
testcase_45 AC 2,965 ms
45,860 KB
testcase_46 AC 2,334 ms
45,988 KB
evil_random20_1.txt AC 204 ms
45,860 KB
evil_random20_2.txt AC 222 ms
45,732 KB
evil_random20_3.txt AC 207 ms
195,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (98 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    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();
        // Test();
    }
    static void Test()
    {
        var r = new Random();
        var count = 0;
        while (true)
        {
            var n = 7;
            var p = r.Next(100) + 1;
            var q = r.Next(100) + 1;
            var map = new int[n][];
            for (var i = 0; i < n; ++i)
            {
                map[i] = new int[] { r.Next(20) + 1, r.Next(20) + 1 };
            }
            // 愚直解
            var s1 = All(n, p, q, map);
            // 作成解
            var s2 = Ticket(n, p, q, map);
            if (s1 != s2)
            {
                WriteLine($"{n} {p} {q}");
                WriteLine(string.Join("\n", map.Select(m => string.Join(" ", m))));
                WriteLine(s1);
                WriteLine(s2);
                return;
            }
            ++count;
            if (count % 1000 == 0) WriteLine(count);
        }
    }
    static long All(int n, int p, int q, int[][] map)
    {
        var bitmax = (int)Math.Pow(3, n);
        var ans = 0L;
        for (var b = 0; b < bitmax; ++b)
        {
            var x = 0L;
            var y = 0L;
            var v = 0L;
            var tmp = b;
            for (var i = 0; i < n; ++i)
            {
                if (tmp % 3 == 1)
                {
                    x += map[i][0];
                    v += map[i][1];
                }
                else if (tmp % 3 == 2)
                {
                    y += map[i][0];
                    v += map[i][1];
                }
                tmp /= 3;
            }
            if (x <= p && y <= q) ans = Math.Max(ans, v);
        }
        return ans;
    }
    static void Solve()
    {
        var c = NList;
        var (n, p, q) = (c[0], c[1], c[2]);
        var map = NArr(n);
        WriteLine(Ticket(n, p, q, map));
    }
    static long Ticket(int n, int p, int q, int[][] map)
    {
        if (n == 1)
        {
            return map[0][0] <= p || map[0][0] <= q ? map[0][1] : 0;
        }
        var bitmax = (int)Math.Pow(3, n / 2);
        var list = new List<(long x, long y, long v)>();
        for (var b = 0; b < bitmax; ++b)
        {
            var x = 0L;
            var y = 0L;
            var v = 0L;
            var tmp = b;
            for (var i = 0; i < n / 2; ++i)
            {
                if (tmp % 3 == 1)
                {
                    x += map[i][0];
                    v += map[i][1];
                }
                else if (tmp % 3 == 2)
                {
                    y += map[i][0];
                    v += map[i][1];
                }
                tmp /= 3;
            }
            list.Add((x, y, v));
            // WriteLine($"first {b} {x} {y} {v}");
        }
        list.Sort((l, r) => l.x.CompareTo(r.x));
        var sq = (int)Math.Sqrt(list.Count);
        var dicsize = (list.Count + sq - 1) / sq;
        var dic = new List<(long x, long y, long v)>[dicsize];
        var ylist = new List<long>[dicsize];
        var seglist = new SegmentTree<long>[dicsize];
        var minx = Enumerable.Repeat(long.MaxValue, dicsize).ToArray();
        var maxx = new long[dicsize];
        for (var i = 0; i < dic.Length; ++i)
        {
            dic[i] = new List<(long x, long y, long v)>();
        }
        for (var i = 0; i < list.Count; ++i)
        {
            var idx = i / sq;
            dic[idx].Add(list[i]);
            minx[idx] = Math.Min(minx[idx], list[i].x);
            maxx[idx] = Math.Max(maxx[idx], list[i].x);
        }
        // WriteLine($"max: {string.Join(" ", maxx)}");
        // WriteLine($"min: {string.Join(" ", minx)}");
        for (var i = 0; i < dic.Length; ++i)
        {
            var yset = new HashSet<long>{ 0 };
            foreach (var di in dic[i]) yset.Add(di.y);
            ylist[i] = new List<long>(yset);
            ylist[i].Sort();
            seglist[i] = new SegmentTree<long>(yset.Count, new SegOp());
            for (var j = 0; j < dic[i].Count; ++j)
            {
                var pos = LowerBound(0, dic[i][j].y, ylist[i]);
                seglist[i][pos] = Math.Max(seglist[i][pos], dic[i][j].v);
            }
        }
        var ans = 0L;
        bitmax = (int)Math.Pow(3, n - n / 2);
        for (var b = 0; b < bitmax; ++b)
        {
            var x = 0L;
            var y = 0L;
            var v = 0L;
            var tmp = b;
            for (var i = n / 2; i < n; ++i)
            {
                if (tmp % 3 == 1)
                {
                    x += map[i][0];
                    v += map[i][1];
                }
                else if (tmp % 3 == 2)
                {
                    y += map[i][0];
                    v += map[i][1];
                }
                tmp /= 3;
            }
            if (x > p || y > q) continue;
            // WriteLine($"second {b} {x} {y} {v}");
            for (var i = 0; i < dicsize; ++i)
            {
                // if (b + 1 == bitmax) WriteLine($"dic{i} max:{maxx[i]}, min:{minx[i]}");
                if (x + maxx[i] <= p)
                {
                    var pos = Math.Min(ylist[i].Count - 1, LowerBound(0, q - y, ylist[i]));
                    if (q - y < ylist[i][pos]) --pos;
                    ans = Math.Max(ans, v + seglist[i].Prod(0, pos + 1));
                    // WriteLine($"x<={x}+{maxx[i]}, y<={y}+{ylist[i][pos]}, v={v}+{seglist[i].Prod(0, pos+1)}");
                    // if (b + 1 == bitmax)
                    // {
                    //     WriteLine($"pos {q - y} in list {string.Join(" ", ylist[i])} = {pos}");
                    //     for (var j = 0; j < ylist[i].Count; ++j) Write($"{seglist[i][j]} ");
                    //     WriteLine();
                    // }
                }
                else if (x + minx[i] <= p)
                {
                    for (var j = 0; j < dic[i].Count; ++j)
                    {
                        if (x + dic[i][j].x <= p && y + dic[i][j].y <= q) ans = Math.Max(ans, v + dic[i][j].v);
                        // if (b + 1 == bitmax) WriteLine($"j: {j}, v={dic[i][j].v}");
                        // WriteLine($"x={x}+{dic[i][j].x}, y={y}+{dic[i][j].y}, v={v}+{dic[i][j].v} {x + dic[i][j].x <= p && y + dic[i][j].y <= q}");
                    }
                }
            }
        }
        return ans;
    }
    class SegOp : ISegmentTreeOperator<long>
    {
        public long Identity => 0L;
        public long Operate(long x, long y)
        {
            return Math.Max(x, y);
        }
    }
    interface ISegmentTreeOperator<T>
    {
        T Identity { get; }
        T Operate(T x, T y);
    }
    class SegmentTree<T>
    {
        int size;
        int log;
        T[] d;
        ISegmentTreeOperator<T> op;
        void Update(int k)
        {
            d[k] = op.Operate(d[2 * k], d[2 * k + 1]);
        }
        public SegmentTree(int n, ISegmentTreeOperator<T> op)
        {
            this.op = op;
            size = 1;
            while (size < n) size <<= 1;
            log = CountRZero(size);
            d = new T[2 * size];
            for (var i = 0; i < d.Length; ++i) d[i] = op.Identity;
        }
        public SegmentTree(T[] v, ISegmentTreeOperator<T> op)
        {
            this.op = op;
            size = 1;
            while (size < v.Length) size <<= 1;
            log = CountRZero(size);
            d = new T[2 * size];
            for (var i = 0; i < size; ++i) d[i] = op.Identity;
            for (var i = 0; i < v.Length; ++i) d[size + i] = v[i];
            for (var i = size - 1; i >= 1; --i) Update(i);
        }
        int CountRZero(int n)
        {
            var ans = 0;
            while (n % 2 == 0)
            {
                ++ans;
                n >>= 1;
            }
            return ans;
        }
        public T this[int p]
        {
            get { return d[p + size]; }
            set
            {
                p += size;
                d[p] = value;
                for (var i = 1; i <= log; ++i) Update(p >> i);
            }
        }
        public T Prod(int l, int r)
        {
            var sml = op.Identity;
            var smr = op.Identity;
            l += size;
            r += size;
            while (l < r)
            {
                if ((l & 1) != 0) sml = op.Operate(sml, d[l++]);
                if ((r & 1) != 0) smr = op.Operate(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }
            return op.Operate(sml, smr);
        }
        T AllProd() => d[1];
    }
    static int LowerBound(int left, long min, IList<long> list)
    {
        if (list[left] >= min) return left;
        var ng = left;
        var ok = list.Count;
        while (ok - ng > 1)
        {
            var center = (ng + ok) / 2;
            if (list[center] < min) ng = center;
            else ok = center;
        }
        return ok;
    }
}
0