結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー ace_amuroace_amuro
提出日時 2022-05-27 14:42:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,151 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 74,552 KB
実行使用メモリ 14,436 KB
最終ジャッジ日時 2023-10-20 19:41:24
合計ジャッジ時間 2,148 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,852 KB
testcase_01 AC 3 ms
9,852 KB
testcase_02 AC 3 ms
7,696 KB
testcase_03 AC 3 ms
5,756 KB
testcase_04 AC 3 ms
9,852 KB
testcase_05 AC 3 ms
9,852 KB
testcase_06 AC 3 ms
9,852 KB
testcase_07 AC 3 ms
9,852 KB
testcase_08 AC 3 ms
9,852 KB
testcase_09 AC 3 ms
9,852 KB
testcase_10 AC 3 ms
9,852 KB
testcase_11 AC 3 ms
9,852 KB
testcase_12 AC 3 ms
9,852 KB
testcase_13 AC 3 ms
9,852 KB
testcase_14 AC 3 ms
9,852 KB
testcase_15 AC 3 ms
9,852 KB
testcase_16 AC 47 ms
12,388 KB
testcase_17 AC 47 ms
12,388 KB
testcase_18 AC 47 ms
14,436 KB
testcase_19 AC 47 ms
14,436 KB
testcase_20 AC 45 ms
12,388 KB
testcase_21 AC 45 ms
12,388 KB
testcase_22 AC 42 ms
6,244 KB
testcase_23 AC 4 ms
6,136 KB
testcase_24 AC 39 ms
12,288 KB
testcase_25 AC 7 ms
10,232 KB
testcase_26 AC 47 ms
12,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int MR=1e6+10;
typedef long long LL;
int n,k[MR],a[MR],b[10],c[10];
int v[MR];
char s[MR];
int main() {
	scanf("%d%s",&n,s);
	for(int i=1;i<=9;i++) scanf("%d",c+i);
	int m=strlen(s);
	if(m>n){
		printf("-1\n");
		return 0;
	}
	if(m<n){
		for(int i=1;i<=9;i++){
			for(int j=1;j<=c[i];j++){
				printf("%d",i);
			}
		}
		printf("\n");
		return 0;
	}
	for(int i=1;i<=n;i++) k[i]=s[i-1]-'0';
	for(int i=1;i<=9;i++) b[i]=0;
	for(int i=1;i<=n;i++){
		for(int j=k[i]+1;j<=9;j++){
			if(b[j]<c[j]) {
				v[i]=j;
				break;
			}
		}
		if(b[k[i]]<c[k[i]]){
			b[k[i]]++;
		}
		else break;
	}
	//for(int i=1;i<=n;i++) printf("%d",v[i]);printf("\n");
	int cur=-1;
	for(int i=n;i>=1;i--){
		if(v[i]){
			cur=i;
			break;
		}
	}
	if(cur==-1){
		printf("-1\n");
		return 0;
	}
	for(int i=1;i<cur;i++){
		a[i]=k[i];
		c[k[i]]--;
	}
	a[cur]=v[cur];
	c[a[cur]]--;
	for(int i=1;i<=9;i++){
		for(int j=1;j<=c[i];j++){
			cur++;
			a[cur]=i;
		}
	}
	for(int i=1;i<=n;i++) printf("%d",a[i]);printf("\n");
    return 0;
}
0