結果

問題 No.2741 Balanced Choice
ユーザー 👑 kakel-sankakel-san
提出日時 2024-05-05 23:32:27
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 4,004 bytes
コンパイル時間 19,134 ms
コンパイル使用メモリ 166,712 KB
実行使用メモリ 211,124 KB
最終ジャッジ日時 2024-05-05 23:32:50
合計ジャッジ時間 12,746 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 155 ms
55,296 KB
testcase_01 AC 170 ms
55,424 KB
testcase_02 AC 195 ms
55,552 KB
testcase_03 AC 149 ms
55,424 KB
testcase_04 AC 152 ms
55,680 KB
testcase_05 AC 52 ms
30,592 KB
testcase_06 AC 50 ms
30,592 KB
testcase_07 AC 122 ms
55,168 KB
testcase_08 AC 139 ms
55,424 KB
testcase_09 AC 124 ms
54,784 KB
testcase_10 AC 124 ms
55,424 KB
testcase_11 AC 167 ms
211,124 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (141 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.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();
    }
    static void Solve()
    {
        var c = NList;
        var (n, w, d) = (c[0], c[1], c[2]);
        var map = NArr(n);
        WriteLine(Choice(n, w, d, map));
    }
    static long Choice(int n, int w, int d, int[][] map)
    {
        var dp0 = Enumerable.Repeat(long.MinValue / 2, w + 1).ToArray();
        dp0[0] = 0;
        var dp1 = (long[]) dp0.Clone();
        for (var i = 0; i < n; ++i)
        {
            if (map[i][0] == 0)
            {
                var ndp = (long[]) dp0.Clone();
                for (var j = w; j >= 0; --j) if (j + map[i][1] <= w) ndp[j + map[i][1]] = Math.Max(ndp[j + map[i][1]], dp0[j] + map[i][2]);
                dp0 = ndp;
            }
            else
            {
                var ndp = (long[]) dp1.Clone();
                for (var j = w; j >= 0; --j) if (j + map[i][1] <= w) ndp[j + map[i][1]] = Math.Max(ndp[j + map[i][1]], dp1[j] + map[i][2]);
                dp1 = ndp;
            }
        }
        var seg = new SegmentTree<long>(w + 1, new SegOp());
        for (var i = 0; i <= w; ++i) seg[i] = dp1[i];
        var ans = 0L;
        for (var i = 0; i < dp0.Length; ++i)
        {
            var min = Math.Max(0, i - d);
            var max = Math.Min(w - i, i + d);
            if (min <= max)
            {
                ans = Math.Max(ans, dp0[i] + seg.Prod(min, max + 1));
            }
        }
        return ans;
    }
    class SegOp : ISegmentTreeOperator<long>
    {
        public long Identity => long.MinValue / 2;

        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];
    }
}
0