https://www.acmicpc.net/problem/2841
이 문제를 잘 이해하면, 예제가 잘 이해될 것이다.
7 15
1 5 => 1회
2 3 => 1회
2 5 => 1회
2 7 => 1회
2 4 => 3회 => 7번, 5번 플렛 뗌 4번 플렛을 누름
1 5 => 1회 => 눌려있음. (cnt 제외)
1 3 => 2회 => 5번 플렛 떼고 3번 플렛 누름
또한, 총 6개의 줄이있기떄문에, stack 6개를 생성하는 것보다 stack 1개에 pair<first,second>로 묶어서 정리하는것이 효율적이다.
#include<iostream>
using namespace std;
#include<stack>
stack <pair<int, int>> s[8];
int n,p,cnt;
int main()
{
cin >> n>>p;
pair <int, int> p;
for (int i = 0; i < n; i++)
{
cin >> p.first >> p.second;
if (s[p.first].empty()) //비어 있다면 넣고,
{
s[p.first].push(p);
cnt++;
}
if (s[p.first].top().second < p.second) { //숫자가 클 경우 (ex) 1 3 / 1 5 순으로 들어오면 3<5)
s[p.first].push(p);
cnt++;
}
else if (s[p.first].top().second == p.second) {
continue;
}
else
{
while (!s[p.first].empty() && s[p.first].top().second > p.second) { // 숫자가 작을 경우 ex) 1 5 / 1 3 순으로 들어올 경우)
s[p.first].pop();
cnt++;
}
if(!s[p.first].empty()) // s[p.first].empty()유무 파악후, 비어있지않으면 같지않은지를 비교해야한다. 같으면 넣지못하니까
{
if (s[p.first].top().second != p.second)
{
s[p.first].push(p);
cnt++;
}
}
else if (s[p.first].empty())
{
s[p.first].push(p);
cnt++;
}
}
}
cout << cnt << "\n";
return 0;
}
'백준_문제풀이' 카테고리의 다른 글
[백준]: 10610번: 30 / 탐욕적 기법(Greedy Algorithm) (0) | 2018.10.04 |
---|---|
[백준]: 5557번: 1학년 / DP(동적프로그래밍) (0) | 2018.09.27 |
[백준]: 1918번: 후위표기식 / Stack (0) | 2018.09.23 |
[백준]: 12842번: 튀김 소보루 / 단순 반복 (0) | 2018.09.16 |
[백준]: 2150번: Strongly Connected Component / 코사리주 알고리즘 (0) | 2018.09.16 |