結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー uafr_csuafr_cs
提出日時 2015-09-02 16:25:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 738 ms / 5,000 ms
コード長 1,688 bytes
コンパイル時間 2,078 ms
コンパイル使用メモリ 76,056 KB
実行使用メモリ 61,248 KB
最終ジャッジ日時 2023-09-25 22:12:15
合計ジャッジ時間 6,267 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 682 ms
61,156 KB
testcase_01 AC 738 ms
60,880 KB
testcase_02 AC 558 ms
60,700 KB
testcase_03 AC 707 ms
61,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;

public class Main {
	
	public static class Bamboo implements Comparable<Bamboo> {
		int len, count;

		public Bamboo(int len, int count) {
			super();
			this.len = len;
			this.count = count;
		}

		@Override
		public int compareTo(Bamboo o) {
			if(Integer.compare(o.count, this.count) != 0){
				return Integer.compare(o.count, this.count);
			}else{
				return Integer.compare(this.len, o.len);
			}
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int T = sc.nextInt();
		for(int tt = 0; tt < T; tt++){
			final int N = sc.nextInt();
			
			Map<Integer, Integer> map = new HashMap<Integer, Integer>();
			for(int i = 0; i < N; i++){
				final int l = sc.nextInt();
				
				if(!map.containsKey(l)){
					map.put(l, 0);
				}
				map.put(l, map.get(l) + 1);
			}
			
			
			PriorityQueue<Bamboo> queue = new PriorityQueue<Bamboo>();
			for(Entry<Integer, Integer> entry : map.entrySet()){
				queue.add(new Bamboo(entry.getKey(), entry.getValue()));
			}
			
			int count = 0;
			while(queue.size() >= 3){
				final Bamboo fst = queue.poll();
				final Bamboo snd = queue.poll();
				final Bamboo thd = queue.poll();
				
				
				count++;
				if(fst.count > 1){
					fst.count--;
					queue.add(fst);
				}
				if(snd.count > 1){
					snd.count--;
					queue.add(snd);
				}
				if(thd.count > 1){
					thd.count--;
					queue.add(thd);
				}
			}
			
			System.out.println(count);
		}
		
	}
	
}
0