※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 4649번 문제인 Tanning Salon이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/4649
4649번: Tanning Salon
The input consists of data for one or more salons, followed by a line containing the number 0 that signals the end of the input. Data for each salon is a single line containing a positive integer, representing the number of tanning beds in the salon, follo
www.acmicpc.net
각 손님이 방문하는 상태인지 나가는 상태인지를 알기 위한 visited 배열을 준비해 문제를 해결하자.
특히 빈 베드가 없을 때 들어오는 손님은 기존 태닝중인 손님보다 항상 먼저 나간다는 조건이 있으므로, 새 손님이 들어올 때 현재 가게에 있는 손님 수가 베드보다 많은지를 확인하는 것으로 문제의 답을 간단하게 구할 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>;
#include <string>
using namespace std;
int bed; string s;
int cnt, ans;
bool visited[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
while (cin >> bed >> s) {
ans = 0;
for (auto& l : s) {
if (visited[l]) visited[l] = 0, cnt--;
else {
visited[l] = 1, cnt++;
if (cnt > bed) ans++;
}
}
if (ans) cout << ans << " customer(s) walked away.\n";
else cout << "All customers tanned successfully.\n";
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 2852 // C++] NBA 농구 (0) | 2023.12.25 |
---|---|
[BOJ 3018 // C++] 캠프파이어 (0) | 2023.12.24 |
[BOJ 4335 // C++] 숫자 맞추기 (1) | 2023.12.22 |
[BOJ 11059 // C++] 크리 문자열 (1) | 2023.12.21 |
[BOJ 20052 // C++] 괄호 문자열 ? (0) | 2023.12.20 |