結果
問題 | No.297 カードの数式 |
ユーザー |
|
提出日時 | 2021-01-15 19:33:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 1,317 bytes |
コンパイル時間 | 1,977 ms |
コンパイル使用メモリ | 198,288 KB |
最終ジャッジ日時 | 2025-01-17 18:23:12 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
#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; }