알고리즘
백준 17178번 줄서기 C++
영춘권의달인
2022. 11. 2. 12:18
https://www.acmicpc.net/problem/17178
17178번: 줄서기
아이즈원의 팬인 시온이는 드디어 티켓팅에 성공하여 콘서트를 갔다. 콘서트장에 일찍 도착한 시온이는 기대하며 입장을 위해 줄을 섰다. 하지만 아이즈원의 인기대로 시온이를 포함한 많은
www.acmicpc.net
스택, 시뮬레이션
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
struct ticketInfo {
char alp;
int num;
bool operator<(const ticketInfo& other) const{
if (alp != other.alp) return alp < other.alp;
return num < other.num;
}
bool operator>(const ticketInfo& other) const{
if (alp != other.alp) return alp > other.alp;
return num > other.num;
}
bool operator==(const ticketInfo& other) const {
if (alp == other.alp && num == other.num) return true;
return false;
}
};
vector<ticketInfo> sortV;
vector<vector<ticketInfo>> v;
stack<ticketInfo> st;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
v = vector<vector<ticketInfo>>(n, vector<ticketInfo>(5));
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
string str;
cin >> str;
string tmp = "";
char alp = str[0];
for (int k = 2; k < str.length(); k++) tmp += str[k];
int num = stoi(tmp);
v[i][j] = { alp,num };
sortV.push_back({ alp,num });
}
}
sort(sortV.begin(), sortV.end());
int idx = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
while (!st.empty()) {
if (st.top() == sortV[idx]) {
st.pop();
idx++;
}
else break;
}
if (v[i][j] == sortV[idx]) {
idx++;
continue;
}
else if (v[i][j] > sortV[idx]) {
st.push(v[i][j]);
}
}
}
while (!st.empty()) {
if (st.top() == sortV[idx]) {
st.pop();
idx++;
}
else break;
}
if (idx == n * 5) cout << "GOOD";
else cout << "BAD";
}