結果

問題 No.2696 Sign Creation
ユーザー 👑 kakel-sankakel-san
提出日時 2024-03-22 23:42:32
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 1,195 ms / 2,500 ms
コード長 4,395 bytes
コンパイル時間 7,011 ms
コンパイル使用メモリ 167,296 KB
実行使用メモリ 189,740 KB
最終ジャッジ日時 2024-05-17 18:21:25
合計ジャッジ時間 14,097 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
30,964 KB
testcase_01 AC 54 ms
30,464 KB
testcase_02 AC 55 ms
32,668 KB
testcase_03 AC 55 ms
30,336 KB
testcase_04 AC 55 ms
30,720 KB
testcase_05 AC 54 ms
30,848 KB
testcase_06 AC 54 ms
30,712 KB
testcase_07 AC 55 ms
30,592 KB
testcase_08 AC 70 ms
32,512 KB
testcase_09 AC 60 ms
30,976 KB
testcase_10 AC 67 ms
33,408 KB
testcase_11 AC 56 ms
30,720 KB
testcase_12 AC 55 ms
30,720 KB
testcase_13 AC 626 ms
63,000 KB
testcase_14 AC 253 ms
63,040 KB
testcase_15 AC 222 ms
57,440 KB
testcase_16 AC 484 ms
56,180 KB
testcase_17 AC 57 ms
30,976 KB
testcase_18 AC 109 ms
50,244 KB
testcase_19 AC 236 ms
65,408 KB
testcase_20 AC 57 ms
30,976 KB
testcase_21 AC 193 ms
62,208 KB
testcase_22 AC 103 ms
50,224 KB
testcase_23 AC 215 ms
63,268 KB
testcase_24 AC 222 ms
62,744 KB
testcase_25 AC 274 ms
70,144 KB
testcase_26 AC 66 ms
35,584 KB
testcase_27 AC 112 ms
61,192 KB
testcase_28 AC 128 ms
66,044 KB
testcase_29 AC 58 ms
31,112 KB
testcase_30 AC 58 ms
31,600 KB
testcase_31 AC 1,195 ms
65,272 KB
testcase_32 AC 54 ms
30,848 KB
testcase_33 AC 53 ms
30,592 KB
testcase_34 AC 55 ms
30,816 KB
testcase_35 AC 55 ms
30,592 KB
testcase_36 AC 54 ms
30,976 KB
testcase_37 AC 54 ms
31,092 KB
testcase_38 AC 54 ms
30,720 KB
testcase_39 AC 56 ms
30,464 KB
testcase_40 AC 55 ms
189,740 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (85 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);
        WriteLine(Stars(h, w, n, d, map));
    }
    static string Stars(int h, int w, int n, int d, int[][] map)
    {
        var dic = new Dictionary<long, int>();
        for (var i = 0; i < n; ++i)
        {
            var k = Key(map[i][0], map[i][1]);
            dic[k] = i;
        }

        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 newstar = new Dictionary<long, Data>();
        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 k = Key(pos.x, pos.y);
                if (dic.ContainsKey(k)) continue;
                if (!newstar.ContainsKey(k)) newstar[k] = new Data();
                if (uf.GetSize(i) == 1) newstar[k].Singles.Add(i);
                else newstar[k].Signs.Add(uf.GetRoot(i));
            }
        }
        var sum = 0;
        for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i && uf.GetSize(i) > 1) ++sum;
        var min = int.MaxValue;
        var max = 0;
        foreach (var s in newstar)
        {
            var sub = sum;
            if (s.Value.Signs.Count > 0)
            {
                sub -= s.Value.Signs.Count - 1;
            }
            else if (s.Value.Singles.Count > 0)
            {
                ++sub;
            }
            min = Math.Min(min, sub);
            max = Math.Max(max, sub);
        }
        var flg = false;
        for (var i = 1; i <= h; ++i)
        {
            for (var j = 1; j <= w; ++j)
            {
                var k = Key(i, j);
                if (!dic.ContainsKey(k) && !newstar.ContainsKey(k))
                {
                    min = Math.Min(min, sum);
                    max = Math.Max(max, sum);
                    flg = true;
                    break;
                }
            }
            if (flg) break;
        }
        return $"{min} {max}";
    }
    class Data
    {
        public HashSet<int> Singles = new HashSet<int>();
        public HashSet<int> Signs = new HashSet<int>();
    }
    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