結果

問題 No.349 干支の置き物
ユーザー gam0022gam0022
提出日時 2016-05-01 21:34:25
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,115 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 85,244 KB
実行使用メモリ 6,816 KB
最終ジャッジ日時 2024-04-16 16:50:07
合計ジャッジ時間 1,839 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <list>
#include <deque>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

void print_map(map<string, int> m) {
    for(auto itr = m.begin(); itr != m.end(); ++itr) {
        std::cout << "key = " << itr->first
            << ", val = " << itr->second << "\n";
    }
}

void print_vector(vector<int> v) {
    for(auto itr = v.begin(); itr != v.end(); ++itr) {
        std::cout << *itr << ", ";
    }
    cout << endl;
}

vector<int> get_vals_from_map(map<string, int> m) {
    vector<int> vals;

    for(auto itr = m.begin(); itr != m.end(); ++itr) {
        vals.push_back(itr->second);
    }

    return vals;
}

void check(vector<int> counts) {
    int max = 0;
    int rest = 0;

    int n = counts.size();
    for(int i = 0; i < n; ++i) {
        int count = counts[i];
        if (i < n - 1) {
            rest += count;
        } else {
            max += count;
        }
    }

    //cout << "max: " << max << endl;
    //cout << "rest:" << rest << endl;

    if ( max == rest || max == rest - 1 || max == rest + 1) {
        cout << "YES" << endl;
        return;
    } else if (max + 1 > rest) {
        cout << "NO" << endl;
    } else {
        counts.pop_back();
        int n = counts.size();
        //print_vector(counts);
        int i = n - 1;
        while(max > 0) {
            i = i % n;
            if (counts[i] > 0) {
                --counts[i];
                --max;
            }
            --i;
        }
        //print_vector(counts);
        counts.erase(remove(counts.begin(), counts.end(), 0), counts.end());
        sort(counts.begin(), counts.end());
        //print_vector(counts);
        //cout << "--" << endl;
        check(counts);
    }
}

int main(){
    int n;
    map<string, int> etos;
    cin >> n;
    for (int i = 0; i < n; i++) {
        string eto;
        cin >> eto;
        ++etos[eto];
    }

    //print_map(etos);
    vector<int> counts = get_vals_from_map(etos);
    sort(counts.begin(), counts.end());

    check(counts);

    return 0;
}
0