結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー publflpublfl
提出日時 2021-07-30 20:49:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 822 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 48,744 KB
実行使用メモリ 36,308 KB
最終ジャッジ日時 2023-10-14 03:06:00
合計ジャッジ時間 2,382 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 67 ms
36,308 KB
testcase_17 AC 69 ms
36,300 KB
testcase_18 AC 67 ms
36,036 KB
testcase_19 AC 67 ms
35,976 KB
testcase_20 AC 40 ms
5,052 KB
testcase_21 AC 41 ms
5,128 KB
testcase_22 AC 37 ms
4,352 KB
testcase_23 AC 4 ms
4,352 KB
testcase_24 AC 57 ms
29,500 KB
testcase_25 AC 35 ms
36,280 KB
testcase_26 AC 68 ms
36,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>
#include <string.h>

char x[500010];
std::vector<int> ans;
int c[110],b;
int func(int k)
{
	if(k<1) return 0;
	int digit = x[b-k+1]-'0';
	if(c[digit]>0)
	{
		ans.push_back(digit);
		c[digit]--;
		int t = func(k-1);
		if(t==1) return 1;
		c[digit]++;
		ans.pop_back();
	}
	for(int i=digit+1;i<=9;i++)
	{
		if(c[i]>0)
		{
			ans.push_back(i);
			c[i]--;
			for(int j=1;j<=9;j++) while(c[j]--) ans.push_back(j);
			return 1;
		}
	}
	return 0;
}

int main()
{
	int a;
	scanf("%d",&a);
	scanf("%s",x+1);
	b = strlen(x+1);
	for(int i=1;i<=9;i++) scanf("%d",&c[i]);
	
	if(b>a) printf("-1");
	else if(b<a)
	{
		for(int i=1;i<=9;i++) while(c[i]--) printf("%d",i);
		return 0;
	}
	else
	{
		int t = func(a);
		if(t==1) for(int i=0;i<ans.size();i++) printf("%d",ans[i]);
		else printf("-1");
	}
}
0