結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 kakel-sankakel-san
提出日時 2024-01-26 00:20:39
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 2,741 bytes
コンパイル時間 14,751 ms
コンパイル使用メモリ 158,944 KB
実行使用メモリ 186,524 KB
最終ジャッジ日時 2024-01-26 00:21:00
合計ジャッジ時間 12,304 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
32,804 KB
testcase_01 AC 79 ms
32,804 KB
testcase_02 AC 75 ms
32,676 KB
testcase_03 AC 76 ms
32,804 KB
testcase_04 AC 76 ms
32,804 KB
testcase_05 AC 77 ms
32,804 KB
testcase_06 AC 78 ms
32,804 KB
testcase_07 AC 78 ms
32,804 KB
testcase_08 AC 81 ms
32,804 KB
testcase_09 AC 79 ms
32,804 KB
testcase_10 AC 78 ms
32,804 KB
testcase_11 AC 79 ms
32,804 KB
testcase_12 AC 77 ms
32,804 KB
testcase_13 WA -
testcase_14 AC 78 ms
32,804 KB
testcase_15 AC 77 ms
32,804 KB
testcase_16 WA -
testcase_17 AC 110 ms
40,196 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 103 ms
40,164 KB
testcase_21 AC 108 ms
40,164 KB
testcase_22 AC 100 ms
39,716 KB
testcase_23 AC 82 ms
35,620 KB
testcase_24 AC 104 ms
38,556 KB
testcase_25 AC 89 ms
38,308 KB
testcase_26 AC 100 ms
186,524 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (110 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.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[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = ReadLine().Split();
        var n = int.Parse(c[0]);
        var k = c[1];
        var a = NList;
        if (k.Length > n)
        {
            WriteLine(-1);
            return;
        }
        var min = new List<char>(n);
        for (var i = 0; i < 9; ++i) for (var j = 0; j < a[i]; ++j) min.Add((char)(i + '1'));
        if (IsLarge(min, k))
        {
            WriteLine(string.Concat(min));
            return;
        }
        var max = new List<char>(n);
        for (var i = 8; i >= 0; --i) for (var j = 0; j < a[i]; ++j) max.Add((char)(i + '1'));
        if (!IsLarge(max, k))
        {
            WriteLine(-1);
            return;
        }
        var ok = 0;
        var ng = n;
        while (ng - ok > 1)
        {
            var count = (int[]) a.Clone();
            var mid = (ok + ng) / 2;
            var flg = true;
            for (var i = 0; i < mid; ++i)
            {
                if (k[i] == '0' || count[k[i] - '1'] == 0)
                {
                    flg = false;
                    break;
                }
                --count[k[i] - '1'];
            }
            if (flg)
            {
                flg = false;
                for (var j = k[mid] - '0'; j < 9; ++j) if (count[j] > 0)
                {
                    flg = true;
                    break;
                }
            }
            if (flg) ok = mid;
            else ng = mid;
        }
        var ans = new List<char>();
        for (var i = 0; i < ok; ++i)
        {
            ans.Add(k[i]);
            --a[k[i] - '1'];
        }
        for (var i = k[ok] - '0'; i < 9; ++i) if (a[i] > 0)
        {
            ans.Add((char)(i + '1'));
            --a[i];
            break;
        }
        for (var i = 0; i < 9; ++i) for (var j = 0; j < a[i]; ++j) ans.Add((char)(i + '1'));
        WriteLine(string.Concat(ans));
    }
    static bool IsLarge(List<char> m, string k)
    {
        if (m.Count > k.Length) return true;
        for (var i = 0; i < m.Count; ++i)
        {
            if (m[i] > k[i]) return true;
            if (m[i] < k[i]) return false;
        }
        return false;
    }
}
0