結果

問題 No.297 カードの数式
ユーザー uafr_csuafr_cs
提出日時 2015-11-06 23:01:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,893 bytes
コンパイル時間 3,878 ms
コンパイル使用メモリ 76,800 KB
実行使用メモリ 61,636 KB
最終ジャッジ日時 2023-10-11 14:48:44
合計ジャッジ時間 6,106 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 151 ms
61,556 KB
testcase_01 AC 152 ms
56,664 KB
testcase_02 AC 155 ms
56,728 KB
testcase_03 AC 150 ms
56,908 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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(long total, long sign, long current, final int n_count, int[] numbers, int o_count, int[] ops){
		if(sign > 0 || ops[0] > 0){
			if(sign < 0 && ops[0] > 0){
				total += sign * current;
				sign = 1;
				current = 0;
				o_count--;
			}
			
			for(int rest = n_count - o_count, i = 9; i >= 0; i--){
				final int times = Math.min(numbers[i], rest);
			
				rest -= times;
				for(int j = 0; j < times; j++){
					current *= 10;
					current += i;
				}
			
				for(int j = 0; j < numbers[i] - times; j++){
					current += i;
				}
			}
			
			return total + sign * current;
		}else{
			for(int rest = n_count - o_count, i = 0; i < 10; i++){
				final int times = Math.min(numbers[i], rest);
			
				rest -= times;
				for(int j = 0; j < times; j++){
					current *= 10;
					current += i;
				}
			
				for(int j = 0; j < numbers[i] - times; j++){
					current += i;
				}
			}
			
			return total + sign * current;	
		}
	}
	
	public static long estimate_min(long total, long sign, long current, final int n_count, int[] numbers, int o_count, int[] ops){
		if(sign < 0 || ops[1] > 0){
			if(sign > 0 && ops[1] > 0){
				total += sign * current;
				sign = -1;
				current = 0;
				o_count--;
			}
			
			for(int rest = n_count - o_count, i = 9; i >= 0; i--){
				final int times = Math.min(numbers[i], rest);
			
				rest -= times;
				for(int j = 0; j < times; j++){
					current *= 10;
					current += i;
				}
			
				for(int j = 0; j < numbers[i] - times; j++){
					current += i;
				}
			}
			
			return total + sign * current;
		}else{
			for(int rest = n_count - o_count, i = 0; i < 10; i++){
				final int times = Math.min(numbers[i], rest);
			
				rest -= times;
				for(int j = 0; j < times; j++){
					current *= 10;
					current += i;
				}
			
				for(int j = 0; j < numbers[i] - times; j++){
					current += i;
				}
			}
			
			return total + sign * current;	
		}
	}
	
	public static void dfs(long total, long sign, long current, boolean first, final int n_count, int[] numbers, final int o_count, int[] ops){
		if(n_count == 0 && o_count == 0){
			total += current * sign;
			
			min = Math.min(min, total);
			max = Math.max(max, total);
			return;
		}
		
		final long estimate_max = estimate_max(total, sign, current, n_count, numbers, o_count, ops);
		final long estimate_min = estimate_min(total, sign, current, n_count, numbers, o_count, ops);
		if(estimate_max < max && estimate_min > min){
			return;
		}
		//System.out.println();
		
		
		if(first || n_count > o_count){
			for(int i = 0; i < 10; i++){
				if(numbers[i] > 0){
					numbers[i]--;
					dfs(total, sign, current * 10 + i, false, n_count - 1, numbers, o_count, ops);
					numbers[i]++;
				}
			}
		}
		
		if(!first){
			if(ops[0] > 0){ 
				ops[0]--;
				dfs(total + current * sign,  1, 0, true, n_count, numbers, o_count - 1, ops);
				ops[0]++;
			}
			if(ops[1] > 0){
				ops[1]--;
				dfs(total + current * sign, -1, 0, true, n_count, numbers, o_count - 1, ops);
				ops[1]++;
			}
		}
	}
	
	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++;
			}
		}
		
		dfs(0, 1, 0, true, n_count, numbers, o_count, ops);
		
		System.out.println(max + " " + min);
	}

}
0