結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー codershifthcodershifth
提出日時 2015-10-12 10:43:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 2,421 bytes
コンパイル時間 1,348 ms
コンパイル使用メモリ 157,372 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 12:38:14
合計ジャッジ時間 2,158 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(int)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

class TrendAndCountermeasures_PineDecorationSequence1 {
public:
    void solve(void) {
            int T;
            cin>>T;
            // O(T*N^2)
            REP(t,T)
            {
                //
                // 5 5 5 5 4 4 4 3 2 9 1
                // [5,4,3] [5,4,2] [5,4,9]  ... 5,1
                //
                // 同じものの数が多いものが残ってしまうと、作れる門松の数が減ってしまう。
                // [5,4,3] [2,9,1] ... 5,5,5,4,4
                //
                // よって数が多いものから貪欲に取っていけばよい。
                //
                int N;
                cin>>N;

                map<int,int> degree;
                REP(i,N)
                {
                    int l;
                    cin>>l;
                    degree.emplace(l,0);
                    ++degree[l];
                }
                priority_queue<int> pq;
                for (auto kv : degree)
                    pq.push(kv.second);

                if (pq.size() < 3)
                {
                    cout<<0<<endl;
                    continue;
                }

                int cnt = 0;
                while (true)
                {
                    // 3つ連続で取り出すことで門松の高さの重複を防ぐ
                    int a = pq.top(); pq.pop();
                    int b = pq.top(); pq.pop();
                    int c = pq.top(); pq.pop();

                    // 竹が足りなくて門松が作れないとき
                    if (c <= 0) // a > b > c の順なので c でチェックすれば十分
                    {
                        cout<<cnt<<endl;
                        break;
                    }
                    ++cnt;
                    // 個数を減らして再度 push
                    pq.push(a-1);
                    pq.push(b-1);
                    pq.push(c-1);
                }
            }
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new TrendAndCountermeasures_PineDecorationSequence1();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0