結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー 夜
提出日時 2021-07-30 22:13:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,303 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 94,568 KB
実行使用メモリ 9,568 KB
最終ジャッジ日時 2023-10-14 05:11:04
合計ジャッジ時間 7,508 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
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 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 36 ms
4,348 KB
testcase_23 AC 11 ms
4,348 KB
testcase_24 WA -
testcase_25 AC 13 ms
5,084 KB
testcase_26 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
long long c[10];
int main() {
	long long n;
	string s;
	cin >> n >> s;
	long long ans = 0;
	long long sum = 0, p = 1;
	for (int i = 1; i <= 9; i++) {
		cin >> c[i];
	}
	if (s.size() < n) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 0; j < c[i]; j++) {
				cout << i;
			}
		}
		cout << endl;
	}
	else if (s.size() > n) {
		cout << -1 << endl;
	}
	else {
		string ans1 = "";
		for (int i = 9; i >= 1; i--) {
			for (int j = 0; j < c[i]; j++) {
				ans1 += char(i + '0');
			}
		}
		if (s >= ans1) {
			cout << -1 << endl;
		}
		else {
			string ans = ans1;
			reverse(ans1.begin(), ans1.end());
			for (int i = 0; i < n; i++) {
				auto itr = lower_bound(ans1.begin() + i + 1, ans1.end(), s[i]);
				if (itr == ans1.end() || *itr > s[i]) {
					break;
				}
				else {
					swap(ans1[i], *itr);
					ans = ans1;
				}
			}
			if (ans == s) {
				for (int i = n - 1; i > 0; i--) {
					if (ans[i] != ans[i - 1]) {
						swap(ans[i], ans[i - 1]);
						break;
					}
				}
			}
			cout << ans << endl;
		}
	}
}

0