๐Ÿ’  BOJ

[BOJ][C++] ๋ฐฑ์ค€ 2839๋ฒˆ: ์„คํƒ• ๋ฐฐ๋‹ฌ

์„ ๋‹ฌ 2023. 11. 2. 13:45
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/2839

 

2839๋ฒˆ: ์„คํƒ• ๋ฐฐ๋‹ฌ

์ƒ๊ทผ์ด๋Š” ์š”์ฆ˜ ์„คํƒ•๊ณต์žฅ์—์„œ ์„คํƒ•์„ ๋ฐฐ๋‹ฌํ•˜๊ณ  ์žˆ๋‹ค. ์ƒ๊ทผ์ด๋Š” ์ง€๊ธˆ ์‚ฌํƒ•๊ฐ€๊ฒŒ์— ์„คํƒ•์„ ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ์„ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์„คํƒ•๊ณต์žฅ์—์„œ ๋งŒ๋“œ๋Š” ์„คํƒ•์€ ๋ด‰์ง€์— ๋‹ด๊ฒจ์ ธ ์žˆ๋‹ค. ๋ด‰์ง€๋Š” 3ํ‚ฌ๋กœ๊ทธ

www.acmicpc.net

 

๋ฌธ์ œ

์ƒ๊ทผ์ด๋Š” ์š”์ฆ˜ ์„คํƒ•๊ณต์žฅ์—์„œ ์„คํƒ•์„ ๋ฐฐ๋‹ฌํ•˜๊ณ  ์žˆ๋‹ค. ์ƒ๊ทผ์ด๋Š” ์ง€๊ธˆ ์‚ฌํƒ•๊ฐ€๊ฒŒ์— ์„คํƒ•์„ ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ์„ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. ์„คํƒ•๊ณต์žฅ์—์„œ ๋งŒ๋“œ๋Š” ์„คํƒ•์€ ๋ด‰์ง€์— ๋‹ด๊ฒจ์ ธ ์žˆ๋‹ค. ๋ด‰์ง€๋Š” 3ํ‚ฌ๋กœ๊ทธ๋žจ ๋ด‰์ง€์™€ 5ํ‚ฌ๋กœ๊ทธ๋žจ ๋ด‰์ง€๊ฐ€ ์žˆ๋‹ค.

์ƒ๊ทผ์ด๋Š” ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์—, ์ตœ๋Œ€ํ•œ ์ ์€ ๋ด‰์ง€๋ฅผ ๋“ค๊ณ  ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 18ํ‚ฌ๋กœ๊ทธ๋žจ ์„คํƒ•์„ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•  ๋•Œ, 3ํ‚ฌ๋กœ๊ทธ๋žจ ๋ด‰์ง€ 6๊ฐœ๋ฅผ ๊ฐ€์ ธ๊ฐ€๋„ ๋˜์ง€๋งŒ, 5ํ‚ฌ๋กœ๊ทธ๋žจ 3๊ฐœ์™€ 3ํ‚ฌ๋กœ๊ทธ๋žจ 1๊ฐœ๋ฅผ ๋ฐฐ๋‹ฌํ•˜๋ฉด, ๋” ์ ์€ ๊ฐœ์ˆ˜์˜ ๋ด‰์ง€๋ฅผ ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ๊ทผ์ด๊ฐ€ ์„คํƒ•์„ ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ ๋ฐฐ๋‹ฌํ•ด์•ผ ํ•  ๋•Œ, ๋ด‰์ง€ ๋ช‡ ๊ฐœ๋ฅผ ๊ฐ€์ ธ๊ฐ€๋ฉด ๋˜๋Š”์ง€ ๊ทธ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. (3 ≤ N ≤ 5000)

์ถœ๋ ฅ

์ƒ๊ทผ์ด๊ฐ€ ๋ฐฐ๋‹ฌํ•˜๋Š” ๋ด‰์ง€์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ์ •ํ™•ํ•˜๊ฒŒ Nํ‚ฌ๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

๊ทธ๋ฆฌ๋”” ๊ฐ™๋‹ค

 

์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ 5๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ

๋‚˜๋จธ์ง€ 0 -> 5ํ‚ค๋กœ ๋ชซ ๋ด‰์ง€

๋‚˜๋จธ์ง€ 1 -> 5ํ‚ค๋กœ ๋ชซ-1 ๋ด‰์ง€ + 3ํ‚ค๋กœ 2๋ด‰์ง€

๋‚˜๋จธ์ง€ 2 -> 5ํ‚ค๋กœ ๋ชซ-2๋ด‰์ง€ + 3ํ‚ค๋กœ 4๋ด‰์ง€

๋‚˜๋จธ์ง€ 3 -> 5ํ‚ค๋กœ ๋ชซ-3๋ด‰์ง€ + 3ํ‚ค๋กœ 6๋ด‰์ง€

๋‚˜๋จธ์ง€ 4 -> 5ํ‚ค๋กœ ๋ชซ-1๋ด‰์ง€ + 3ํ‚ค๋กœ 3๋ด‰์ง€

๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค 

 

์ด ๋•Œ ๋‹ต์œผ๋กœ ๋‚˜์˜จ ๋ด‰์ง€์ˆ˜์— ๊ฐ ํ‚ค๋กœ๋ฅผ ๊ณฑํ–ˆ๋Š”๋ฐ

์ฃผ์–ด์ง„ ์ˆซ์ž๋ณด๋‹ค ํฌ๊ฒŒ ๋‚˜์˜ค๋ฉด (์ฃผ์–ด์ง„ ์ˆซ์ž๊ฐ€ ๋” ์ž‘์œผ๋ฉด)

๋ฐ”๋กœ -1 ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค

 

// ํ’€์ด : https://whkakrkr.tistory.com

#include <iostream>

using namespace std;

int main() {
    int n, five, three;
    cin >> n;
    
    int share = n/5;
    int remain = n%5;
    
    if(remain==0) {
        five = share;
        three = 0;
    } else if(remain==1) {
        five = share-1;
        three = 2;
    } else if(remain==2) {
        five = share-2;
        three = 4;
    } else if(remain==3) {
        five = share;
        three = 1;
    } else {
        five = share-1;
        three = 3;
    }
    
    int ans;
    if(five < 0 || five*5+three*3 > n) {
        ans = -1;
    } else {
        ans = five+three;
    }
    cout << ans;
    
    return 0;  
}
๋ฐ˜์‘ํ˜•