結果

問題 No.366 ロボットソート
ユーザー mbanmban
提出日時 2017-01-10 04:59:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,754 bytes
コンパイル時間 3,289 ms
コンパイル使用メモリ 107,188 KB
実行使用メモリ 22,932 KB
最終ジャッジ日時 2023-08-28 15:31:10
合計ジャッジ時間 4,634 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
20,736 KB
testcase_01 AC 56 ms
20,736 KB
testcase_02 AC 56 ms
20,740 KB
testcase_03 AC 54 ms
18,756 KB
testcase_04 AC 55 ms
22,796 KB
testcase_05 AC 57 ms
22,752 KB
testcase_06 AC 56 ms
20,660 KB
testcase_07 AC 56 ms
20,672 KB
testcase_08 AC 57 ms
20,676 KB
testcase_09 AC 56 ms
20,672 KB
testcase_10 AC 63 ms
20,848 KB
testcase_11 AC 64 ms
20,976 KB
testcase_12 AC 64 ms
20,936 KB
testcase_13 AC 63 ms
22,852 KB
testcase_14 AC 63 ms
20,812 KB
testcase_15 AC 63 ms
20,852 KB
testcase_16 AC 62 ms
18,812 KB
testcase_17 AC 64 ms
22,932 KB
testcase_18 AC 63 ms
22,852 KB
testcase_19 AC 65 ms
20,868 KB
testcase_20 AC 56 ms
20,800 KB
testcase_21 AC 65 ms
20,880 KB
testcase_22 AC 66 ms
20,944 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.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;


class Magatro
{
    static Scanner sc = new Scanner();
    static int N, K;
    static int[] a;
    static void Main()
    {
        N = sc.NextInt();
        K = sc.NextInt();
        int[][] a = new int[K][];
        for(int i = 0; i < K; i++)
        {
            if (N % K > i)
            {
                a[i] = new int[N / K+1];
            }
            else
            {
                a[i] = new int[N / K];
            }
        }
        for(int i = 0; i < N; i++)
        {
            a[i % K][i / K] = sc.NextInt();
        }
        int cnt = 0;
        for(int i = 0; i < K; i++)
        {
            cnt += BubbleSort(ref a[i]);
        }
        int[] ans = Arr(a);
        if (Sorted(ans))
        {
            Console.WriteLine(cnt);
        }
        else
        {
            Console.WriteLine(-1);
        }

    }
    static int BubbleSort(ref int[] array)
    {
        int cnt = 0;
      for(int i = 0; i < array.Length-1; i++)
        {
            for(int j = array.Length-1; j > i; j--)
            {
                if (array[j] < array[j - 1])
                {
                    Swap(ref array[j], ref array[j - 1]);
                    cnt++;
                }
            }
        }
        return cnt;
    }
    static int[] Arr(int[][] a)
    {
        int[] res = new int[N];
        for(int i = 0; i < N; i++)
        {
            res[i] = a[i % K][i / K];
        }
        return res;
    }
    static bool Sorted(int[] array)
    {
        for(int i = 0; i < array.Length-1; i++)
        {
            if (array[i] > array[i + 1])
            {
                return false;
            }
        }
        return true;
    }
    static void Swap(ref int a,ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
}

public class Scanner
{
    public string[] S;
    private int Index;
    private char Separator;
    public Scanner(char separator=' ')
    {
        Index = 0;
        Separator = separator;
    }
    public string Next()
    {
        string result;
        if (S == null || Index >= S.Length)
        {
            S = Line();
            Index = 0;
        }
        result = S[Index];
        Index++;
        return result;
    }
    private string[] Line()
    {
        return Console.ReadLine().Split(Separator);
    } 
    public int NextInt()
    {
        return int.Parse(Next());
    }
    public double NextDouble()
    {
        return double.Parse(Next());
    }
    public long NextLong()
    {
        return long.Parse(Next());
    }
}
0