ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Task 'gift1': Greedy Gift Givers /c++
    ALGORITHM/USACO 2019. 8. 11. 02:44
    728x90

    problem link

    #include <fstream>
    #include <string>
    #include <map>
    using namespace std;
    
    int main() {
        ofstream fout("gift1.out");
        ifstream fin("gift1.in");
        int NP;
        map <string, int> m;
        string temp[20];
        fin >> NP;
        for (int i = 0; i < NP; i++) {
            fin >> temp[i];
            m.insert(pair<string, int>(temp[i], 0));
        }
        for (int i = 0; i < NP; i++) {
            string temp;
            int x;
            int y;
            fin >> temp;
            fin >> x >> y;
            if (y != 0) {
                string name = m.find(temp)->first;
                int leftover = x % y;
                m.at(name) -= x - leftover;
                for (int i = 0; i < y; i++) {
                    string temp2;
                    fin >> temp2;
                    m.at(m.find(temp2)->first) += x / y;
                }
            }
    
        }
    
        for (int i = 0; i < NP; i++) {
            fout << m.find(temp[i])->first << " " << m.find(temp[i])->second << endl;
        }
    }

    map 을 사용하면 쉽습니다.

    'ALGORITHM > USACO' 카테고리의 다른 글

    Solving the usaco transform problem in c++  (0) 2019.08.11
    Milking Cows /c++  (0) 2019.08.11
    Broken Necklace /c++  (0) 2019.08.11
    Friday the Thirteenth /c++  (0) 2019.08.11
    your ride is here c++  (0) 2019.08.11

    댓글

Designed by Tistory.