結果

問題 No.349 干支の置き物
ユーザー jp_stejp_ste
提出日時 2016-05-03 00:25:57
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,720 bytes
コンパイル時間 2,441 ms
コンパイル使用メモリ 79,676 KB
実行使用メモリ 41,760 KB
最終ジャッジ日時 2024-04-16 12:23:38
合計ジャッジ時間 7,585 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
41,172 KB
testcase_01 AC 133 ms
41,380 KB
testcase_02 AC 132 ms
41,156 KB
testcase_03 AC 137 ms
41,280 KB
testcase_04 AC 140 ms
41,412 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 132 ms
41,096 KB
testcase_19 AC 133 ms
41,168 KB
testcase_20 AC 133 ms
41,600 KB
testcase_21 AC 133 ms
41,248 KB
testcase_22 AC 132 ms
41,080 KB
testcase_23 AC 131 ms
41,236 KB
testcase_24 AC 134 ms
41,252 KB
testcase_25 AC 119 ms
40,196 KB
testcase_26 AC 133 ms
41,480 KB
testcase_27 AC 137 ms
41,252 KB
testcase_28 AC 133 ms
41,268 KB
testcase_29 AC 139 ms
41,080 KB
testcase_30 AC 136 ms
41,364 KB
testcase_31 AC 136 ms
41,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            int N = scan.nextInt();
            HashMap<String, Integer> map = new HashMap<>();
            for(int i=0; i<N; i++) {
                String key = scan.next();
                Integer value = map.get(key);
                if(value == null) {
                    map.put(key, 1);
                } else {
                    map.replace(key, value+1);
                }                
            }
            
            ArrayList<Integer> array = new ArrayList<>();
            for(Entry entry : map.entrySet()) {
                array.add((Integer)entry.getValue());
            }
                        
            while(true) {
                
                if(array.size() == 1) {
                    System.out.println("NO");
                    break;
                }
                
                int max = 0;
                int index = 0;
                int zeroCount = 0;
                for(int i=0; i<array.size(); i++) {
                    Integer v = array.get(i);
                    if(v == 0) {
                        zeroCount++;
                    }
                    
                    if( max < v ) {
                        max = v;
                        index = i;
                    }
                }
                
                if(zeroCount == array.size()) {
                    System.out.println("YES");
                    break;
                }
                if(zeroCount == array.size()-1) {
                    if(max == 1) {
                        System.out.println("YES");
                    } else {
                        System.out.println("NO");
                    }
                    break;
                }
                
                int sum = 0;
                int rem = max;
                if(rem % 2 == 1) {
                    rem--;
                }
                
                for(int i=0; i<array.size(); i++) {
                    if(i == index) continue;
                    Integer v = array.get(i);
                    if(v == 0) continue;
                    sum += v;
                    if(rem > sum) {
                        array.set(i, 0);
                        int t = array.get(index);
                        array.set(index, t-v);
                    } else {
                        array.set(i, sum-rem);
                        array.set(index, 0);
                        break;
                    }
                }
            } 
        }
    }
}
0