Click Here to go back to the homepage.

Warehouse Solution:


#include <bits/stdc++.h>
// using namespace std;

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    typedef std::function<bool(std::pair<std::string, int>, std::pair<std::string, int>)> Comparator;

    Comparator comparator = [](std::pair<std::string, int> p1, std::pair<std::string, int> p2){
        if(p1.second == p2.second){
            return p1.first < p2.first;
        } else {
            return p1.second > p2.second;
        }
    };

    int cases;
    std::cin >> cases;
    while(cases--){
        std::unordered_map<std::string, int> map;
        std::string name;
        int shipments, quantity;
        std::cin >> shipments;
        while(shipments--){
            std::cin >> name >> quantity;
            std::unordered_map<std::string, int>::iterator it = map.find(name);
            if(it == map.end()){
                map.insert(std::make_pair(name, quantity));
            } else {
                (*it).second += quantity;
            }
        }

        std::set<std::pair<std::string, int>, Comparator> set(map.begin(), map.end(), comparator);

        std::cout << map.size() << std::endl;
        for(std::pair<std::string, int> i: set){
            std::cout << i.first << " " << i.second << std::endl;
        }
    }

    return 0;
}