結果

問題 No.326 あみだますたー
コンテスト
ユーザー aketijyuuzou
提出日時 2026-06-20 20:19:19
言語 C#(csc)
(csc 3.9.0)
コンパイル:
csc -langversion:latest -unsafe -warn:0 -o+ /r:System.Numerics.dll _filename_ -out:a.exe
実行:
/usr/bin/mono a.exe
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 3,107 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,065 ms
コンパイル使用メモリ 109,184 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2026-06-20 20:19:26
合計ジャッジ時間 5,529 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #
raw source code

using System;
using System.Collections.Generic;
using System.Linq;

// https://yukicoder.me/problems/no/326
// yukicoder 326 あみだますたー
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5");
            WillReturn.Add("6");
            WillReturn.Add("1 2");
            WillReturn.Add("3 4");
            WillReturn.Add("2 3");
            WillReturn.Add("3 4");
            WillReturn.Add("1 2");
            WillReturn.Add("4 5");
            WillReturn.Add("5 1 4 3 2");
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("6");
            WillReturn.Add("4");
            WillReturn.Add("1 2");
            WillReturn.Add("2 3");
            WillReturn.Add("5 6");
            WillReturn.Add("3 4");
            WillReturn.Add("2 6 3 1 5 4");
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => int.Parse(pX)).ToArray();
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);

        int N = int.Parse(InputList[0]);
        int K = int.Parse(InputList[1]);
        int[] AArr = GetSplitArr(InputList[InputList.Count - 1]);

        var CurrDict = new int[N + 1];
        for (int I = 1; I <= N; I++) {
            CurrDict[I] = I;
        }

        foreach (string EachStr in InputList.Skip(2).Take((int)K)) {
            SplitAct(EachStr);
            int L = wkArr[0];
            int R = wkArr[1];
            int tmp = CurrDict[L];
            CurrDict[L] = CurrDict[R];
            CurrDict[R] = tmp;
        }
        InputList.Clear();
        System.GC.Collect();

        var GoalDict = new int[N + 1];
        for (int I = 0; I <= AArr.GetUpperBound(0); I++) {
            GoalDict[AArr[I]] = I + 1;
        }

        var sb = new System.Text.StringBuilder();
        long AnswerCnt = 0;
        for (int I = 1; I <= N; I++) {
            int SearchVal = GoalDict[I];
            int StaInd = 0;
            for (int J = I + 1; J <= N; J++) {
                if (CurrDict[J] == SearchVal) {
                    StaInd = J;
                    break;
                }
            }
            // 既にゴールにいる場合
            if (I == StaInd) continue;

            for (int J = StaInd; I + 1 <= J; J--) {
                // 三角交換
                int tmp = CurrDict[J];
                CurrDict[J] = CurrDict[J - 1];
                CurrDict[J - 1] = tmp;

                AnswerCnt++;
                sb.AppendFormat("{0} {1}", J - 1, J);
                sb.AppendLine();
            }
        }
        Console.WriteLine(AnswerCnt);
        Console.Write(sb.ToString());
    }
}
0