結果

問題 No.297 カードの数式
ユーザー tentententen
提出日時 2023-01-06 10:56:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,422 bytes
コンパイル時間 2,961 ms
コンパイル使用メモリ 95,292 KB
実行使用メモリ 104,228 KB
最終ジャッジ日時 2024-11-30 05:46:54
合計ジャッジ時間 14,289 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
58,124 KB
testcase_01 AC 81 ms
104,016 KB
testcase_02 AC 81 ms
58,008 KB
testcase_03 AC 82 ms
103,772 KB
testcase_04 TLE -
testcase_05 AC 84 ms
104,228 KB
testcase_06 AC 81 ms
57,916 KB
testcase_07 AC 84 ms
51,752 KB
testcase_08 AC 81 ms
51,284 KB
testcase_09 AC 81 ms
51,236 KB
testcase_10 AC 81 ms
53,280 KB
testcase_11 AC 84 ms
51,628 KB
testcase_12 AC 94 ms
52,136 KB
testcase_13 AC 128 ms
53,468 KB
testcase_14 AC 131 ms
53,392 KB
testcase_15 AC 117 ms
53,324 KB
testcase_16 TLE -
testcase_17 AC 118 ms
53,440 KB
testcase_18 AC 90 ms
51,632 KB
testcase_19 AC 94 ms
53,224 KB
testcase_20 AC 112 ms
53,184 KB
testcase_21 AC 80 ms
51,368 KB
testcase_22 AC 95 ms
52,072 KB
testcase_23 AC 85 ms
51,628 KB
testcase_24 TLE -
testcase_25 TLE -
権限があれば一括ダウンロードができます

ソースコード

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