結果

問題 No.2844 Birthday Party Decoration
ユーザー 👑 kakel-sankakel-san
提出日時 2024-08-23 21:24:59
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 8,077 ms
コンパイル使用メモリ 168,392 KB
実行使用メモリ 194,016 KB
最終ジャッジ日時 2024-08-23 21:25:20
合計ジャッジ時間 9,117 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
31,360 KB
testcase_01 AC 100 ms
34,432 KB
testcase_02 AC 86 ms
34,688 KB
testcase_03 AC 89 ms
34,944 KB
testcase_04 AC 99 ms
194,016 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (99 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 t = NN;
        var ans = new long[t];
        for (var u = 0; u < t; ++u)
        {
            var p = ReadLine().Split();
            var (n, x) = (int.Parse(p[0]), long.Parse(p[1]));
            var c = NList;
            ans[u] = Shop(n, x, c);
        }
        WriteLine(string.Join("\n", ans));
    }
    static long Shop(int n, long x, int[] c)
    {
        var list = new List<(long l, long r)>();
        var max = x;
        for (var i = 0; i < n; ++i)
        {
            var lx = x;
            if (((lx >> c[i]) & 1) == 0)
            {
                for (var j = 0; j < c[i]; ++j) if (((lx >> j) & 1) != 0) lx ^= 1L << j;
                --lx;
            }
            var rx = x;
            if (((rx >> c[i]) & 1) == 0)
            {
                rx ^= 1L << c[i];
                for (var j = 0; j < c[i]; ++j) if (((rx >> j) & 1) != 0) rx ^= 1L << j;
            }
            if (lx >= 0 && lx != rx) list.Add((lx, rx));
            else max = Math.Max(max, rx);
        }
        if (list.Count == 0) return (max - x) * 2;
        list.Sort((l, r) => r.l.CompareTo(l.l));
        var ans = (max - list[^1].l) * 2;
        while (list.Count > 0)
        {
            max = Math.Max(max, list[^1].r);
            list.RemoveAt(list.Count - 1);
            if (list.Count > 0) ans = Math.Min(ans, (max - list[^1].l) * 2);
            else ans = Math.Min(ans, (max - x) * 2);
        }
        return ans;
    }
}
0