結果

問題 No.1051 PQ Permutation
ユーザー keymoonkeymoon
提出日時 2020-05-08 22:28:13
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,046 bytes
コンパイル時間 5,583 ms
コンパイル使用メモリ 104,848 KB
実行使用メモリ 45,688 KB
最終ジャッジ日時 2023-09-19 07:47:49
合計ジャッジ時間 14,852 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
16,056 KB
testcase_01 AC 80 ms
16,128 KB
testcase_02 AC 67 ms
15,884 KB
testcase_03 AC 79 ms
15,980 KB
testcase_04 WA -
testcase_05 AC 80 ms
15,964 KB
testcase_06 WA -
testcase_07 AC 65 ms
15,884 KB
testcase_08 AC 80 ms
16,020 KB
testcase_09 WA -
testcase_10 AC 80 ms
15,924 KB
testcase_11 AC 81 ms
15,908 KB
testcase_12 WA -
testcase_13 AC 81 ms
16,016 KB
testcase_14 WA -
testcase_15 AC 323 ms
39,724 KB
testcase_16 AC 314 ms
39,520 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 273 ms
38,852 KB
testcase_21 AC 282 ms
38,868 KB
testcase_22 WA -
testcase_23 AC 277 ms
39,568 KB
testcase_24 AC 276 ms
39,752 KB
testcase_25 AC 102 ms
18,220 KB
testcase_26 WA -
testcase_27 AC 91 ms
16,548 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 105 ms
18,276 KB
testcase_31 AC 93 ms
16,856 KB
testcase_32 AC 104 ms
18,236 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 202 ms
32,544 KB
testcase_36 AC 183 ms
32,652 KB
testcase_37 AC 153 ms
36,036 KB
testcase_38 AC 274 ms
38,620 KB
testcase_39 AC 151 ms
33,216 KB
testcase_40 WA -
testcase_41 AC 240 ms
41,596 KB
testcase_42 AC 81 ms
22,036 KB
testcase_43 AC 70 ms
23,992 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 69 ms
22,020 KB
testcase_48 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
public static class P
{
    public static void Main()
    {
        Action abort = () => { Console.WriteLine(-1); Environment.Exit(0); };
        var npq = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n = npq[0];
        var p = npq[1];
        var q = npq[2];
        var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
        if (Permutations(a).Take(2).Count() == 1) abort();
        var nexta = Permutations(a).Take(2).Last();
        a = nexta;

        var pInd = Array.IndexOf(a, p);
        var qInd = Array.IndexOf(a, q);
        if (pInd < qInd) { Console.WriteLine(string.Join(" ", a)); return; }
        //p<qの場合、一時的な逆転が解決された後。q . . p となっているが、このqの位置にqより大きいものが来た後すぐ
        if (p < q)
        {
            //自分より右にp, q, 自分より大きいqでない数を含む最初の場所
            var target = a.Skip(qInd).Max();
            for (; qInd >= 0; qInd--)
            {
                if (a[qInd] < target) break;
                target = Max(target, a[qInd]);
            }
            if (qInd == -1) abort();

            target = a.Skip(qInd).Where(x => a[qInd] < x).Min();

            var res = 
                a.Take(qInd)
                .Concat(new int[] { target })
                .Concat(a.Skip(qInd).Where(x => x != target).OrderBy(x => x)).ToArray();

            Console.WriteLine(string.Join(" ", res));
        }
        //p>qの場合、q . . pとなっているが、pの位置に
        else
        {
            var target = a.Skip(qInd).Where(x => x > q).Min();
            var res =
                a.Take(qInd)
                .Concat(new int[] { target })
                .Concat(a.Skip(qInd).Where(x => x != target && x != q && x <= p).OrderBy(x => x))
                .Concat(new int[] { q })
                .Concat(a.Skip(qInd).Where(x => x != target && x > p).OrderBy(x => x)).ToArray();

            Console.WriteLine(string.Join(" ", res));
        }
    }


    static IEnumerable<T[]> Permutations<T>(T[] array) where T : IComparable<T>
    {
        int index = 0;
        yield return array;
        while (true)
        {
            for (int i = array.Length - 1; i > 0; i--)
            {
                if (array[i - 1].CompareTo(array[i]) >= 0) continue;
                int j = Array.FindLastIndex(array, x => array[i - 1].CompareTo(x) < 0);
                T tmp = array[i - 1]; array[i - 1] = array[j]; array[j] = tmp;
                Array.Reverse(array, i, array.Length - i);
                yield return array;
                goto end;
            }
            Array.Reverse(array, index, array.Length);
            yield break;
            end:;
        }
    }
}
0