結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー GrenacheGrenache
提出日時 2015-12-21 20:51:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 336 ms / 5,000 ms
コード長 2,543 bytes
コンパイル時間 5,839 ms
コンパイル使用メモリ 80,532 KB
実行使用メモリ 59,684 KB
最終ジャッジ日時 2023-10-18 22:06:32
合計ジャッジ時間 6,814 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 320 ms
59,340 KB
testcase_01 AC 336 ms
59,684 KB
testcase_02 AC 259 ms
59,296 KB
testcase_03 AC 316 ms
59,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;


public class Main_yukicoder120 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int t = sc.nextInt();
        for (int tcase = 0; tcase < t; tcase++) {
        	int n = sc.nextInt();
        	Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
        	for (int i = 0; i < n; i++) {
        		int l = sc.nextInt();
        		if (!hm.containsKey(l)) {
        			hm.put(l, 1);
        		} else {
        			hm.put(l, hm.get(l) + 1);
        		}
        	}

        	PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
        	pq.addAll(hm.values());
        	int ret = 0;
        	while (pq.size() >= 3) {
        		int tmp1 = pq.remove();
        		int tmp2 = pq.remove();
        		int tmp3 = pq.remove();

        		ret++;
        		if (tmp1 - 1 > 0) {
        			pq.add(tmp1 - 1);
        		}
        		if (tmp2 - 1 > 0) {
        			pq.add(tmp2 - 1);
        		}
        		if (tmp3 - 1 > 0) {
        			pq.add(tmp3 - 1);
        		}
        	}

        	pr.println(ret);
        }

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0