結果

問題 No.14 最小公倍数ソート
ユーザー warashiwarashi
提出日時 2015-11-02 20:41:05
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 1,058 bytes
コンパイル時間 12,101 ms
コンパイル使用メモリ 219,952 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-19 03:29:54
合計ジャッジ時間 18,644 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 101 ms
6,016 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
)

var key int

func main() {
	var N int
	fmt.Scan(&N)
	a := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&a[i])
	}
	if len(a) > 1 {
		lcmSort(a[0], a[1:])
	}
	ans := fmt.Sprintln(a)
	fmt.Println(ans[1 : len(ans)-2])
}

func lcmSort(k int, a []int) {
	if len(a) == 1 {
		return
	}

	b := make([]int, len(a))
	for i := range a {
		b[i] = lcm(k, a[i])
	}

	if b[1] < b[0] || (b[1] == b[0] && a[1] < a[0]) {
		a[0], a[1] = a[1], a[0]
		b[0], b[1] = b[1], b[0]
	}

	for i := range a {
		if i == 0 || i == 1 {
			continue
		}
		if less(a, b, i, 0) {
			a[0], a[1], a[i] = a[i], a[0], a[1]
			b[0], b[1], b[i] = b[i], b[0], b[1]
		} else if less(a, b, i, 1) {
			a[1], a[i] = a[i], a[1]
			b[1], b[i] = b[i], b[1]
		}
	}
	lcmSort(a[0], a[1:])
}
func less(a, b []int, i, j int) bool {
	if b[i] == b[j] {
		return a[i] < a[j]
	}
	return b[i] < b[j]
}
func lcm(a, b int) int {
	g := gcd(a, b)
	return (a / g) * (b / g) * g
}

func gcd(a, b int) int {
	if a > b {
		a, b = b, a
	}
	if a == 0 {
		return b
	}

	return gcd(b%a, a)
}
0