結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー YamaKasaYamaKasa
提出日時 2018-07-12 19:37:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 748 ms / 5,000 ms
コード長 1,904 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 80,860 KB
実行使用メモリ 61,336 KB
最終ジャッジ日時 2024-04-14 06:00:13
合計ジャッジ時間 6,760 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 748 ms
60,764 KB
testcase_01 AC 748 ms
61,316 KB
testcase_02 AC 595 ms
59,836 KB
testcase_03 AC 745 ms
61,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int T = scan.nextInt();
		int []ans = new int[T];
		Arrays.fill(ans, 0);
		ArrayList<Long> list = new ArrayList<Long>();
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for(int i = 0; i < T; i++) {
			int N = scan.nextInt();
			list.clear();
			map.clear();
			for(int j = 0; j < N; j++) {
				int L = scan.nextInt();
				if(map.containsKey(L)) {
					map.put(L, map.get(L) + 1);
				}else {
					map.put(L, 1);
				}
			}


			Data []data = new Data[map.size()];
			int cnt = 0;
			for(int key : map.keySet()) {
				data[cnt] = new Data(key, map.get(key));
				cnt ++;
			}
			PriorityQueue<Data> pq = new PriorityQueue<Data>(new Comparator<Data>(){
				@Override
				public int compare(Data d1, Data d2) {
					return d2.getB() - d1.getB();
				}
			});
			for(int j = 0; j < data.length; j++) {
				pq.add(data[j]);
			}
			int cnt1 = 0;
			while (pq.size() >= 3) {
				cnt1 ++;
				Data d1 = pq.poll();
				Data d2 = pq.poll();
				Data d3 = pq.poll();
				if(d1.getB() - 1 > 0) {
					d1.setB(d1.getB() - 1);
					pq.add(d1);
				}
				if(d2.getB() - 1 > 0) {
					d2.setB(d2.getB() - 1);
					pq.add(d2);
				}
				if(d3.getB() - 1 > 0) {
					d3.setB(d3.getB() - 1);
					pq.add(d3);
				}
			}
			ans[i] = cnt1;
		}
		scan.close();
		for(int i = 0; i < T; i++) {
			System.out.println(ans[i]);
		}
	}
}

class Data{
    private long a;
    private int b;
    public Data(long a, int b) {
        this.a = a;
        this.b = b;
    }
    public long getA() {
        return a;
    }
    public int getB() {
        return b;
    }
    public void setB(int b) {
    	this.b = b;
    }
}
0