結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー RCCWRCCW
提出日時 2017-03-04 22:30:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 1,283 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 156,416 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 05:39:10
合計ジャッジ時間 2,119 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
4,376 KB
testcase_01 AC 52 ms
4,380 KB
testcase_02 AC 27 ms
4,380 KB
testcase_03 AC 53 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
/*
余っている数の多いものから、貪欲に取って行けば間に合います!
貪欲に取るのは、優先度付きキューを使えば簡単ですね
*/

void calc() {
	int N;
	cin >> N;
	vector<int> L(N);
	//mは結局、数字のカウンター。同じものがあればプラスしていく。
	map<int, int> m;
	for (int i = 0; i < N; i++)
	{
		cin >> L[i];
		m[L[i]]++;
	}
	//これで数値の大きいものから格納していく。
	priority_queue<int> pq;
	//for autoで変数のsecondの値をpushしていく。
	for (auto i : m){
		pq.push(i.second);
	}

	if (m.size() < 3){
		cout << 0 << endl;
		return;
	}
	//top.popのpqやることで3つが1つ以上であればans増やし、pqを減らしまた挿入
	//0になったときは終了のとき
	int ans = 0;
	while (true){
		int a = pq.top(); pq.pop();
		int b = pq.top(); pq.pop();
		int c = pq.top(); pq.pop();
		if (c <= 0){
			cout << ans << endl;
			return;
		}
		ans++;
		pq.push(a - 1);
		pq.push(b - 1);
		pq.push(c - 1);
	}
}
//関数で飛ばすのは良い方法(testcaseが何回もある場合は)
//リバエンを何回もやってたら力つく。
int main(){
	int T;
	cin >> T;
	for (int i = 0; i < T; i++)
	{
		calc();
	}
}
0