結果

問題 No.366 ロボットソート
ユーザー mban
提出日時 2017-01-10 04:59:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,754 bytes
コンパイル時間 2,941 ms
コンパイル使用メモリ 110,396 KB
実行使用メモリ 26,552 KB
最終ジャッジ日時 2024-12-29 18:35:48
合計ジャッジ時間 4,341 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
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