結果

問題 No.14 最小公倍数ソート
ユーザー mbanmban
提出日時 2017-01-11 01:16:13
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,614 bytes
コンパイル時間 2,950 ms
コンパイル使用メモリ 107,964 KB
実行使用メモリ 25,908 KB
最終ジャッジ日時 2023-08-23 07:29:08
合計ジャッジ時間 65,643 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,888 KB
testcase_01 AC 58 ms
20,900 KB
testcase_02 AC 58 ms
20,916 KB
testcase_03 AC 122 ms
20,952 KB
testcase_04 TLE -
testcase_05 AC 1,941 ms
23,416 KB
testcase_06 AC 2,244 ms
21,392 KB
testcase_07 AC 2,903 ms
21,424 KB
testcase_08 AC 3,787 ms
21,572 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 2,061 ms
21,300 KB
testcase_17 AC 1,500 ms
21,276 KB
testcase_18 AC 702 ms
23,140 KB
testcase_19 AC 3,433 ms
23,548 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 int N;
    static int[] a;
    static List<int> ans = new List<int>();
    static bool[] c;
    static void Main()
    {
        Scan();
        c = new bool[N];
        c[0] = true;
        ans.Add(a[0]);
        for(int i = 1; i < N; i++)
        {
            int ind = -1;
            int minlcm = int.MaxValue;
            int min = int.MaxValue;
            for(int j = 0; j < N; j++)
            {
                if (c[j]) continue;
                int lcm = LCM(a[j], ans[ans.Count - 1]);
                if (minlcm > lcm)
                {
                    minlcm = lcm;
                    ind = j;
                   min = a[j];
                }
                if (minlcm == lcm)
                {
                    if (min > a[j])
                    {
                        ind = j;
                        min = a[j];
                    }
                }
            }
            ans.Add(a[ind]);
            c[ind] = true;
        }
        Console.WriteLine(string.Join(" ", ans));
    }
    static int LCM(int a,int b)
    {
        if (a < b)
        {
            Swap(ref a, ref b);
        }
        int x = a * b;
        int r = a % b;
        while (r > 0)
        {
            a = b;
            b = r;
            r = a % b;
        }
        return x / b;
    }
    static void Swap(ref int a,ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    static void Scan()
    {
        Scanner sc = new Scanner();
        N = sc.NextInt();
        a = new int[N];
        for(int i = 0; i < N; i++)
        {
            a[i] = sc.NextInt();
        }
    }
}
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