結果

問題 No.297 カードの数式
ユーザー motimoti
提出日時 2015-11-29 22:40:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,109 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 107,708 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 22:50:11
合計ジャッジ時間 1,874 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define minimize(a, x) a = std::min(a, x)
#define maximize(a, x) a = std::max(a, x)

using ll  = long long;
int const inf = 1<<29;

vector<int> ds;
int N;
int p, m;

void append(ll& x, int a) {
  x *= 10; x += a;
}

ll number(vector<int>::iterator begin, vector<int>::iterator end) {
  ll num = 0;
  for(auto iter = begin; iter != end; iter++) {
    num *= 10;
    num += *iter;
  }
  return num;
}

ll calc_max() {
  sort(ds.rbegin(), ds.rend());
  int D = ds.size();
  ll max = number(ds.begin(), ds.begin() + D - p - m);
  rep(i, p) max += *(ds.begin() + i + D - p - m);
  rep(i, m) max -= *(ds.begin() + i + D - m);
  return max;
}

ll calc_min() {
  int D = ds.size();
  ll min = 0;
  if(m == 0) {
    sort(all(ds));
    int arr[p+1]; zero(arr);
    int curr = 0;
    rep(i, ds.size()) {
      arr[curr] *= 10;
      arr[curr++] += ds[i];
      curr %= p+1;
    }
    min = accumulate(arr, arr + p+1, 0);
  }
  else {
    sort(ds.rbegin(), ds.rend());
    min = ds[D-1];
    auto dt = vector<int>(ds.begin(), ds.end() - 1);
    D = dt.size();  // p(>=0), m(>0) 符号の個数
    min -= number(dt.begin(), dt.begin() + (D - p - m + 1));
    rep(i, m-1) min -= *(dt.begin() + i + (D - p - m + 1));
    rep(i, p)   min += *(dt.begin() + i + (D - p));
  }
  return min;
}

int main() {

  cin >> N;
  rep(i, N) {
    char ch; cin >> ch;
    if(isdigit(ch)) { ds.push_back(ch-'0'); }
    if(ch == '+') {
      p ++;
    }
    if(ch == '-') {
      m ++;
    }
  }

  cout << calc_max() << " " << calc_min() << endl;
  
  return 0;
}
0