結果
| 問題 |
No.297 カードの数式
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-01-06 10:56:32 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 TLE * 4 |
ソースコード
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();
}
}
tenten