CSES - Datatähti 2024 loppu - Results
Submission details
Task:Sukat
Sender:ToukoP
Submission time:2024-01-20 15:51:17 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

In file included from /usr/include/c++/11/map:61,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:81,
                 from input/code.cpp:1:
/usr/include/c++/11/bits/stl_map.h: In instantiation of 'void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = int; _Key = int; _Tp = int; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, int> >]':
input/code.cpp:31:21:   required from here
/usr/include/c++/11/bits/stl_map.h:894:38: error: no matching function for call to 'std::_Rb_tree<int, std::pair<const int, int>, std::_Select1st<std::pair<const int, int> >, std::less<int>, std::allocator<std::pair<const int, int> > >::_M_insert_range_unique(int&, int&)'
  894 |         { _M_t._M_insert_range_unique(__first, __last); }
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/s...

Code

#include <bits/stdc++.h>

using namespace std;

int main() {
    const int Z = 30;
    string str;
    cin >> str;
    
    vector<int> sukat;

    for (int i = 0; i < (int) str.length(); i++) {
        sukat.push_back((int) str[i]);
    }
    sort(sukat.begin(), sukat.end());

    map<int, int> m;
    int c1 = 0;
    int c2 = 0;

    int last = -1;
    bool g = true;
    for (int i = 0; i < (int) sukat.size(); i++) {
        //cout << sukat[i] << " ";
        int c = ((int) sukat[i]) - 65;
        if (c != last) {
            g = c1 < c2;
            last = c;
        }
        if (m.count(c) == 0) {
            m.insert(c, 0);
        }
        if (g) {
            m.at(c)++;
            c1++;
        } else {
            m.at(c)++;
            c2++;
       }
    }
    
    //cout << endl;

    //cout << s1[0] << s1[1] << s1[2] << endl;
    //cout << s2[0] << s2[1] << s2[2] << endl;

    vector<pair<char, char>> res;

    while (c1 > 0 && c2 > 0) {
        int max1 = -1;
        int index1 = -1;
        int max2 = -1;
        int index2 = -1;
        for (int i = 0; i < Z; i++) {
            int c = m.at(i);
            if (c > max1) {
                max2 = max1;
                index2 = index1;
                max1 = c;
                index1 = i;
            } else if (c > max2) {
                max2 = c;
                index2 = i;
            }
        }

        //cout << "i1: " << index1 << " i2: " << index2 << endl;

        int a = min(max1, max2);
        m.at(index1) -= a;
        m.at(index2) -= a;
        c1 -= a;
        c2 -= a;
        
        for (int i = 0; i < a; i++) {
            res.push_back({ (char) (index1 + 65), (char) (index2 + 65)});
        }
    }

    cout << res.size() << "\n";
    for (int i = 0; i < (int) res.size(); i++) {
        cout << res[i].first << res[i].second << "\n"; 
    }
}