結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー finefine
提出日時 2016-03-28 01:52:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 1,267 bytes
コンパイル時間 1,718 ms
コンパイル使用メモリ 168,028 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 04:15:02
合計ジャッジ時間 2,585 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
6,812 KB
testcase_01 AC 24 ms
6,816 KB
testcase_02 AC 13 ms
6,944 KB
testcase_03 AC 25 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int sol(priority_queue< pair<int, int> >& pq){
    int ans = 0;
    pair<int, int> x[3];
    while(!pq.empty()) {
        for(int i = 0; i < 3; i++) {
            if(pq.empty()) return ans;
            x[i] = pq.top();
            pq.pop();
        }
        for(int i = 0; i < 3; i++) {
            if (x[i].first > 1) {
                x[i].first--;
                pq.push(x[i]);
            }
        }
        ans++;
    }
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t, n, tmp = 0, count = 1;
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> n;
        vector<int> a(n);
        priority_queue< pair<int, int> > pq;
        for(int j = 0; j < n; j++) {
            cin >> a[j];
        }
        sort(a.begin(), a.end());
        for(int j = 0; j < n; j++) {
            if (tmp == 0) {
                tmp = a[j];
            } else if (tmp == a[j]) {
                count++;
            } else {
                pq.push(make_pair(count, tmp));
                tmp = a[j];
                count = 1;
            }
        }
        pq.push(make_pair(count, tmp));
        tmp = 0;
        count = 1;
        cout << sol(pq) << "\n";
    }
    return 0;
}
0