結果

問題 No.1508 Avoid being hit
ユーザー kakel-san
提出日時 2024-06-24 23:22:24
言語 C#
(.NET 8.0.404)
結果
AC  
実行時間 240 ms / 3,000 ms
コード長 2,781 bytes
コンパイル時間 12,918 ms
コンパイル使用メモリ 168,432 KB
実行使用メモリ 188,512 KB
最終ジャッジ日時 2024-06-24 23:22:46
合計ジャッジ時間 21,394 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /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();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var a = NList;
        var b = NList;
        var ans = Move(n, q, a, b);
        WriteLine(ans);
    }
    static string Move(int n, int q, int[] _a, int[] _b)
    {
        var a = _a.Select(ai => ai - 1).ToArray();
        var b = _b.Select(bi => bi - 1).ToArray();
        var dp = Enumerable.Repeat(true, n).ToArray();
        var cand = new List<int>();
        var moves = new List<(int from, int to)>[q];
        for (var i = 0; i < moves.Length; ++i) moves[i] = new List<(int from, int to)>();
        for (var i = 0; i < q; ++i)
        {
            var ncand = new List<int>();
            foreach (var ci in cand)
            {
                if (dp[ci] || a[i] == ci || b[i] == ci) continue;
                if (ci > 0 && dp[ci - 1])
                {
                    moves[i].Add((ci - 1, ci));
                    ncand.Add(ci - 1);
                    if (ci + 1 < n) ncand.Add(ci + 1);
                }
                else if (ci + 1 < n && dp[ci + 1])
                {
                    moves[i].Add((ci + 1, ci));
                    ncand.Add(ci + 1);
                    if (ci > 0) ncand.Add(ci - 1);
                }
            }
            foreach (var mi in moves[i]) dp[mi.to] = true;
            dp[a[i]] = false;
            ncand.Add(a[i]);
            dp[b[i]] = false;
            ncand.Add(b[i]);
            cand = ncand.Distinct().ToList();
        }
        if (dp.Any(f => f))
        {
            var ans = new List<int>();
            var pos = 0;
            for (var i = 0; i < dp.Length; ++i) if (dp[i])
            {
                pos = i;
                break;
            }
            ans.Add(pos);
            for (var i = q - 1; i >= 0; --i)
            {
                for (var j = 0; j < moves[i].Count; ++j) if (moves[i][j].to == pos)
                {
                    pos = moves[i][j].from;
                    break;
                }
                ans.Add(pos);
            }
            ans.Reverse();
            return $"YES\n{string.Join("\n", ans.Select(ai => ai + 1))}";
        }
        else
        {
            return "NO";
        }
    }
}
0