結果

問題 No.297 カードの数式
ユーザー mitsuomitsuo
提出日時 2016-05-15 20:23:20
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,332 bytes
コンパイル時間 3,233 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 54,700 KB
最終ジャッジ日時 2024-04-20 04:02:08
合計ジャッジ時間 8,684 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
42,680 KB
testcase_01 AC 175 ms
42,560 KB
testcase_02 AC 158 ms
42,700 KB
testcase_03 AC 154 ms
42,472 KB
testcase_04 WA -
testcase_05 AC 147 ms
42,512 KB
testcase_06 AC 174 ms
42,404 KB
testcase_07 AC 173 ms
42,516 KB
testcase_08 AC 153 ms
42,712 KB
testcase_09 AC 170 ms
42,388 KB
testcase_10 AC 161 ms
42,384 KB
testcase_11 AC 173 ms
42,524 KB
testcase_12 AC 173 ms
42,660 KB
testcase_13 AC 155 ms
42,356 KB
testcase_14 AC 165 ms
42,140 KB
testcase_15 AC 157 ms
42,408 KB
testcase_16 AC 172 ms
42,704 KB
testcase_17 AC 172 ms
42,376 KB
testcase_18 AC 158 ms
42,396 KB
testcase_19 AC 163 ms
42,568 KB
testcase_20 AC 158 ms
42,500 KB
testcase_21 AC 180 ms
42,312 KB
testcase_22 AC 177 ms
42,212 KB
testcase_23 AC 161 ms
42,520 KB
testcase_24 AC 173 ms
42,816 KB
testcase_25 AC 168 ms
42,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package jp.fedom.challange.yuki.l2.q297;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String[] c = new String[N];
		for (int i = 0; i < N; i++) {
			c[i] = sc.next();
		}
		sc.close();
		System.out.println(solve(N, c));
	}

	public static String solve(int N, String[] c) {
		PriorityQueue<Long> n = new PriorityQueue<>(new Comparator<Long>() {
			@Override
			public int compare(Long o1, Long o2) {
				return (int) (o2 - o1);
			};
		});
		PriorityQueue<Character> ex = new PriorityQueue<>(new Comparator<Character>() {
			@Override
			public int compare(Character o1, Character o2) {
				return o1 - o2;
			};
		});
		PriorityQueue<Character> nex = new PriorityQueue<>(new Comparator<Character>() {
			@Override
			public int compare(Character o1, Character o2) {
				return o2 - o1;
			};
		});
		boolean containMinus = false;
		for (String s : c) {
			if (s.equals("+") || s.equals("-")) {
				ex.add(s.charAt(0));
				containMinus |= s.charAt(0) == '-';
			} else {
				n.add(new Long(s));
			}
		}
		long max = 0;
		long min = 0;

		if (containMinus) {
			while (n.size() > ex.size()) {
				max *= 10;
				max += n.poll();
			}
			nex.addAll(ex);
			long absmax = max;
			PriorityQueue<Long> nc = new PriorityQueue<>(n);

			while (ex.size() > 0) {
				char ch = (char) ex.poll();
				if (ch == '-') {
					max -= n.poll();
				} else {
					max += n.poll();
				}
			}
			nc.add(absmax);
			nex.add('+');
			while (nex.size() > 0) {
				char ch = (char) nex.poll();
				if (ch == '-') {
					min -= nc.poll();
				} else {
					min += nc.poll();
				}
			}
		} else {
			PriorityQueue<Long> nn = new PriorityQueue<>(new Comparator<Long>() {
				@Override
				public int compare(Long o1, Long o2) {
					return (int) (o1 - o2);
				};
			});
			nn.addAll(n);

			while (n.size() > ex.size()) {
				max *= 10;
				max += n.poll();
			}
			while (n.size() > 0) {
				max += n.poll();
			}

			int m = ex.size() + 1;
			int[] mins = new int[m];
			int i = 0;
			while (nn.size() > 0) {
				mins[i % m] *= 10;
				mins[i % m] += nn.poll();
				i++;
			}

			for (int j = 0; j < mins.length; j++) {
				min += mins[j];
			}
		}

		return max + " " + min;
	}
}
0