結果

問題 No.918 LISGRID
ユーザー EmKjpEmKjp
提出日時 2019-10-26 10:48:36
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 6,273 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 114,896 KB
実行使用メモリ 48,120 KB
最終ジャッジ日時 2023-10-12 03:13:01
合計ジャッジ時間 13,270 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
23,756 KB
testcase_01 AC 155 ms
38,788 KB
testcase_02 AC 92 ms
24,180 KB
testcase_03 AC 108 ms
33,312 KB
testcase_04 AC 95 ms
24,452 KB
testcase_05 AC 171 ms
40,968 KB
testcase_06 AC 175 ms
40,100 KB
testcase_07 AC 249 ms
47,220 KB
testcase_08 AC 158 ms
36,416 KB
testcase_09 AC 187 ms
39,396 KB
testcase_10 AC 188 ms
41,324 KB
testcase_11 AC 186 ms
41,376 KB
testcase_12 AC 193 ms
40,604 KB
testcase_13 AC 172 ms
35,868 KB
testcase_14 AC 221 ms
46,164 KB
testcase_15 AC 241 ms
48,120 KB
testcase_16 AC 127 ms
33,736 KB
testcase_17 AC 133 ms
33,700 KB
testcase_18 AC 130 ms
31,680 KB
testcase_19 AC 87 ms
26,016 KB
testcase_20 AC 151 ms
35,648 KB
testcase_21 AC 94 ms
26,100 KB
testcase_22 AC 85 ms
22,020 KB
testcase_23 AC 121 ms
31,504 KB
testcase_24 AC 72 ms
21,816 KB
testcase_25 AC 69 ms
21,692 KB
testcase_26 AC 68 ms
21,820 KB
testcase_27 AC 68 ms
21,884 KB
testcase_28 AC 69 ms
21,804 KB
testcase_29 AC 69 ms
23,916 KB
testcase_30 AC 69 ms
21,936 KB
testcase_31 AC 69 ms
21,728 KB
testcase_32 AC 69 ms
21,944 KB
testcase_33 AC 68 ms
21,832 KB
testcase_34 AC 71 ms
21,768 KB
testcase_35 AC 75 ms
23,760 KB
testcase_36 AC 70 ms
21,692 KB
testcase_37 AC 150 ms
37,336 KB
testcase_38 AC 123 ms
31,660 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

using E = System.Linq.Enumerable;

internal partial class Solver {
    public void Run() {
        var h = ni();
        var w = ni();
        var a = ni(h);
        var b = ni(w);
        Array.Sort(a);
        Array.Sort(b);
        var adj = E.Range(0, w * h).Select(_ => new List<int>()).ToArray();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j + 1 < w; j++) {
                var p = i * w + j;
                var q = i * w + (j + 1);
                if (j < a[i] - 1) { // increase
                    adj[p].Add(q);
                } else {
                    adj[q].Add(p);
                }
            }
        }

        for (int j = 0; j < w; j++) {
            for (int i = 0; i + 1 < h; i++) {
                var p = i * w + j;
                var q = (i + 1) * w + j;
                if (i < b[j] - 1) { // increase
                    adj[p].Add(q);
                } else {
                    adj[q].Add(p);
                }
            }
        }

        var order = GetTopologicalOrder(adj);

        var ans = new int[h, w];
        int index = 1;
        foreach (var x in order) {
            ans[x / w, x % w] = index++;
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (j > 0) cout.Write(" ");
                cout.Write(ans[i, j]);
            }
            cout.WriteLine();
        }
    }
    static public List<int> GetTopologicalOrder(List<int>[] adj) {
        var n = adj.Length;
        var inDegree = new int[n];
        for (int i = 0; i < n; i++) {
            foreach (var p in adj[i]) {
                inDegree[p]++;
            }
        }
        var qu = new Queue<int>();
        for (int i = 0; i < n; i++) {
            if (inDegree[i] == 0) {
                qu.Enqueue(i);
                qu.Enqueue(1);
            }
        }
        int maxDay = 0;
        var order = new List<int>();
        while (qu.Count > 0) {
            var current = qu.Dequeue();
            var day = qu.Dequeue();
            order.Add(current);
            maxDay = Math.Max(day, maxDay);
            foreach (var next in adj[current]) {
                inDegree[next]--;
                if (inDegree[next] == 0) {
                    qu.Enqueue(next);
                    qu.Enqueue(day + 1);
                }
            }
        }
        if (inDegree.Any(d => d > 0)) return null;
        return order;
    }
}

// PREWRITEN CODE BEGINS FROM HERE
internal partial class Solver : Scanner {
    public static void Main(string[] args) {
#if LOCAL
        new Solver(Console.In, Console.Out).Run();
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

internal static class LinqPadExtension {
    public static T Dump<T>(this T obj) {
#if LOCAL
        return LINQPad.Extensions.Dump(obj);
#else
        return obj;
#endif
    }
}

public class Scanner {
    private readonly TextReader Reader;
    private readonly Queue<string> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return int.Parse(Next(), ci); }
    public long NextLong() { return long.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (TokenQueue.Count == 0) {
            if (!StockTokens()) {
                throw new InvalidOperationException();
            }
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext() {
        if (TokenQueue.Count > 0) {
            return true;
        }

        return StockTokens();
    }

    private static readonly char[] _separator = new[] { ' ', '\t' };
    private bool StockTokens() {
        while (true) {
            string line = Reader.ReadLine();
            if (line == null) {
                return false;
            }

            string[] tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) {
                continue;
            }

            foreach (string token in tokens) {
                TokenQueue.Enqueue(token);
            }

            return true;
        }
    }
}
0