結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー kotatsugamekotatsugame
提出日時 2021-07-30 20:46:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 699 bytes
コンパイル時間 629 ms
コンパイル使用メモリ 68,908 KB
実行使用メモリ 43,948 KB
最終ジャッジ日時 2023-10-14 03:03:14
合計ジャッジ時間 2,618 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,356 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 46 ms
43,940 KB
testcase_17 AC 47 ms
43,940 KB
testcase_18 AC 45 ms
43,632 KB
testcase_19 AC 46 ms
43,896 KB
testcase_20 AC 13 ms
5,080 KB
testcase_21 AC 14 ms
5,096 KB
testcase_22 AC 36 ms
4,348 KB
testcase_23 AC 11 ms
4,352 KB
testcase_24 AC 37 ms
35,732 KB
testcase_25 AC 44 ms
42,620 KB
testcase_26 AC 46 ms
43,948 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:29:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   29 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;
int N;
string K,ans;
int C[10];
bool dfs(int id)
{
	if(id==N)return false;
	int c=K[id]-'0';
	if(C[c]>0)
	{
		C[c]--;
		if(dfs(id+1))
		{
			ans+=c+'0';
			return true;
		}
		C[c]++;
	}
	c++;
	while(c<=9&&C[c]==0)c++;
	if(c==10)return false;
	C[c]--;
	for(int i=9;i>=1;i--)for(int j=0;j<C[i];j++)ans+=i+'0';
	ans+=c+'0';
	return true;
}
main()
{
	cin>>N>>K;
	for(int i=1;i<=9;i++)cin>>C[i];
	if(K.size()!=N)
	{
		if(K.size()>N)cout<<-1<<endl;
		else
		{
			for(int i=1;i<=9;i++)for(int j=0;j<C[i];j++)cout<<i;
			cout<<endl;
		}
		return 0;
	}
	if(!dfs(0))cout<<-1<<endl;
	else
	{
		reverse(ans.begin(),ans.end());
		cout<<ans<<endl;
	}
}
0