<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>그리디 알고리즘 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<atom:link href="https://lycos7560.com/tag/%EA%B7%B8%EB%A6%AC%EB%94%94-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Thu, 17 Oct 2024 10:19:12 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://lycos7560.com/wp-content/uploads/2022/11/cropped-cropped-cropped-log-1-150x150-1-80x80.png</url>
	<title>그리디 알고리즘 Archives - 어제와 내일의 나 그 사이의 이야기</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>백준 16953번 (A → B, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 17 Oct 2024 08:40:22 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[16953번]]></category>
		<category><![CDATA[A → B]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Recursive]]></category>
		<category><![CDATA[그래프]]></category>
		<category><![CDATA[그래프 이론]]></category>
		<category><![CDATA[그래프 탐색]]></category>
		<category><![CDATA[그리디 알고리즘]]></category>
		<category><![CDATA[너비]]></category>
		<category><![CDATA[너비 우선 탐색]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 16953번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[재귀]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=38376</guid>

					<description><![CDATA[<p>A → B https://www.acmicpc.net/problem/16953 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 512 MB 61243 25275 19996 39.758% 문제 정수 A를 B로 바꾸려고 한다. 가능한 연산은 다음과 같은 두 가지이다. A를 B로 바꾸는데 필요한 연산의 최솟값을 구해보자. 입력 첫째 줄에 A, B (1 ≤ A &#60; B ≤ 109)가 주어진다. 출력 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/">백준 16953번 (A → B, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h1 class="wp-block-heading">A → B <img decoding="async" width="30" height="38" class="wp-image-37910" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Sliver_2-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h1>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/problem/16953" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/16953</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">512 MB</td><td class="has-text-align-left" data-align="left">61243</td><td class="has-text-align-left" data-align="left">25275</td><td class="has-text-align-left" data-align="left">19996</td><td class="has-text-align-left" data-align="left">39.758%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="wp-block-paragraph">정수 A를 B로 바꾸려고 한다. 가능한 연산은 다음과 같은 두 가지이다.</p>



<ul class="wp-block-list">
<li>2를 곱한다.</li>



<li>1을 수의 가장 오른쪽에 추가한다.&nbsp;</li>
</ul>



<p class="wp-block-paragraph">A를 B로 바꾸는데 필요한 연산의 최솟값을 구해보자.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="wp-block-paragraph">첫째 줄에 A, B (1 ≤ A &lt; B ≤ 10<sup>9</sup>)가 주어진다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="wp-block-paragraph">A를 B로 바꾸는데 필요한 연산의 최솟값에 1을 더한 값을 출력한다. </p>



<p class="wp-block-paragraph">만들 수 없는 경우에는 -1을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입/출력 1</h3>



<div class="wp-block-uagb-container uagb-block-ecb9f492 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-5a2a4110">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2 162</pre>
</div>



<div class="wp-block-uagb-container uagb-block-99e5d95d">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5</pre>
</div>
</div></div>



<p class="wp-block-paragraph">2&nbsp;→ 4&nbsp;→ 8&nbsp;→ 81&nbsp;→ 162</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입/출력 2</h3>



<div class="wp-block-uagb-container uagb-block-1a4486a4 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-edc25381">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4 42</pre>
</div>



<div class="wp-block-uagb-container uagb-block-7c5a70a3">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1</pre>
</div>
</div></div>



<p class="wp-block-paragraph">2&nbsp;→ 4&nbsp;→ 8&nbsp;→ 81&nbsp;→ 162</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading">예제 입/출력 3</h3>



<div class="wp-block-uagb-container uagb-block-789cb5d6 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-dd969038">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100 40021</pre>
</div>



<div class="wp-block-uagb-container uagb-block-32bc121e">
<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5</pre>
</div>
</div></div>



<p class="wp-block-paragraph">100&nbsp;→ 200&nbsp;→ 2001&nbsp;→ 4002&nbsp;→ 40021</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/7">그래프</a><a href="https://www.acmicpc.net/problem/tag/7" target="_blank" rel="noreferrer noopener"> </a><a href="https://www.acmicpc.net/problem/tag/7">이론</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/33" target="_blank" rel="noreferrer noopener">그리디 알고리즘</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/11" target="_blank" rel="noreferrer noopener">그래프 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/126" target="_blank" rel="noreferrer noopener">너비 우선 탐색</a></li>
</ul>



<div style="height:92px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<p class="wp-block-paragraph">DFS(재귀)를 이용하여 해결</p>



<p class="wp-block-paragraph">재귀를 수행하면서 조건을 만족할 수 없다면 Return</p>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
using namespace std;

int MinRes = INT16_MAX;
long long int Target = 0;

void Recursion(int _depth, long long int _val) {
    // 목표 값보다 크면 반환
    if (_val > Target)
        return;

    // 현재 깊이가 최소값보다 크면 반환
    if (_depth > MinRes)
        return;

    // 목표 값에 도달하면 최소 깊이 갱신
    if (Target == _val) {
        MinRes = min(MinRes, _depth);
        return;
    }

    // 재귀 호출을 통해 2를 곱하거나 1을 추가
    Recursion(_depth + 1, _val * 2);
    Recursion(_depth + 1, (_val * 10) + 1);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long int Start = 0;
    cin >> Start >> Target;
    
    // 초기 깊이는 1로 설정
    Recursion(1, Start);
    
    // 결과 출력: 만들 수 없는 경우 -1 출력
    if (INT16_MAX == MinRes)
        cout &lt;&lt; -1;
    else
        cout &lt;&lt; MinRes;
}
</pre>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1039" height="113" src="https://lycos7560.com/wp-content/uploads/2024/10/image-11.png" alt="" class="wp-image-38377" srcset="https://lycos7560.com/wp-content/uploads/2024/10/image-11.png 1039w, https://lycos7560.com/wp-content/uploads/2024/10/image-11-300x33.png 300w, https://lycos7560.com/wp-content/uploads/2024/10/image-11-768x84.png 768w" sizes="(max-width: 1039px) 100vw, 1039px" /></figure>



<p class="wp-block-paragraph">int =&gt; long long int</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/">백준 16953번 (A → B, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-16953%eb%b2%88-a-%e2%86%92-b-c-baekjoon/38376/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1026번 (보물, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1026%eb%b2%88-%eb%b3%b4%eb%ac%bc-c-baekjoon/35644/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1026%eb%b2%88-%eb%b3%b4%eb%ac%bc-c-baekjoon/35644/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 03 Jul 2023 10:35:42 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1026]]></category>
		<category><![CDATA[1026번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그리디]]></category>
		<category><![CDATA[그리디 알고리즘]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1026]]></category>
		<category><![CDATA[백준 1026번]]></category>
		<category><![CDATA[보물]]></category>
		<category><![CDATA[수학]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[정렬]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=35644</guid>

					<description><![CDATA[<p>백준 1026번 '보물' 문제에 대한 글입니다. (This article is about the issue of "Treasure" in Baekjoon Number 1026)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1026%eb%b2%88-%eb%b3%b4%eb%ac%bc-c-baekjoon/35644/">백준 1026번 (보물, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>


				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-bf784e4e      "
					data-scroll= "1"
					data-offset= "30"
					style=""
				>
				<div class="uagb-toc__wrap">
						<div class="uagb-toc__title">
							목차 테이블						</div>
																						<div class="uagb-toc__list-wrap ">
						<ol class="uagb-toc__list"><li class="uagb-toc__list"><a href="#보물" class="uagb-toc-link__trigger">보물</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#통과된-코드" class="uagb-toc-link__trigger">통과된 코드</a></ul></ol>					</div>
									</div>
				</div>
			


<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">보물</h1>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/problem/1026" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1026</a></p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">52186</td><td class="has-text-align-left" data-align="left">34340</td><td class="has-text-align-left" data-align="left">29166</td><td class="has-text-align-left" data-align="left">68.049%</td></tr></tbody></table></figure>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">문제</h3>



<p class="wp-block-paragraph">옛날 옛적에 수학이 항상 큰 골칫거리였던 나라가 있었다. </p>



<p class="wp-block-paragraph">이 나라의 국왕 김지민은 다음과 같은 문제를 내고 큰 상금을 걸었다.</p>



<p class="wp-block-paragraph">길이가 N인 정수 배열 A와 B가 있다. </p>



<p class="wp-block-paragraph">다음과 같이 함수 S를 정의하자.</p>



<p class="wp-block-paragraph">S = A[0] × B[0] + &#8230; + A[N-1] × B[N-1]</p>



<p class="wp-block-paragraph">S의 값을 가장 작게 만들기 위해 A의 수를 재배열하자. </p>



<p class="wp-block-paragraph">단, B에 있는 수는 재배열하면 안 된다.</p>



<p class="wp-block-paragraph">S의 최솟값을 출력하는 프로그램을 작성하시오.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">입력</h3>



<p class="wp-block-paragraph">첫째 줄에 N이 주어진다. </p>



<p class="wp-block-paragraph">둘째 줄에는 A에 있는 N개의 수가 순서대로 주어지고, </p>



<p class="wp-block-paragraph">셋째 줄에는 B에 있는 수가 순서대로 주어진다. </p>



<p class="wp-block-paragraph">N은 50보다 작거나 같은 자연수이고, </p>



<p class="wp-block-paragraph">A와 B의 각 원소는 100보다 작거나 같은 음이 아닌 정수이다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출력</h3>



<p class="wp-block-paragraph">첫째 줄에 S의 최솟값을 출력한다.</p>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">5
1 1 1 6 0
2 7 8 3 1</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 1</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">18</pre>



<div style="height:80px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 2</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3
1 1 3
10 30 20</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 2</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">80</pre>



<div style="height:80px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 입력 3</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">9
5 15 100 31 39 0 0 3 26
11 12 13 2 3 4 5 9 1</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">예제 출력 3</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">528</pre>



<div style="height:80px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">힌트</h3>



<p class="wp-block-paragraph">예제 1의 경우 A를 {1, 1, 0, 1, 6}과 같이 재배열하면 된다.</p>



<div style="height:80px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">출처</h3>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:80px" aria-hidden="true" class="wp-block-spacer"></div>



<h3 class="wp-block-heading has-large-font-size">알고리즘 분류</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/33" target="_blank" rel="noreferrer noopener">그리디 알고리즘</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/97" target="_blank" rel="noreferrer noopener">정렬</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://lycos7560.com/wp-content/uploads/2023/07/image-5.png" alt="" class="wp-image-35652" width="1076" height="752" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-5.png 1435w, https://lycos7560.com/wp-content/uploads/2023/07/image-5-300x210.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-5-768x537.png 768w" sizes="(max-width: 1076px) 100vw, 1076px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading has-large-font-size">통과된 코드</h2>



<p class="wp-block-paragraph">문제에서 &#8220;단, B에 있는 수는 재배열하면 안 <em>된다.&#8221;라는</em> 문장이 왜 있는지 의문</p>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;algorithm>
using namespace std;
int _N, _Res = 0, _A[50], _B[50];
// 오름차순 정렬
bool Greater(int a, int b)
{
	return a > b;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> _N;
	for (int i = 0; i &lt; _N; i++)
		cin >> _A[i];
	for (int i = 0; i &lt; _N; i++)
		cin >> _B[i];
	sort(_A, _A + _N, Greater);
	sort(_B, _B + _N);
	for (int i = 0; i &lt; _N; i++)
		_Res += (_B[i] * _A[i]);
	cout &lt;&lt; _Res;
	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1205" height="96" src="https://lycos7560.com/wp-content/uploads/2023/07/image-6.png" alt="" class="wp-image-35653" srcset="https://lycos7560.com/wp-content/uploads/2023/07/image-6.png 1205w, https://lycos7560.com/wp-content/uploads/2023/07/image-6-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2023/07/image-6-768x61.png 768w" sizes="(max-width: 1205px) 100vw, 1205px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1026%eb%b2%88-%eb%b3%b4%eb%ac%bc-c-baekjoon/35644/">백준 1026번 (보물, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1026%eb%b2%88-%eb%b3%b4%eb%ac%bc-c-baekjoon/35644/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1541번 (잃어버린 괄호, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1541%eb%b2%88-%ec%9e%83%ec%96%b4%eb%b2%84%eb%a6%b0-%ea%b4%84%ed%98%b8-c-baekjoon/5649/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1541%eb%b2%88-%ec%9e%83%ec%96%b4%eb%b2%84%eb%a6%b0-%ea%b4%84%ed%98%b8-c-baekjoon/5649/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 27 Jan 2023 09:57:23 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[ASCII]]></category>
		<category><![CDATA[atoi]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cin]]></category>
		<category><![CDATA[cin.tie(NULL)]]></category>
		<category><![CDATA[cout]]></category>
		<category><![CDATA[cout.tie(NULL)]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Greedy]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false)]]></category>
		<category><![CDATA[ios_base::sync_with_stdio(false);]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그리디]]></category>
		<category><![CDATA[그리디 알고리즘]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1541]]></category>
		<category><![CDATA[백준 1541번]]></category>
		<category><![CDATA[수학]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[파싱]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5649</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 1541번 '잃어버린 괄호' 문제에 대한 글입니다. (This is an article on the question of 'Lost Parentheses' in BAEKJOON No. 1541.) </p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1541%eb%b2%88-%ec%9e%83%ec%96%b4%eb%b2%84%eb%a6%b0-%ea%b4%84%ed%98%b8-c-baekjoon/5649/">백준 1541번 (잃어버린 괄호, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">잃어버린 괄호</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/1541" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1541</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">2 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">60829</td><td class="has-text-align-left" data-align="left">31765</td><td class="has-text-align-left" data-align="left">25201</td><td class="has-text-align-left" data-align="left">51.706%</td></tr></tbody></table></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">세준이는 양수와 +, -, 그리고 괄호를 가지고 식을 만들었다. </p>



<p class="has-medium-font-size wp-block-paragraph">그리고 나서 세준이는 괄호를 모두 지웠다.</p>



<p class="has-medium-font-size wp-block-paragraph">그리고 나서 세준이는 괄호를 적절히 쳐서 이 식의 값을 최소로 만들려고 한다.</p>



<p class="has-medium-font-size wp-block-paragraph">괄호를 적절히 쳐서 이 식의 값을 최소로 만드는 프로그램을 작성하시오.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. </p>



<p class="has-medium-font-size wp-block-paragraph">그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다 많이 연속되는 숫자는 없다. </p>



<p class="has-medium-font-size wp-block-paragraph">수는 0으로 시작할 수 있다. 입력으로 주어지는 식의 길이는 50보다 작거나 같다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 정답을 출력한다.</p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">55-50+40</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-35</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">10+20+30+40</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">100</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">00009-00009</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">0</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>문제를 번역한 사람: <a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>잘못된 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/windflower" target="_blank" rel="noreferrer noopener">windflower</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/124">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/158">문자열</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/33" target="_blank" rel="noreferrer noopener">그리디 알고리즘</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/96">파싱</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1126" src="https://lycos7560.com/wp-content/uploads/2023/01/KakaoTalk_20230127_184748740.jpg" alt="" class="wp-image-5651" srcset="https://lycos7560.com/wp-content/uploads/2023/01/KakaoTalk_20230127_184748740.jpg 1600w, https://lycos7560.com/wp-content/uploads/2023/01/KakaoTalk_20230127_184748740-300x211.jpg 300w, https://lycos7560.com/wp-content/uploads/2023/01/KakaoTalk_20230127_184748740-768x540.jpg 768w, https://lycos7560.com/wp-content/uploads/2023/01/KakaoTalk_20230127_184748740-1536x1081.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:20px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>
#include &lt;cmath>

using namespace std;

string str, tempStr;

int result = 0;
bool check = false;

int main()
{
	ios_base::sync_with_stdio(false); // scanf와 동기화를 비활성화
	// cin.tie(null); 코드는 cin과 cout의 묶음을 풀어줍니다.
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> str;

	// 한번이라도 - 가 나온다면 뒤의 수들은 전부 뺴준다.
	for (int i = 0; i &lt; str.length(); i++) {
		if (str[i] == 45) { // '-' = 45
			result += atoi(tempStr.c_str()) * ( pow(-1, check));
			check = true;
			tempStr = "";
		}
		else if (str[i] == 43) { // '+' = 43
			result += atoi(tempStr.c_str()) * (pow(-1, check));
			tempStr = "";
		}
		else tempStr.push_back(str[i]);
	}

	// 마지막 숫자 해결
	result += atoi(tempStr.c_str()) * (pow(-1, check));

	cout &lt;&lt; result;

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1036" height="103" src="https://lycos7560.com/wp-content/uploads/2023/01/image-25.png" alt="" class="wp-image-5652" srcset="https://lycos7560.com/wp-content/uploads/2023/01/image-25.png 1036w, https://lycos7560.com/wp-content/uploads/2023/01/image-25-300x30.png 300w, https://lycos7560.com/wp-content/uploads/2023/01/image-25-768x76.png 768w" sizes="(max-width: 1036px) 100vw, 1036px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1541%eb%b2%88-%ec%9e%83%ec%96%b4%eb%b2%84%eb%a6%b0-%ea%b4%84%ed%98%b8-c-baekjoon/5649/">백준 1541번 (잃어버린 괄호, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-1541%eb%b2%88-%ec%9e%83%ec%96%b4%eb%b2%84%eb%a6%b0-%ea%b4%84%ed%98%b8-c-baekjoon/5649/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11047번 (동전 0, C++, Greedy) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11047%eb%b2%88-%eb%8f%99%ec%a0%84-0-c-greedy-baekjoon/5114/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11047%eb%b2%88-%eb%8f%99%ec%a0%84-0-c-greedy-baekjoon/5114/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 20 Jan 2023 16:48:18 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11047]]></category>
		<category><![CDATA[11047번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Greedy]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[그리디]]></category>
		<category><![CDATA[그리디 알고리즘]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[동전]]></category>
		<category><![CDATA[동전 0]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11047]]></category>
		<category><![CDATA[백준 11047번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=5114</guid>

					<description><![CDATA[<p>백준(BAEKJOON) 11047번 '동전 0' 문제에 대한 글입니다.<br />
'그리디 알고리즘'을 사용하여 해결하였습니다.(This is an article about the 'Coin 0' problem in BAEKJOON No. 11047.<br />
Resolved using 'Gridy Algorithm'.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11047%eb%b2%88-%eb%8f%99%ec%a0%84-0-c-greedy-baekjoon/5114/">백준 11047번 (동전 0, C++, Greedy) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">동전 0</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/11047" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11047</a></p>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">105633</td><td class="has-text-align-left" data-align="left">55437</td><td class="has-text-align-left" data-align="left">42862</td><td class="has-text-align-left" data-align="left">51.811%</td></tr></tbody></table></figure>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">준규가 가지고 있는 동전은 총 N종류이고, 각각의 동전을 매우 많이 가지고 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">동전을 적절히 사용해서 그 가치의 합을 K로 만들려고 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">이때 필요한 동전 개수의 최솟값을 구하는 프로그램을 작성하시오.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000)</p>



<p class="has-medium-font-size wp-block-paragraph">둘째 줄부터 N개의 줄에 동전의 가치 A<sub>i</sub>가 오름차순으로 주어진다. </p>



<p class="has-medium-font-size wp-block-paragraph">(1 ≤ A<sub>i</sub>&nbsp;≤ 1,000,000, A<sub>1</sub>&nbsp;= 1, i&nbsp;≥ 2인 경우에&nbsp;A<sub>i</sub>는 A<sub>i-1</sub>의 배수)</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 K원을 만드는데 필요한 동전 개수의 최솟값을 출력한다.</p>



<div style="height:50px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">10 4200
1
5
10
50
100
500
1000
5000
10000
50000</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">10 4790
1
5
10
50
100
500
1000
5000
10000
50000</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">12</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<ul class="wp-block-list">
<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/ai4youej" target="_blank" rel="noreferrer noopener">ai4youej</a></li>



<li>문제를 만든 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>
</ul>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/33" target="_blank" rel="noreferrer noopener">그리디 알고리즘</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="1054" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230121_014449401.jpg" alt="" class="wp-image-5118" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230121_014449401.jpg 1600w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230121_014449401-300x198.jpg 300w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230121_014449401-768x506.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230121_014449401-1536x1012.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

int value[10]; // 동전의 가치를 저장할 배열

int N, K, temp,  cnt = 0;

int main()
{
	cin >> N >> K;

	// 배열에 동전의 가치를 넣어 둔다.
	for (int i = 0; i &lt; N; i++) cin >> value[i];

	// 가장 큰 동전부터 비교
	for (int i = N - 1; i >= 0; i--) {

		// K가 0이라면 목적 달성 break
		if (K == 0) break;
		
		temp = K / value[i];

		if (temp > 0) {
			cnt += temp;
			K -= temp * value[i];
		}
		else continue;
		// 동전의 가치가 남은 목표보다 크다면 continue;
	}

	// 결과를 출력한다.
	cout &lt;&lt; cnt;

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1035" height="100" src="https://lycos7560.com/wp-content/uploads/image-1009.png" alt="" class="wp-image-5116" srcset="https://lycos7560.com/wp-content/uploads/image-1009.png 1035w, https://lycos7560.com/wp-content/uploads/image-1009-300x29.png 300w, https://lycos7560.com/wp-content/uploads/image-1009-768x74.png 768w" sizes="(max-width: 1035px) 100vw, 1035px" /></figure>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<p class="has-medium-font-size wp-block-paragraph">요즘 DP 문제를 많이 접하다보니 동전만 보면 DP가 생각난다. </p>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11047%eb%b2%88-%eb%8f%99%ec%a0%84-0-c-greedy-baekjoon/5114/">백준 11047번 (동전 0, C++, Greedy) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11047%eb%b2%88-%eb%8f%99%ec%a0%84-0-c-greedy-baekjoon/5114/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2839번 (설탕 배달, C++, DP) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/2839-sugar-delivery-c-dp-baekjoon/4549/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/2839-sugar-delivery-c-dp-baekjoon/4549/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sat, 14 Jan 2023 13:36:26 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2839]]></category>
		<category><![CDATA[2839번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DynamicProgramming]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[그리디 알고리즘]]></category>
		<category><![CDATA[기본]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[다니아믹 프로그래밍]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2839]]></category>
		<category><![CDATA[백준 2839번]]></category>
		<category><![CDATA[설탕 배달]]></category>
		<category><![CDATA[수학]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=4549</guid>

					<description><![CDATA[<p>백준 2839번 '설탕 배달' 문제에 대한 글입니다. 'DP' 알고리즘을 사용하여 해결하였습니다. (This is an article on the issue of 'sugar delivery' in Baekjun 2839. Resolved using 'DP' algorithm.)</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/2839-sugar-delivery-c-dp-baekjoon/4549/">백준 2839번 (설탕 배달, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<!-- HorizontalAD -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5698326622209671" data-ad-slot="6908948342" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:62px" aria-hidden="true" class="wp-block-spacer"></div>



<h1 class="wp-block-heading">설탕 배달</h1>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://www.acmicpc.net/problem/2839">https://www.ac</a><a href="https://www.acmicpc.net/problem/2839" target="_blank" rel="noreferrer noopener">m</a><a href="https://www.acmicpc.net/problem/2839">icpc.net/problem/2839</a></p>



<div style="height:27px" aria-hidden="true" class="wp-block-spacer"></div>



<figure id="problem-info" class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th class="has-text-align-left" data-align="left">시간 제한</th><th class="has-text-align-left" data-align="left">메모리 제한</th><th class="has-text-align-left" data-align="left">제출</th><th class="has-text-align-left" data-align="left">정답</th><th class="has-text-align-left" data-align="left">맞힌 사람</th><th class="has-text-align-left" data-align="left">정답 비율</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">1 초</td><td class="has-text-align-left" data-align="left">128 MB</td><td class="has-text-align-left" data-align="left">262770</td><td class="has-text-align-left" data-align="left">94262</td><td class="has-text-align-left" data-align="left">71956</td><td class="has-text-align-left" data-align="left">35.880%</td></tr></tbody></table></figure>



<div style="height:38px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">문제</h2>



<p class="has-medium-font-size wp-block-paragraph">상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. </p>



<p class="has-medium-font-size wp-block-paragraph">봉지는 3킬로그램 봉지와 5킬로그램 봉지가 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">상근이는 귀찮기 때문에, 최대한 적은 봉지를 들고 가려고 한다. </p>



<p class="has-medium-font-size wp-block-paragraph">예를 들어, 18킬로그램 설탕을 배달해야 할 때, 3킬로그램 봉지 6개를 가져가도 되지만, </p>



<p class="has-medium-font-size wp-block-paragraph">5킬로그램 3개와 3킬로그램 1개를 배달하면, 더 적은 개수의 봉지를 배달할 수 있다.</p>



<p class="has-medium-font-size wp-block-paragraph">상근이가 설탕을 정확하게 N킬로그램 배달해야 할 때, </p>



<p class="has-medium-font-size wp-block-paragraph">봉지 몇 개를 가져가면 되는지 그 수를 구하는 프로그램을 작성하시오.</p>



<div style="height:38px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">입력</h2>



<p class="has-medium-font-size wp-block-paragraph">첫째 줄에 N이 주어진다. (3 ≤ N ≤ 5000)</p>



<div style="height:34px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출력</h2>



<p class="has-medium-font-size wp-block-paragraph">상근이가 배달하는 봉지의 최소 개수를 출력한다. </p>



<p class="has-medium-font-size wp-block-paragraph">만약, 정확하게 N킬로그램을 만들 수 없다면 -1을 출력한다.</p>



<div style="height:36px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">18</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 1</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">4</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 2</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-1</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">6</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 3</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">2</pre>



<div style="height:48px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 4</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">9</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 4</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 입력 5</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">11</pre>



<div style="height:31px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">예제 출력 5</h2>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">3</pre>



<div style="height:60px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">출처</h2>



<p class="wp-block-paragraph"><a href="https://www.acmicpc.net/category/45" target="_blank" rel="noreferrer noopener">Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/17" target="_blank" rel="noreferrer noopener">Croatian Open Competition in Informatics</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/20" target="_blank" rel="noreferrer noopener">COCI 2010/2011</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/81" target="_blank" rel="noreferrer noopener">Contest #7</a>&nbsp;1번</p>



<ul class="wp-block-list">
<li>문제를 번역한 사람:&nbsp;<a href="https://www.acmicpc.net/user/baekjoon" target="_blank" rel="noreferrer noopener">baekjoon</a></li>



<li>데이터를 추가한 사람:&nbsp;<a href="https://www.acmicpc.net/user/hyunynim" target="_blank" rel="noreferrer noopener">hyunynim</a>,&nbsp;<a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a></li>
</ul>



<div style="height:44px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">알고리즘 분류</h2>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/124" target="_blank" rel="noreferrer noopener">수학</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/25" target="_blank" rel="noreferrer noopener">다이나믹 프로그래밍</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/33" target="_blank" rel="noreferrer noopener">그리디 알고리즘</a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity is-style-wide" style="margin-top:var(--wp--preset--spacing--80);margin-bottom:var(--wp--preset--spacing--80)"/>



<figure class="wp-block-image size-full"><img decoding="async" width="1600" height="680" src="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230114_223311146.jpg" alt="" class="wp-image-4552" srcset="https://lycos7560.com/wp-content/uploads/KakaoTalk_20230114_223311146.jpg 1600w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230114_223311146-300x128.jpg 300w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230114_223311146-768x326.jpg 768w, https://lycos7560.com/wp-content/uploads/KakaoTalk_20230114_223311146-1536x653.jpg 1536w" sizes="(max-width: 1600px) 100vw, 1600px" /></figure>



<div style="height:41px" aria-hidden="true" class="wp-block-spacer"></div>



<h2 class="wp-block-heading">통과된 코드</h2>



<div style="height:32px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="cpp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include &lt;iostream>

using namespace std;

int N;
int dp[5001];

int main()
{
	cin >> N;

	dp[0] = 0;
	dp[1] = -1;
	dp[2] = -1;
	dp[3] = 1;
	dp[4] = -1;
	dp[5] = 1;

	for (int i = 6; i &lt;= N; i++) {
		if (dp[i - 3] == -1 &amp;&amp; dp[i - 5] == -1) dp[i] = -1;
		else if (dp[i - 5] == -1) dp[i] = dp[i - 3] + 1;
		else if (dp[i - 3] == -1) dp[i] = dp[i - 5] + 1;
		else dp[i] = min(dp[i - 3] + 1, dp[i - 5] + 1);
	}

	cout &lt;&lt; dp[N];

	return 0;
}</pre>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>



<figure class="wp-block-image size-full"><img decoding="async" width="1041" height="97" src="https://lycos7560.com/wp-content/uploads/image-938.png" alt="" class="wp-image-4550" srcset="https://lycos7560.com/wp-content/uploads/image-938.png 1041w, https://lycos7560.com/wp-content/uploads/image-938-300x28.png 300w, https://lycos7560.com/wp-content/uploads/image-938-768x72.png 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></figure>



<div style="height:49px" aria-hidden="true" class="wp-block-spacer"></div>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5698326622209671" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5698326622209671" data-ad-slot="4245812909"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<div style="height:48px" aria-hidden="true" class="wp-block-spacer"></div>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/2839-sugar-delivery-c-dp-baekjoon/4549/">백준 2839번 (설탕 배달, C++, DP) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://lycos7560.com/cpp/baekjoon_online_judge/2839-sugar-delivery-c-dp-baekjoon/4549/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
