본문 바로가기

Algorithm_BOJ(백준)/그리디(Greedy Algorithm)

[백준 2810 c++] 컵홀더

728x90
반응형

문제 링크

www.acmicpc.net/problem/2810

 

2810번: 컵홀더

첫째 줄에 좌석의 수 N이 주어진다. (1 ≤ N ≤ 50) 둘째 줄에는 좌석의 정보가 주어진다.

www.acmicpc.net

문제 접근

// 접근: 컴홀더를 사용하는 사람의 최대 수

 

 

 

 

 

 

 

 

 

 

 

 

 





 

문제 풀이

// 풀이: S일때와 L일때의 컴홀더 새는 방법을 알아내고 사람은 컴홀더보다 많을 수 없으므로 컴홀더와 사람수중 작은값 출력

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

주의

 

 

 

 

개념

 

 

 

 

 

소스코드

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio> // c 문법 헤더파일
#include<string> // c++ 문자열 클래스
#include<vector> // 동적배열 라이브러리
#include<stack>
#include<queue>
#include<algorithm>  // sort와 unique 사용
#include<cmath> // 제곱이나 루트함수 사용
#include<cstring> // memset 함수
#include <set>
#include <map> // map구조체
#include <numeric> //accumulate(v.begin(), v.end(), 0);

// [백준 2810 c++] 컵홀더 
// 접근: 컴홀더를 사용하는 사람의 최대 수
// 풀이: S일때와 L일때의 컴홀더 새는 방법을 알아내고 사람은 컴홀더보다 많을 수 없으므로 컴//홀더와 사람수중 작은값 출력

using namespace std; // cin,cout 편하게 사용 라이브러리

int n;
string s;
int c = 1; // 처음엔 무조건 하나 있음
int main()
{
	// IO 속도 향상
	//ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n>> s;
	
	int L = 0;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == 'S') { c++; }
		else
		{
			L++;
			if (L == 2)
			{
				c++;
				L = 0;
			}
		}
	}

	cout << min(n, c);
	
	return 0;
}
반응형