結果
| 問題 |
No.297 カードの数式
|
| コンテスト | |
| ユーザー |
uafr_cs
|
| 提出日時 | 2015-11-06 23:23:53 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,584 bytes |
| コンパイル時間 | 2,624 ms |
| コンパイル使用メモリ | 87,692 KB |
| 実行使用メモリ | 55,052 KB |
| 最終ジャッジ日時 | 2024-09-13 13:49:50 |
| 合計ジャッジ時間 | 7,395 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 WA * 5 |
ソースコード
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(int n_count, int[] numbers, int o_count, int[] ops) {
long ret = 0;
for(int i = 9, rest = n_count - o_count, plus = ops[0]; i >= 0; i--){
final int times = Math.min(numbers[i], rest);
rest -= times;
for(int j = 0; j < times; j++){
ret *= 10;
ret += i;
}
for(int j = 0; j < (numbers[i] - times); j++){
if(plus > 0){
plus--;
ret += i;
}else{
ret -= i;
}
}
}
return ret;
}
public static long estimate_min(int n_count, int[] numbers, int o_count, int[] ops) {
long ret = 0;
if(ops[1] > 0){
int min_value = -1;
for(int i = 0; i < 10; i++){
if(numbers[i] > 0){
min_value = i;
break;
}
}
ops[1]--;
numbers[min_value]--;
o_count--;
n_count--;
ret = min_value;
long minus_value = 0;
for(int i = 9, rest = n_count - o_count, minus = ops[1]; i >= 0; i--){
final int times = Math.min(numbers[i], rest);
rest -= times;
for(int j = 0; j < times; j++){
minus_value *= 10;
minus_value += i;
}
for(int j = 0; j < (numbers[i] - times); j++){
if(minus > 0){
minus--;
ret -= i;
}else{
ret += i;
}
}
}
ret -= minus_value;
ops[1]++;
numbers[min_value]++;
o_count++;
n_count++;
}else{
for(int i = 0, rest = n_count - o_count; i < 10; i++){
final int times = Math.min(numbers[i], rest);
rest -= times;
for(int j = 0; j < times; j++){
ret *= 10;
ret += i;
}
for(int j = 0; j < (numbers[i] - times); j++){
ret += i;
}
}
}
return ret;
}
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++;
}
}
System.out.println(estimate_max(n_count, numbers, o_count, ops) + " " + estimate_min(n_count, numbers, o_count, ops));
}
}
uafr_cs