結果

問題 No.297 カードの数式
ユーザー TatamoTatamo
提出日時 2016-06-07 19:06:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,572 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 70,656 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 22:55:05
合計ジャッジ時間 1,542 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<queue>
#include<math.h>

using namespace std;

typedef long long ll;

int main(){
	int n;
	char* k;
	int num_p = 0;
	int num_m = 0;
	priority_queue<int, vector<int>, less<int> > q_max;
	priority_queue<int, vector<int>, less<int> > q_min;
	int c = 0;
	cin >> n;
	k = new char[n];
	for(int i=0;i<n;i++){
		cin >> k[i];
		if(k[i] == '+'){
			num_p += 1;
		}
		else if(k[i] == '-'){
			num_m += 1;
		}
		else{
			c += 1;
			int d = (int)(k[i]-'0');
			q_max.push(d);
			q_min.push(d);
		}
	}
	int num_d = q_max.size();

	int max_d = num_d-num_p-num_m;

	ll result_max = 0;
	ll result_min = 0;

	// MAX
	int i = max_d;
	ll tmp = 0;
	while(i>0){
		tmp += q_max.top()*pow(10,i-1);
		q_max.pop();
		i -= 1;
	}
	result_max += tmp;
	int p=num_p;
	while(p>0){
		result_max += q_max.top();
		q_max.pop();
		p -= 1;
	}
	int m = num_m;
	while(m>0){
		result_max -= q_max.top();
		q_max.pop();
		m -= 1;
	}
	if(num_m!=0){ // contain at least 1 minus(es)
		// min
		i = max_d;
		tmp = 0;
		while(i>0){
			tmp += q_min.top()*pow(10,i-1);
			q_min.pop();
			i -= 1;
		}
		result_min -= tmp;
		int m = num_m;
		while(m>1){
			result_min -= q_min.top();
			q_min.pop();
			m -= 1;
		}
		int p = num_p;
		while(p>=0){
			result_min += q_min.top();
			q_min.pop();
			p -= 1;
		}
	}
	else{ // all plus
		int count = 0;
		int digit = 0;
		while(!q_min.empty()){
			result_min += q_min.top()*pow(10,digit);
			q_min.pop();
			count += 1;
			if(count > num_p) {
				count = 0;
				digit += 1;
			}
		}
	}

	cout << result_max << " " << result_min << endl;
	return 0;
}
0