結果

問題 No.2696 Sign Creation
ユーザー 👑 kakel-sankakel-san
提出日時 2024-03-22 22:45:49
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 4,446 bytes
コンパイル時間 11,599 ms
コンパイル使用メモリ 166,888 KB
実行使用メモリ 71,188 KB
最終ジャッジ日時 2024-05-17 18:16:11
合計ジャッジ時間 16,880 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
33,920 KB
testcase_01 AC 62 ms
38,044 KB
testcase_02 AC 63 ms
30,592 KB
testcase_03 AC 64 ms
30,848 KB
testcase_04 AC 63 ms
30,592 KB
testcase_05 AC 63 ms
30,592 KB
testcase_06 WA -
testcase_07 AC 63 ms
30,848 KB
testcase_08 AC 100 ms
33,280 KB
testcase_09 AC 65 ms
30,848 KB
testcase_10 AC 86 ms
35,200 KB
testcase_11 AC 62 ms
30,592 KB
testcase_12 AC 63 ms
30,976 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (100 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 (h, w, n, d) = (c[0], c[1], c[2], c[3]);
        var map = NArr(n);

        var dic = new Dictionary<long, int>();
        var visited = new HashSet<long>();
        for (var i = 0; i < n; ++i)
        {
            var k = Key(map[i][0], map[i][1]);
            dic[k] = i;
            visited.Add(k);
        }

        var uf = new UnionFindTree(n);
        for (var i = 0; i < n; ++i)
        {
            var ix = map[i][0];
            var iy = map[i][1];
            foreach (var pos in Search(ix, iy, h, w, d))
            {
                var pk = Key(pos.x, pos.y);
                if (dic.ContainsKey(pk)) uf.Unite(i, dic[pk]);
            }
        }
        var maxd = 0;
        var add = 0;
        for (var i = 0; i < n; ++i)
        {
            var ix = map[i][0];
            var iy = map[i][1];
            if (uf.GetSize(i) > 1)
            {
                foreach (var pos in Search(ix, iy, h, w, d))
                {
                    var pk = Key(pos.x, pos.y);
                    visited.Add(pk);
                    if (!dic.ContainsKey(pk))
                    {
                        var set = new HashSet<int>();
                        foreach (var p2 in Search(pos.x, pos.y, h, w, d))
                        {
                            var k2 = Key(p2.x, p2.y);
                            if (dic.ContainsKey(k2) && uf.GetSize(dic[k2]) > 1)
                            {
                                set.Add(uf.GetRoot(dic[k2]));
                            }
                        }
                        maxd = Math.Max(maxd, set.Count - 1);
                    }
                }
            }
            else if (add == 0)
            {
                foreach (var pos in Search(ix, iy, h, w, 1))
                {
                    var pk = Key(pos.x, pos.y);
                    var flg = true;
                    foreach (var p2 in Search(pos.x, pos.y, h, w, d))
                    {
                        var k2 = Key(p2.x, p2.y);
                        if (dic.ContainsKey(k2) && uf.GetSize(dic[k2]) > 1)
                        {
                            flg = false;
                            break;
                        }
                    }
                    if (flg) add = 1;
                    break;
                }
            }
        }
        var ans = 0;
        for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i && uf.GetSize(i) > 1) ++ans;
        WriteLine($"{ans - maxd} {ans + add}");
    }
    static IEnumerable<(int x, int y)> Search(int sx, int sy, int h, int w, int d)
    {
        for (var i = Math.Max(1, sx - d); i <= Math.Min(h, sx + d); ++i)
        {
            var di = Math.Abs(sx - i);
            for (var j = Math.Max(1, sy - d + di); j <= Math.Min(w, sy + d - di); ++j)
            {
                if (i != sx || j != sy) yield return (i, j);
            }
        }
    }
    static long Key(int x, int y)
    {
        return x * 1_000_000L + y;
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0