結果
| 問題 | No.120 傾向と対策:門松列(その1) | 
| コンテスト | |
| ユーザー |  uafr_cs | 
| 提出日時 | 2015-09-02 16:25:17 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 765 ms / 5,000 ms | 
| コード長 | 1,688 bytes | 
| コンパイル時間 | 2,236 ms | 
| コンパイル使用メモリ | 79,588 KB | 
| 実行使用メモリ | 49,600 KB | 
| 最終ジャッジ日時 | 2024-07-18 17:51:51 | 
| 合計ジャッジ時間 | 6,310 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 4 | 
ソースコード
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);
		}
		
	}
	
}
            
            
            
        