성우리뷰

구조체 우선순위큐 넣고 정렬 & vector pair의 정렬

두원공대88학번뚜뚜 2021. 10. 27. 18:52
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>

using namespace std;

int sdoku[9][9];
int column[9][9];
int row[9][9];
int group[9][9];

#define TRUE 1
#define FALSE 0

struct vaccine {
    int since;
    int until;
    int count;
};
//vector<vaccine> vacc;
map<string, vaccine> mapp;

struct man {
    string name;
    int age;
    string want;
};

/********우선순위큐, 구조체를 age 큰것부터 대입********/
struct compare {
    bool operator() (const man& m1, const man& m2) {
        return m1.age < m2.age;
    }
};
priority_queue<man, vector<man>, compare> mann;
/*********우선순위큐********/

struct suc {
    string name;
    string vac;
};
vector<suc> succ;
vector <pair<string, string>> v;

void vacparser(string str) {
    string name; int since; int until; int count; vaccine vc;
    string tmp; int flag = 0;
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == ' ') {
            if (flag == 0) {
                //이름
                name = tmp;
            }
            else if (flag == 1) {
                //숫자
                count = stoi(tmp);
            }
            else if (flag == 2) {
                //첫나이
                since = stoi(tmp);
            }
            flag++;
            tmp = "";
        }
        else {
            tmp += str[i];
        }
    }
    until = stoi(tmp);
    vc.since = since; vc.until = until; vc.count = count;
 //   vacc.push_back(vc);
    mapp[name]=vc;

    return;
}

void peoparser(string str) {
    string name; int age; string want; man mannn;
    string tmp; int flag = 0;
    for (int i = 0; i < str.size(); i++) {

        if (str[i] == ' ' && flag == 0) {
            //이름
            name = tmp;
            tmp = "";
            flag++;
        }
        else if (str[i] == ' ' && flag == 1) {
            //숫자
            age = stoi(tmp);
            tmp = "";
            flag++;
        }
        else {
            tmp += str[i];
        }
    }
    want = tmp;
    mannn.name = name; mannn.age = age; mannn.want = want;
    mann.push(mannn);

    return;
}

void func(man m) {
    string want = m.want;
    int age = m.age;

    string tmp;
    for (int i = 0; i < want.size(); i++) {
        if (want[i] == ' ') {
            //나이도 되고, 숫자도 있음
            if (mapp[tmp].since <= age && mapp[tmp].until >= age && mapp[tmp].count > 0) {
                v.push_back(make_pair(tmp, m.name));
   //             cout << tmp << " " << m.name << " " << mapp[tmp].count << endl;
                mapp[tmp].count--;
                return;
            }
            tmp = "";
        }
        else {
            tmp +=  want[i];
        }
    }
    if (mapp[tmp].since <= age && mapp[tmp].until >= age && mapp[tmp].count > 0) {
        v.push_back(make_pair(tmp, m.name));
        mapp[tmp].count--;
    }

    return;
}

int solve(vector<string> vac, vector<string> peo) {
    //vac
    for (int i = 0; i < vac.size(); i++) {
        vacparser(vac[i]);
    }

    for (int i = 0; i < peo.size(); i++) {
        peoparser(peo[i]);
    }

    while(!mann.empty()) {
        func(mann.top());
        mann.pop();
    }

    return 0;
}

int main(void){
        // your code goes here
    vector<string> vac = { "fizer 3 20 99", "astra 1 9 55", "yansen 10000 22 49" };
    vector<string> peo = { "susan 50 astra yansen fizer", "kevin 55 astra", "luka 60 fizer astra",
    "erika 20 yansen fizer astra", "roy 20 fizer" };

   // cout << peo.size() << " " << mann.size() << endl;
       solve(vac, peo);
       sort(v.begin(), v.end());
       cout << v[0].first << " " << v[0].second << " ";
       for (int i = 1; i < v.size(); i++) {
           if (v[i].first == v[i - 1].first) {
               cout <<  v[i].second << " ";
           }
           else {
               cout << "\n" << v[i].first << " " << v[i].second << " ";
           }
       }
       return 0;
 }

pair 혹은 map 등에서, value 순으로 정렬하는 경우

bool cmp(const pp& a, const pp& b) {
	if (a.second == b.second) return a.first < b.first;
	return a.second < b.second;
}

 second가 같은 경우에만 a.first < b.first를 통해 하며,

아닌 경우 a.second < b.second를 통해 second를 작은 거부터 정렬