結果

問題 No.297 カードの数式
ユーザー uafr_csuafr_cs
提出日時 2015-11-06 23:23:53
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,584 bytes
コンパイル時間 2,372 ms
コンパイル使用メモリ 76,392 KB
実行使用メモリ 59,004 KB
最終ジャッジ日時 2023-10-11 14:57:43
合計ジャッジ時間 7,693 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
56,424 KB
testcase_01 AC 150 ms
56,672 KB
testcase_02 AC 148 ms
56,728 KB
testcase_03 AC 148 ms
56,968 KB
testcase_04 AC 149 ms
56,728 KB
testcase_05 AC 150 ms
56,716 KB
testcase_06 AC 150 ms
56,668 KB
testcase_07 AC 155 ms
56,660 KB
testcase_08 AC 156 ms
56,504 KB
testcase_09 AC 156 ms
56,700 KB
testcase_10 AC 155 ms
56,856 KB
testcase_11 AC 155 ms
56,464 KB
testcase_12 AC 153 ms
56,796 KB
testcase_13 AC 149 ms
56,680 KB
testcase_14 AC 149 ms
56,680 KB
testcase_15 AC 151 ms
56,816 KB
testcase_16 AC 152 ms
56,860 KB
testcase_17 AC 152 ms
56,692 KB
testcase_18 AC 153 ms
56,900 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 150 ms
56,728 KB
testcase_23 AC 152 ms
56,596 KB
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static long min = Long.MAX_VALUE;
	public static long max = Long.MIN_VALUE;

	public static long estimate_max(int n_count, int[] numbers, int o_count, int[] ops) {
		long ret = 0;
		
		for(int i = 9, rest = n_count - o_count, plus = ops[0]; i >= 0; i--){
			final int times = Math.min(numbers[i], rest);
			
			rest -= times;
			for(int j = 0; j < times; j++){
				ret *= 10;
				ret += i;
			}
			
			for(int j = 0; j < (numbers[i] - times); j++){
				if(plus > 0){
					plus--;
					ret += i;
				}else{
					ret -= i;
				}
			}
		}
		
		return ret;
	}
	
	public static long estimate_min(int n_count, int[] numbers, int o_count, int[] ops) {
		long ret = 0;
		
		if(ops[1] > 0){
			int min_value = -1;
			for(int i = 0; i < 10; i++){
				if(numbers[i] > 0){
					min_value = i;
					break;
				}
			}
			
			ops[1]--;
			numbers[min_value]--;
			o_count--;
			n_count--;
			ret = min_value;
			
			long minus_value = 0;
			for(int i = 9, rest = n_count - o_count, minus = ops[1]; i >= 0; i--){
				final int times = Math.min(numbers[i], rest);
			
				rest -= times;
				for(int j = 0; j < times; j++){
					minus_value *= 10;
					minus_value += i;
				}
			
				for(int j = 0; j < (numbers[i] - times); j++){
					if(minus > 0){
						minus--;
						ret -= i;
					}else{
						ret += i;
					}
				}
			}
			ret -= minus_value;
			
			ops[1]++;
			numbers[min_value]++;
			o_count++;
			n_count++;
		}else{
		
			for(int i = 0, rest = n_count - o_count; i < 10; i++){
				final int times = Math.min(numbers[i], rest);
				
				rest -= times;
				for(int j = 0; j < times; j++){
					ret *= 10;
					ret += i;
				}
				
				for(int j = 0; j < (numbers[i] - times); j++){
					ret += i;
				}
			}
		}
		
		return ret;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		final int N = sc.nextInt();
		char[] inputs = new char[N];
		for (int i = 0; i < N; i++) {
			inputs[i] = sc.next().charAt(0);
		}

		int n_count = 0, o_count = 0;
		int[] ops = new int[2];
		int[] numbers = new int[10];
		for (int i = 0; i < inputs.length; i++) {
			switch (inputs[i]) {
			case '+':
				ops[0]++;
				o_count++;
				break;
			case '-':
				ops[1]++;
				o_count++;
				break;
			default:
				numbers[Character.getNumericValue(inputs[i])]++;
				n_count++;
			}
		}
		
		System.out.println(estimate_max(n_count, numbers, o_count, ops) + " " + estimate_min(n_count, numbers, o_count, ops));
	}

}
0