[백준] 1049번 기타줄 / C++

#문제

백준 1049번 기타줄

#풀이

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

vector<int> package;
vector<int> unit;

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

	int n, m;
	cin >> n >> m;

	for (int i = 0; i < m; ++i)
	{
		int x, y;
		cin >> x >> y;

		package.push_back(x);
		unit.push_back(y);
	}

	sort(package.begin(), package.end());
	sort(unit.begin(), unit.end());

	int price = 0;

	int package_cost = (n / 6) * package[0] + min((n % 6) * unit[0], package[0]);
	int unit_cost = n * unit[0];

	price = min(package_cost, unit_cost);

	cout << price;

	return 0;
}

#정리

구입해야 하는 기타 줄 n과 기타 줄 브랜드 m개를 입력받는다. m개 브랜드의 6개 패키지 가격과 개별 가격을 입력받는다. 패키지 + (유닛) 가격과 개별 유닛 구입 가격을 비교하여 가장 저렴하게 구입 가능한 가격을 출력하면 해결할 수 있다. 패키지 가격과 유닛 가격을 각각 정렬하여 가장 작은 수를 찾고, 비교 대상 변수를 두 개 만들어서 패키지와 유닛을 섞은 가격과 모두 유닛으로 구입한 가격을 비교하여 저렴한 수를 출력하여 해결.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 1940번 주몽 / C++
  • [백준] 9613번 GCD 합 / C++
  • [백준] 11651번 좌표 정렬하기 2 / C++
  • [백준] 1934번 최소공배수 / C++
  • [백준] 1010번 다리 놓기 / C++