본문 바로가기

Programming/BOJ

[C/C++] BOJ #1049 - Guitar string

This problem can be solved simply by performing basic division and appropriate multiplication. Although it is classified as a Silver IV problem, it actually feels easier in terms of difficulty. However, the correct answer rate is quite low at 29.3%.

This may be because people are prone to mistakes if they have preconceived notions. If you abandon the assumptions that bundle products should be cheaper than individual items and that a store with cheap bundles will also have cheap individual prices, the problem becomes easier to solve.

The problem is about buying guitar strings. You’re given the number of strings you need, and your task is to buy them at the lowest possible price by appropriately purchasing 6-string bundle packs and individual strings from various stores.

purchasing guitar strings



To solve the problem, you should record the lowest bundle price and the lowest individual string price from the available stores. Then, check whether buying a bundle is cheaper than buying six individual strings. If it’s more expensive, update the bundle price to be six times the individual price.

You divide the required number of strings by 6 to determine how many bundles you need, and the remainder tells you how many individual strings you need to buy. However, even in that case, if buying the remaining individual strings costs more than one bundle, then it’s better to just buy an extra bundle — so you need to account for that exception as well.



Here are some test cases that are easy to make mistakes on.

input output
13 3
24 3
30 5
29 4
39
17 3
23 5
29 4
19 6
57
98 7
39 6
38 7
36 8
35 9
34 10
33 11
32 12
524


This is the code I wrote. Please just use it as a reference.

//----------------------------------------------------------
//    baekjoon #1049    - Guitar string
//        - by Aubrey Choi
//        - created at 2019-11-20
//----------------------------------------------------------
#include <stdio.h>

int main()
{
    int n, m, a, b, mp=1000, ms=1000;
    scanf("%d%d",&n,&m);
    while(m--) { scanf("%d%d",&a,&b); mp=(a<mp)?a:mp; ms=(b<ms)?b:ms; }
    if(ms*6<mp)mp=ms*6;
    ms=((n%6)*ms>mp)?mp:(n%6)*ms;
    printf("%d\n",(n/6)*mp+ms);
}
반응형

'Programming > BOJ' 카테고리의 다른 글

[C/C++] BOJ #1057 - Tournament  (1) 2025.05.09
[C/C++] BOJ #1051 - Number Square  (2) 2025.05.05
[C/C++] BOJ #1041 - Dice  (0) 2025.04.21
[C/C++] BOJ #1038 - Decreasing Number  (0) 2025.04.03
[C/C++] BOJ #1037 - Divisors  (0) 2025.03.13