結果

問題 No.297 カードの数式
ユーザー kk
提出日時 2021-01-15 19:33:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,317 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 204,528 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-17 14:18:45
合計ジャッジ時間 3,221 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

// max
long long f(vector<int> a, int x, int y) {
  sort(a.begin(), a.end());

  int pos = 0;
  long long ret = 0;

  for (int i = 0; i < y; i++)
    ret -= a[pos++];
  for (int i = 0; i < x; i++)
    ret += a[pos++];

  long long base = 1;
  while (pos < a.size()) {
    base *= 10;
    ret += base * a[pos++];
  }

  return ret;
}

long long p10(int x) {
  long long ret = 1;
  for (int i = 0; i < x; i++)
    ret *= 10;
  return ret;
}

// min
long long g(vector<int> a, int x, int y) {
  sort(a.begin(), a.end());

  int pos = 0;
  long long ret = 0;

  if (y == 0) {
    int n = a.size();
    for (int i = 0; i < a.size(); i++) {
      ret += a[n-1-i] * p10(i / x);
    }
    return ret;
  }

  for (int i = 0; i < x; i++)
    ret += a[pos++];
  for (int i = 0; i < y; i++)
    ret -= a[pos++];

  long long base = 1;
  while (pos < a.size()) {
    base *= 10;
    ret -= base * a[pos++];
  }
  return ret;
}


int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  int x = 1, y = 0;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    if (isdigit(c))
      a.push_back(c -  '0');
    else if (c == '+')
      ++x;
    else
      ++y;
  }

  cout << f(a, x, y) << " " << g(a, x, y) << endl;
  
  return 0;
}
0