結果

問題 No.297 カードの数式
ユーザー bal4ubal4u
提出日時 2019-07-07 13:25:44
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 1,635 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 32,488 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 03:25:48
合計ジャッジ時間 977 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

// yukicoder: No.297 カードの数式
// 2019.7.7 bal4u

#include <stdio.h>
#include <string.h>

#define gc() getchar()

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

char p, m, f[10]; int N;

int main()
{
	int i;
	long long a, mi, ma;
	char pp, mm, g[10];

	N = in();
	for (i = 0; i < N; i++) {
		int a = gc(); gc();
		if (a == '+') p++;
		else if (a == '-') m++;
		else f[a & 0xf]++;
	}
	
	// 最大値
	ma = 0, pp = p, mm = m;
	memcpy(g, f, sizeof(g));
	for (i = 0; i < 10 && (mm | pp); i++) if (g[i]) {
		if (mm) {
			if (mm >= g[i]) { mm -= g[i], ma -= i*g[i], g[i] = 0; continue; }
			ma -= i*mm, g[i] -= mm, mm = 0;
		}
		if (pp) {
			if (pp >= g[i]) pp -= g[i], ma += i*g[i], g[i] = 0;
			else ma += i*pp, g[i] -= pp, pp = 0;
		}
	}
	a = 0; for (i = 9; i >= 0; i--) {
		while (g[i]) a = a * 10 + i, g[i]--;
	}
	ma += a;
	
	// 最小値
	mi = 0;
	if (m) {
		m--, a = -1;
		for (i = 0; i < 10; i++) if (f[i]) {
			if (a < 0) {
				a = i;
				if (--f[i] == 0) continue;
			}
			if (p) {
				if (p >= f[i]) { p -= f[i], mi += i*f[i], f[i] = 0; continue; }
				mi += i*p, f[i] -= p, p = 0;
			}
			if (m) {
				if (m >= f[i]) m -= f[i], mi -= i*f[i], f[i] = 0;
				else mi -= i*m, f[i] -= m, m = 0;
			}
		}
		mi += a, a = 0;
		for (i = 9; i >= 0; i--) {
			while (f[i]) a = a * 10 + i, f[i]--;
		}
		mi -= a;
	} else {
		int k = ++p, s = 0;	long long b = 1;
		for (i = 9; i >= 0; i--) {
			while (f[i]) {
				s += i, f[i]--;
				if (--k == 0) mi += b*s, k = p, s = 0, b *= 10;
			}
		}
		mi += b*s;
	}
	printf("%lld %lld\n", ma, mi);
	return 0;
}
0