PS/BOJ

[PS][자료구조] BOJ 17479 : 정식당

오리버거 2021. 8. 13. 23:00

[2021년 08월 13일 22시 47분 작성]

[PS][자료구조] BOJ 17479 : 정식당

[문제 링크 : 클릭]

 


1. 풀이

 

문제에 주어진 조건을 그대로 코드로 옮기면 됩니다.

또한, 메뉴--가격을 표현하기위해 unordered_map과 같은 해시 컨테이너를 사용해야합니다.

 

 

+) 처음에는 주어진 주문을 순차적으로 조건을 따져서 정답을 구해줬습니다.  --> 결과는 오답 파티... 

    그냥 주문 전체가 조건에 맞는지 여부를 따지면 해결할 수 있습니다.

 


2. 소스코드

[Github 링크 : 클릭]

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

typedef unordered_map<string, int> umap;
//key가 string, value가 int형인 map

string ans="Okay";
int a, b, c, n;
umap normal, special, forfree; 
vector<string> order;

void Input(int k, umap &tmp, bool is_free)
{
	for(int i=0; i<k; i++)
	{
		string name; int price = 0;
		if(is_free) cin>>name;
		else cin>>name>>price;
		tmp[name] = price;
	}
}

char GetMenuType(string str)
{
	if(normal.count(str)) return 'A';
	else if(special.count(str)) return 'B';
	return 'C';
}

int GetMenuPrice(string str)
{
	if(normal.count(str)) return normal[str];
	else if(special.count(str)) return special[str];
	return 0;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	cin>>a>>b>>c;

	Input(a, normal, false); //각각 입력
	Input(b, special, false);
	Input(c, forfree, true);	

	cin>>n;

	order = vector<string>(n); 
	for(int i=0; i<n; i++) //주문 입력
		cin>>order[i];
	
	long sum[2]; //0 : 일반메뉴 가격 합, 1: 스페셜 메뉴 합
	int free_cnt = 0; //서비스 메뉴 개수

	for(int i=0; i<n; i++)
	{
		char type = GetMenuType(order[i]);
		int price = GetMenuPrice(order[i]);

		if(type=='A') sum[0] += (long)price;
		else if(type=='B') sum[1] += (long)price;
		free_cnt+=(type == 'C');
	}
	
    //특별메뉴가 주문된 상태이고, 특별메뉴를 주문하기 위한 조건이 맞지 않다면
	if(sum[0] < 20000 && sum[1] > 0) cout<<"No\n";
    //서비스 메뉴가 주문이 되었고, 서비스 메뉴를 주문하기 위한 조건이 맞지 않다면
	else if(sum[0]+sum[1] < 50000 && free_cnt > 0) cout<<"No\n";
    //서비스 메뉴가 2개 이상 주문이 되었다면
	else if(free_cnt >= 2) cout<<"No\n";
	else cout<<"Okay\n";

	return 0;	
}

3. 복기

- 문제를 다시 잘 읽자..

- 제출에 신중하자..