結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー tnakao0123tnakao0123
提出日時 2021-08-01 19:13:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 1,739 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 91,312 KB
実行使用メモリ 4,656 KB
最終ジャッジ日時 2023-10-14 19:00:07
合計ジャッジ時間 2,952 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 1 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 7 ms
4,524 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 6 ms
4,456 KB
testcase_20 AC 6 ms
4,396 KB
testcase_21 AC 6 ms
4,364 KB
testcase_22 AC 6 ms
4,396 KB
testcase_23 AC 5 ms
4,396 KB
testcase_24 AC 8 ms
4,480 KB
testcase_25 AC 5 ms
4,348 KB
testcase_26 AC 7 ms
4,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1630.cc:  No.1630 Sorting Integers (Greater than K) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 500000;

/* typedef */

/* global variables */

char s[MAX_N + 4], t[MAX_N + 4];
int cs[10];

/* subroutines */

int cmpv(int m, char s[], int n, char t[]) {
  if (m != n) return m - n;
  for (int i = 0; i < n; i++)
    if (s[i] != t[i]) return s[i] - t[i];
  return 0;
}

/* main */

int main() {
  int n;
  scanf("%d%s", &n, s);
  for (int i = 1; i <= 9; i++) scanf("%d", cs + i);
  int m = strlen(s);

  int co = 1;
  for (int i = m - 1; i >= 0; i--) {
    int d = (s[i] - '0') + co;
    s[i] = d % 10 + '0';
    co = d / 10;
  }
  if (co) {
    m++;
    for (int i = m; i > 0; i--) s[i] = s[i - 1];
    s[0] = '1';
  }
  //printf("m=%d\n", m);

  for (int i = 9, p = 0; i >= 1; i--)
    for (int j = 0; j < cs[i]; j++) t[p++] = '0' + i;
  if (cmpv(m, s, n, t) > 0) { puts("-1"); return 0; }
  
  int p = 0;
  if (m == n) {
    for (; p < n; p++) {
      int si = s[p] - '0';
      if (cs[si] > 0) t[p] = s[p], cs[si]--;
      else break;
    }
    if (p < n) {
      while (p >= 0) {
	int si = s[p] - '0', d = si + 1;
	for (; d < 10 && cs[d] == 0; d++);
	if (d >= 10)
	  p--, cs[s[p] - '0']++;
	else {
	  t[p++] = d + '0';
	  cs[d]--;
	  break;
	}
      }
    }
  }

  for (int i = 1; i <= 9; i++)
    while (cs[i]) t[p++] = '0' + i, cs[i]--;

  puts(t);
  return 0;
}
0