結果

問題 No.297 カードの数式
ユーザー tentententen
提出日時 2023-01-06 10:56:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,422 bytes
コンパイル時間 3,043 ms
コンパイル使用メモリ 97,080 KB
実行使用メモリ 45,480 KB
最終ジャッジ日時 2024-05-07 12:54:02
合計ジャッジ時間 5,658 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
45,480 KB
testcase_01 AC 70 ms
38,104 KB
testcase_02 AC 71 ms
38,320 KB
testcase_03 AC 68 ms
39,956 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.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static long max = Long.MIN_VALUE;
    static long min = Long.MAX_VALUE;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] counts = new int[12];
        int oCount = 0;
        int nCount = 0;
        for (int i = 0; i < n; i++) {
            char c = sc.next().charAt(0);
            if (c == '+') {
                counts[10]++;
                oCount++;
            } else if (c == '-') {
                counts[11]++;
                oCount++;
            } else {
                counts[c - '0']++;
                nCount++;
            }
        }
        check(0, 0, oCount, nCount, counts, true, true);
        System.out.println(max + " " + min);
    }
    
    static void check(long total, long current, int oCount, int nCount, int[] counts, boolean isPlus, boolean needNum) {
        if (oCount == 0 && nCount == 0) {
            if (isPlus) {
                total += current;
            } else {
                total -= current;
            }
            max = Math.max(total, max);
            min = Math.min(total, min);
            return;
        }
        if (oCount < nCount) {
            for (int i = 0; i < 10; i++) {
                if (counts[i] == 0) {
                    continue;
                }
                counts[i]--;
                check(total, current * 10 + i, oCount, nCount - 1, counts, isPlus, false);
                counts[i]++;
            }
        }
        if (needNum) {
            return;
        }
        if (oCount > 0) {
            long next = total;
            if (isPlus) {
                next += current;
            } else {
                next -= current;
            }
            if (counts[10] > 0) {
                counts[10]--;
                check(next, 0, oCount - 1, nCount, counts, true, true);
                counts[10]++;
            }
            if (counts[11] > 0) {
                counts[11]--;
                check(next, 0, oCount - 1, nCount, counts, false, true);
                counts[11]++;
            }
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0