본문 바로가기

백준/실버

[백준 2841번] 외계인의 기타 연주 (C++)

문제링크 : https://www.acmicpc.net/problem/2841

#include <bits/stdc++.h> 
using namespace std;

int N, P;
stack<int>play[7];
int moving = 0;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> N >> P;
	while (N--)
	{
		int a, b;
		cin >> a >> b;
		if (play[a].empty())  //해당 줄이 비어있다면 해당 플렛 푸쉬
		{
			play[a].push(b);
			moving++;
		}
		else if (play[a].top() < b)  //뒤에 값이 더 크다면 해당 플렛 푸쉬
		{
			play[a].push(b);
			moving++;
		}
		else if (play[a].top() > b)  //뒤에 값이 더 작다면
		{
			while (play[a].top() > b)  //앞에 값이 더 작아질 때까지 팝
			{
				play[a].pop();
				moving++;
				if (play[a].empty() || play[a].top() < b)  //해당 줄이 비거나, 뒤에 값이 더 커진다면 푸쉬
				{
					play[a].push(b);  
					moving++;
				}
			}
		}
	}
	cout << moving;
}

처음에 문제를 이해하는 것만 다소 헷갈렸다.

조건을 세우고, 푸쉬할때, 팝을 할때마다 움직임을 증가시켜주면 된다.