結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 tatt61880tatt61880
提出日時 2021-07-31 14:38:31
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,818 bytes
コンパイル時間 2,255 ms
コンパイル使用メモリ 154,540 KB
実行使用メモリ 11,948 KB
最終ジャッジ日時 2023-10-14 18:40:40
合計ジャッジ時間 3,702 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 28 ms
11,756 KB
testcase_17 AC 26 ms
11,856 KB
testcase_18 AC 29 ms
11,948 KB
testcase_19 AC 29 ms
11,948 KB
testcase_20 AC 20 ms
11,908 KB
testcase_21 AC 20 ms
11,940 KB
testcase_22 AC 13 ms
5,556 KB
testcase_23 AC 9 ms
5,524 KB
testcase_24 AC 20 ms
10,164 KB
testcase_25 AC 11 ms
5,640 KB
testcase_26 AC 24 ms
11,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

func main()
	var n: int :: cui@inputInt()
	var k: []char :: cui@input()
	var a: []int :: #[10]int
	for i(1, 9)
		do a[i] :: cui@inputInt()
	end for
	
	if(n < ^k)
		do cui@print("-1\n")
		ret
	elif(n > ^k)
		for i(1, 9)
			do cui@print("\{i}".repeat(a[i]))
		end for
		do cui@print("\n")
		ret
	end if
	
	block
		var ans: []char :: #[n]char
		var j: int :: 0
		for i(9, 1, -1)
			for(1, a[i])
				do ans[j] :: '0'.offset(i)
				do j :+ 1
			end for
		end for
		if(ans <= k)
			do cui@print("-1\n")
			ret
		end if
	end block
	
	var ans: []char :: #[n]char
	var flag: bool :: false
	var failFlag: bool :: false
	for i(0, n - 1)
		if(!failFlag)
			for j(1, 9)
				if(a[j] <> 0)
					if(flag)
						do a[j] :- 1
						do ans[i] :: '0'.offset(j)
						skip i
					end if
					if('0'.offset(j) = k[i])
						do a[j] :- 1
						do ans[i] :: '0'.offset(j)
						skip i
					elif('0'.offset(j) > k[i])
						do flag :: true
						do a[j] :- 1
						do ans[i] :: '0'.offset(j)
						skip i
					end if
				end if
			end for
		end if
		do failFlag :: true
		for j(9, 1, -1)
			if(a[j] <> 0)
				do a[j] :- 1
				do ans[i] :: '0'.offset(j)
				skip i
			end if
		end for
	end for
	if(ans <= k)
		do @permutationNext(ans)
	end if
	
	do cui@print("\{ans}\n")
end func

func permutationNext(array: []char): bool
	var left: int :: ^array - 2
	while(left >= 0 & array[left] >= array[left + 1])
		do left :- 1
	end while
	if(left < 0)
		ret false
	end if
	var right: int :: ^array - 1
	while(array[left] >= array[right])
		do right :- 1
	end while
	var tmp: char :: array[left]
	do array[left] :: array[right]
	do array[right] :: tmp
	do left :+ 1
	do right :: ^array - 1
	while(left < right)
		do tmp :: array[left]
		do array[left] :: array[right]
		do array[right] :: tmp
		do left :+ 1
		do right :- 1
	end while
	ret true
end func
0