結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 👑 tatt61880tatt61880
提出日時 2021-07-30 21:33:08
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 421 ms / 2,000 ms
コード長 1,868 bytes
コンパイル時間 2,437 ms
コンパイル使用メモリ 154,852 KB
実行使用メモリ 6,456 KB
最終ジャッジ日時 2023-10-14 18:38:49
合計ジャッジ時間 5,689 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 185 ms
6,424 KB
testcase_17 AC 182 ms
6,432 KB
testcase_18 AC 185 ms
6,440 KB
testcase_19 AC 186 ms
6,456 KB
testcase_20 AC 179 ms
6,280 KB
testcase_21 AC 179 ms
6,224 KB
testcase_22 AC 421 ms
5,624 KB
testcase_23 AC 9 ms
5,620 KB
testcase_24 AC 147 ms
5,792 KB
testcase_25 AC 11 ms
5,716 KB
testcase_26 AC 182 ms
6,228 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)
			for(1, a[i])
				do cui@print("\{i}")
			end for
		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 @nextPermutation(ans)
	end if
	
	for i(0, n - 1)
		do cui@print("\{ans[i]}")
	end for
	do cui@print("\n")
end func

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