자라는 개발자/문제풀이

백준 c++ 21156 A Rank Problem

자란다 2022. 1. 21. 15:37
728x90
반응형

21156 A Rank Problem

문제풀이

#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
void fast_io(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main()
{
    fast_io();
    list<int> l;
    std::list<int>::iterator it;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        l.push_back(i);
    while (m--)
    {
        it = l.begin();
        string a, b;
        cin >> a >> b;
        int winner = stoi(a.substr(1));
        int loser = stoi(b.substr(1));
        int i = 0, ck = 0;
        for (auto it = l.begin(); it != l.end(); it++)
        {
            if (*it == loser && i == 1)
            {
                ck = 1;
                break;
            }
            else if (*it == winner)
                i = 1;
        }
        if (ck == 0)
        {
            while (it != l.end())
            {
                if (*it == loser)
                {
                    it = l.erase(it++);
                }
                else if (*it == winner)
                {
                    l.insert(++it, loser);
                }
                else
                {
                    it++;
                }
            }
        }
    }
    for (auto it = l.begin(); it != l.end(); it++)
        cout << "T" << *it << " ";
}

왼쪽이 이겼어도 이미 리스트에서 앞에있다면 원소값 변화를 주지 않아도 될때를 어떻게 할지 고민했다.

728x90
반응형

'자라는 개발자 > 문제풀이' 카테고리의 다른 글

백준 c++ 1120 문자열  (0) 2022.01.24
백준 C++ 1439 뒤집기  (0) 2022.01.23
백준 c++ 10104 Party Invitation  (1) 2022.01.20
백준 c++ 1235 학생 번호  (0) 2022.01.19
백준 c++ 1205번 등수 구하기  (0) 2022.01.19