하루 1문제 챌린지/Silver1

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

그린푸딩 2024. 2. 16. 16:14
728x90
반응형

https://www.acmicpc.net/problem/2841

 

2841번: 외계인의 기타 연주

첫째 줄에 멜로디에 포함되어 있는 음의 수 N과 한 줄에 있는 프렛의 수 P가 주어진다. (1 ≤ N ≤ 500,000, 2 ≤ P ≤ 300,000) 다음 N개 줄에는 멜로디의 한 음을 나타내는 두 정수가 주어진다. 첫 번째

www.acmicpc.net

#include <iostream>
#include<vector>
#include<stack>
#include<deque>
#include<map>

using namespace std;

map<int,stack<int>>arr;

int main()
{
  
  int n,p; 
  cin>>n>>p;
  int cnt=0; 
 
  int a,b; 
  for(int i=0;i<n;i++){
      cin>>a>>b; //줄 프랫번호  - 순서대로 연주해야
      //손 떼고 누르는거 다 
      if(arr[a].empty()){ 
          cnt++;
          arr[a].push(b);
         
          continue;
      }

      if(arr[a].top()==b){
         
          continue;
      }
      if(arr[a].top()>b){
     //프랫이 크면? 작아질때까지 떼고 누르기      
        while(arr[a].top()>b){
    
            arr[a].top();
           
            arr[a].pop();
            cnt++;
            if(arr[a].empty() || arr[a].top()<=b)break;
        }
        if(arr[a].empty()){
            cnt++;
            arr[a].push(b);
            continue; 
        }
        if(arr[a].top()<b){
            cnt++;
            arr[a].push(b);
        }
      }
      else{
             //프랫이 작으면? 그냥 누르기 
             arr[a].push(b);cnt++;
      }
  }
  
  
   cout<<cnt;
    return 0;
}
728x90
반응형