結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 風月風月
提出日時 2021-07-30 22:16:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,949 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 200,416 KB
実行使用メモリ 4,688 KB
最終ジャッジ日時 2023-10-14 05:18:05
合計ジャッジ時間 3,512 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 RE -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 5 ms
4,348 KB
testcase_19 WA -
testcase_20 AC 4 ms
4,388 KB
testcase_21 AC 5 ms
4,356 KB
testcase_22 AC 5 ms
4,420 KB
testcase_23 AC 4 ms
4,352 KB
testcase_24 WA -
testcase_25 AC 4 ms
4,348 KB
testcase_26 AC 5 ms
4,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<unordered_map>
#define for_int(i,a,b) for(int i=(a);i<=(b);++i)
#define rep_int(i,a,b) for(int i=(a);i>=(b);--i)
#define for_ll(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep_ll(i,a,b) for(ll i=(a);i>=(b);--i)
#define Ios std::ios::sync_with_stdio(false),std::cin.tie(0)
#define ull unsigned long long
#define ll long long
#define db double
#define PII std::pair<int,int>
#define PLI std::pair<long long , int>
template<class T> void chkmax(T& a, T b) { a > b ? (a = a) : (a = b); }
template<class T> void chkmin(T& a, T b) { a > b ? (a = b) : (a = a); }
template<class T> T min(T a, T b) { return a > b ? b : a; }
template<class T> T max(T a, T b) { return a < b ? b : a; }
template<class T> T abs(T a) { return a < 0 ? -a : a; }
//using namespace std;
/*
	1.注意边界情况
	2.优先考虑时间复杂度
!!! 3.求最大值时,答案应至多初始化为INT_MIN;求最小值时,答案应至少初始化为INT_MAX
	4.遇事不决先暴力
	5.代码要写简洁
	6.计数题要开long long
*/
//快速IO
template<class T>
inline bool rd(T& ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return true;
}
template<class T>
inline void print(T x) {
	if (x > 9) print(x / 10);
	putchar(x % 10 + '0');
	return;
}
using std::vector;
using std::queue;
using std::string;
using std::map;
using std::unordered_map;
using std::priority_queue;
using std::cout;
using std::cin;

const int N = 5e5 + 5;
char k[N], res[N];
int n, a[10];
bool solve(int st) {
	while (st <= n) {
		int tmp = k[st] - '0';
		if (a[tmp]) res[st] = k[st], --a[tmp], ++st;
		else {
			int idx1 = -1;
			for(int j = tmp + 1 ; j <= 9 ; ++j)
				if (a[j]) {
					idx1 = j; break;
				}
			int idx2 = -1;
			for(int j = res[st - 1] - '0' + 1; j <= 9 ; ++j)
				if (a[j]) { idx2 = j; break; }
			if (idx1 == -1 && idx2 == -1) return false;
			if (idx1 != -1){
				res[st++] = idx1 + '0'; 
				--a[idx1];
				for (int i = 1; i <= 9; ++i)
					while (a[i]) res[st++] = i + '0', --a[i];
				return true;
			}
			else {
				++a[res[st - 1] - '0'];
				res[st - 1] = idx2 + '0';
				--a[idx2];
				for (int i = 1; i <= 9; ++i)
					while (a[i]) res[st++] = i + '0', --a[i];
				return true;
			}
		}
	}
	return true;
}
int main() {
	rd(n);
	k[0] = '0';
	scanf("%s", k + 1);
	for (int i = 1; i <= 9; ++i) rd(a[i]);
	int m = strlen(k + 1);
	k[m] ++;
	int idx = m;
	while (k[idx] > '9') k[idx] = '0', --idx, k[idx] ++;
	int st = min(idx, 1);
	m = m - st + 1;
	if (m > n) { puts("-1"); return 0; }
	else if(m < n){
		idx = 1;
		for (int i = 1; i <= 9; ++i)
			while (a[i]) res[idx++] = i + '0', --a[i];
		printf("%s\n", res + 1);
		return 0;
	}
	if (!solve(min(idx , 1))) puts("-1");
	else printf("%s\n", res + min(idx , 1));
	return 0;
}
0