結果

問題 No.1051 PQ Permutation
ユーザー keymoonkeymoon
提出日時 2020-05-08 22:52:28
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 4,242 bytes
コンパイル時間 3,442 ms
コンパイル使用メモリ 110,504 KB
実行使用メモリ 49,924 KB
最終ジャッジ日時 2023-09-19 07:56:39
合計ジャッジ時間 13,114 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
22,132 KB
testcase_01 AC 80 ms
19,952 KB
testcase_02 AC 67 ms
23,868 KB
testcase_03 AC 80 ms
22,092 KB
testcase_04 AC 68 ms
21,944 KB
testcase_05 AC 80 ms
21,988 KB
testcase_06 AC 68 ms
19,916 KB
testcase_07 AC 63 ms
21,844 KB
testcase_08 AC 79 ms
20,004 KB
testcase_09 AC 67 ms
21,828 KB
testcase_10 AC 81 ms
22,092 KB
testcase_11 AC 82 ms
21,980 KB
testcase_12 AC 69 ms
21,772 KB
testcase_13 AC 80 ms
21,944 KB
testcase_14 AC 69 ms
21,876 KB
testcase_15 AC 328 ms
45,972 KB
testcase_16 AC 323 ms
49,924 KB
testcase_17 AC 198 ms
43,624 KB
testcase_18 AC 197 ms
41,568 KB
testcase_19 AC 197 ms
47,724 KB
testcase_20 AC 275 ms
45,796 KB
testcase_21 AC 288 ms
49,268 KB
testcase_22 AC 198 ms
45,588 KB
testcase_23 AC 280 ms
47,160 KB
testcase_24 AC 280 ms
49,056 KB
testcase_25 AC 99 ms
23,188 KB
testcase_26 AC 80 ms
22,636 KB
testcase_27 AC 89 ms
22,224 KB
testcase_28 AC 90 ms
23,340 KB
testcase_29 AC 81 ms
24,644 KB
testcase_30 AC 103 ms
23,192 KB
testcase_31 AC 91 ms
22,616 KB
testcase_32 AC 103 ms
23,220 KB
testcase_33 AC 77 ms
24,172 KB
testcase_34 AC 78 ms
24,128 KB
testcase_35 AC 205 ms
38,704 KB
testcase_36 AC 184 ms
38,608 KB
testcase_37 AC 140 ms
36,964 KB
testcase_38 AC 278 ms
47,540 KB
testcase_39 AC 147 ms
41,132 KB
testcase_40 AC 193 ms
45,748 KB
testcase_41 AC 246 ms
46,360 KB
testcase_42 AC 80 ms
22,064 KB
testcase_43 AC 81 ms
21,936 KB
testcase_44 AC 69 ms
21,796 KB
testcase_45 AC 68 ms
21,864 KB
testcase_46 AC 69 ms
23,868 KB
testcase_47 AC 67 ms
21,724 KB
testcase_48 AC 146 ms
41,056 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;
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()
    {
#if !DEBUG
        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();
        int[] res = null;
        if (Permutations(a).Take(2).Count() != 1)
        {
            //var nexta = Permutations(a).Take(2).Last();
            //a = nexta;
            res = Solve(n, p, q, a);
        }
        else res = null;
        Console.WriteLine(res == null ? "-1" : string.Join(" ", res));
#else
        int n = int.Parse(Console.ReadLine());
        foreach (var perm in Permutations(Enumerable.Range(1, n).ToArray()))
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (i == j) continue;
                    var a = Stupid(n, i, j, perm);
                    var b = Solve(n, i, j, perm);
                    if (a != b && !a.SequenceEqual(b))
                    {
                        Console.WriteLine($"{n} {i} {j}\n{string.Join(" ", perm)}");
                        Console.ReadLine();
                    }
                }
            }
        }
        Console.WriteLine("end");
#endif
    }

    public static int[] Stupid(int n, int p, int q, int[] a)
    {
        var copied = a.ToArray();
        foreach (var perm in Permutations(copied))
        {
            var pInd = Array.IndexOf(perm, p);
            var qInd = Array.IndexOf(perm, q);
            if (pInd < qInd) return perm;
        }
        return null;
    }

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

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

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

            return 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();

            return 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