結果
問題 |
No.297 カードの数式
|
ユーザー |
![]() |
提出日時 | 2015-11-06 22:42:36 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,687 bytes |
コンパイル時間 | 2,913 ms |
コンパイル使用メモリ | 82,084 KB |
実行使用メモリ | 61,556 KB |
最終ジャッジ日時 | 2024-09-13 13:21:49 |
合計ジャッジ時間 | 5,602 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 1 TLE * 1 -- * 21 |
ソースコード
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 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; } 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); } }