<?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/%ec%bd%94%ed%85%8c/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>생각의 흐름을 타고 다니며 만드는 블로그</description>
	<lastBuildDate>Tue, 14 Oct 2025 19:12:46 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</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>백준 14003번 (가장 긴 증가하는 부분 수열 5, C++)</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/#comments</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 13 Oct 2025 06:06:17 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[14003]]></category>
		<category><![CDATA[14003번]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Algorithm Analysis]]></category>
		<category><![CDATA[Algorithm Explanation]]></category>
		<category><![CDATA[Algorithm Practice]]></category>
		<category><![CDATA[Algorithm Study]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Binary Search LIS]]></category>
		<category><![CDATA[binary_search]]></category>
		<category><![CDATA[BOJ 14003]]></category>
		<category><![CDATA[BOJ Solution]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ Algorithm]]></category>
		<category><![CDATA[C++ STL]]></category>
		<category><![CDATA[Coding Interview]]></category>
		<category><![CDATA[Coding Lecture]]></category>
		<category><![CDATA[Coding Test]]></category>
		<category><![CDATA[Competitive Coding]]></category>
		<category><![CDATA[Competitive Programming]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[DP]]></category>
		<category><![CDATA[DP Table]]></category>
		<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[Example Walkthrough]]></category>
		<category><![CDATA[Fast IO]]></category>
		<category><![CDATA[Greedy]]></category>
		<category><![CDATA[Greedy Algorithm]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Increasing Sequence]]></category>
		<category><![CDATA[Index Mapping]]></category>
		<category><![CDATA[Index Tracking]]></category>
		<category><![CDATA[Input Output]]></category>
		<category><![CDATA[LIS]]></category>
		<category><![CDATA[LIS Diagram]]></category>
		<category><![CDATA[LIS Example]]></category>
		<category><![CDATA[LIS Flow]]></category>
		<category><![CDATA[LIS Indexing]]></category>
		<category><![CDATA[LIS Pattern]]></category>
		<category><![CDATA[LIS Reconstruction]]></category>
		<category><![CDATA[LIS Reconstruction Algorithm]]></category>
		<category><![CDATA[LIS step by step]]></category>
		<category><![CDATA[LIS Study]]></category>
		<category><![CDATA[LIS Table]]></category>
		<category><![CDATA[LIS Tail]]></category>
		<category><![CDATA[LIS Track]]></category>
		<category><![CDATA[LIS Tutorial]]></category>
		<category><![CDATA[LIS using Binary Search]]></category>
		<category><![CDATA[LIS Visualization]]></category>
		<category><![CDATA[LIS 강의]]></category>
		<category><![CDATA[LIS 구현]]></category>
		<category><![CDATA[LIS 단계별]]></category>
		<category><![CDATA[LIS 문제]]></category>
		<category><![CDATA[LIS 문제풀이]]></category>
		<category><![CDATA[LIS 복원]]></category>
		<category><![CDATA[LIS 복원 알고리즘]]></category>
		<category><![CDATA[LIS 알고리즘]]></category>
		<category><![CDATA[LIS 예제]]></category>
		<category><![CDATA[LIS 추적]]></category>
		<category><![CDATA[LIS 코드]]></category>
		<category><![CDATA[LIS 패턴]]></category>
		<category><![CDATA[Logic Explanation]]></category>
		<category><![CDATA[Longest Increasing Subsequence]]></category>
		<category><![CDATA[Longest Subsequence]]></category>
		<category><![CDATA[lower_bound]]></category>
		<category><![CDATA[Memory Limit]]></category>
		<category><![CDATA[Memory Optimization]]></category>
		<category><![CDATA[O(N log N)]]></category>
		<category><![CDATA[O(N^2)]]></category>
		<category><![CDATA[O(NlogN)]]></category>
		<category><![CDATA[Optimal Algorithm]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Prev Index]]></category>
		<category><![CDATA[Problem Explanation]]></category>
		<category><![CDATA[problem solving]]></category>
		<category><![CDATA[Programming Algorithm]]></category>
		<category><![CDATA[Programming Study]]></category>
		<category><![CDATA[Sequence Analysis]]></category>
		<category><![CDATA[Sequence Optimization]]></category>
		<category><![CDATA[Sequence Problem]]></category>
		<category><![CDATA[Sequence Reconstruction]]></category>
		<category><![CDATA[Sequence Trace]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Subsequence]]></category>
		<category><![CDATA[Tail Array]]></category>
		<category><![CDATA[Theory]]></category>
		<category><![CDATA[Time Complexity]]></category>
		<category><![CDATA[Time Limit]]></category>
		<category><![CDATA[Traceback]]></category>
		<category><![CDATA[upper_bound]]></category>
		<category><![CDATA[vector]]></category>
		<category><![CDATA[가장 긴 증가하는 부분 수열]]></category>
		<category><![CDATA[가장 긴 증가하는 부분 수열 5]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[동적 계획법]]></category>
		<category><![CDATA[디버깅]]></category>
		<category><![CDATA[로직 해설]]></category>
		<category><![CDATA[메모리 최적화]]></category>
		<category><![CDATA[문제 해설]]></category>
		<category><![CDATA[배열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 14003]]></category>
		<category><![CDATA[백준 14003번]]></category>
		<category><![CDATA[백준 문제풀이]]></category>
		<category><![CDATA[벡터]]></category>
		<category><![CDATA[수열 문제]]></category>
		<category><![CDATA[수열 분석]]></category>
		<category><![CDATA[수열 추적]]></category>
		<category><![CDATA[시간 복잡도]]></category>
		<category><![CDATA[시간 제한]]></category>
		<category><![CDATA[시간 최적화]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[알고리즘 분석]]></category>
		<category><![CDATA[알고리즘 설명]]></category>
		<category><![CDATA[알고리즘 연습]]></category>
		<category><![CDATA[예제 해설]]></category>
		<category><![CDATA[이론]]></category>
		<category><![CDATA[이분 탐색]]></category>
		<category><![CDATA[인덱스 기반 LIS]]></category>
		<category><![CDATA[입출력]]></category>
		<category><![CDATA[자료구조]]></category>
		<category><![CDATA[증가 수열]]></category>
		<category><![CDATA[최장 부분 수열]]></category>
		<category><![CDATA[최적화]]></category>
		<category><![CDATA[최적화 알고리즘]]></category>
		<category><![CDATA[추적]]></category>
		<category><![CDATA[코딩 강의]]></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=40314</guid>

					<description><![CDATA[<p>가장 긴 증가하는 부분 수열 5 https://www.acmicpc.net/problem/14003 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 3 초 512 MB 45574 15974 11322 34.533% 문제 수열 A가 주어졌을 때, 가장 긴 증가하는&#160;부분&#160;수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분&#160;수열은&#160;A = {10,&#160;20, [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/">백준 14003번 (가장 긴 증가하는 부분 수열 5, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-87ca5ab3      "
					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="#가장-긴-증가하는-부분-수열-5" class="uagb-toc-link__trigger">가장 긴 증가하는 부분 수열 5</a><ul class="uagb-toc__list"><li class="uagb-toc__list"><a href="#문제" class="uagb-toc-link__trigger">문제</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#입력" class="uagb-toc-link__trigger">입력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출력" class="uagb-toc-link__trigger">출력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-1" class="uagb-toc-link__trigger">예제 입력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-1" class="uagb-toc-link__trigger">예제 출력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출처" class="uagb-toc-link__trigger">출처</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#비슷한-문제" class="uagb-toc-link__trigger">비슷한 문제</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#알고리즘-분류" class="uagb-toc-link__trigger">알고리즘 분류</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#1차-시도-로직-그대로-구현" class="uagb-toc-link__trigger">1차 시도 &#8211; 로직 그대로 구현</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#2차-시도-이분-탐색-인덱스-추적을-이용한-lis-복원-on-log-n" class="uagb-toc-link__trigger">2차 시도 &#8211; 이분 탐색 + 인덱스 추적을 이용한 LIS 복원 / O(N log N)</a></ul></ol>					</div>
									</div>
				</div>
			


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



<h2 class="wp-block-heading">가장 긴 증가하는 부분 수열 5 <img decoding="async" width="35" height="45" class="wp-image-37909" style="width: 35px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_5.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_5.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Platinum_5-234x300.jpg 234w" sizes="(max-width: 35px) 100vw, 35px" /></h2>



<p><a href="https://www.acmicpc.net/problem/14003" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/14003</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>시간 제한</th><th>메모리 제한</th><th>제출</th><th>정답</th><th>맞힌 사람</th><th>정답 비율</th></tr></thead><tbody><tr><td>3 초</td><td>512 MB</td><td>45574</td><td>15974</td><td>11322</td><td>34.533%</td></tr></tbody></table></figure>



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



<p>수열 A가 주어졌을 때, 가장 긴 증가하는&nbsp;부분&nbsp;수열을 구하는 프로그램을 작성하시오.</p>



<p>예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분&nbsp;수열은&nbsp;A = {<strong>10</strong>,&nbsp;<strong>20</strong>, 10,&nbsp;<strong>30</strong>, 20,&nbsp;<strong>50</strong>} 이고, 길이는 4이다.</p>



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



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



<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다.</p>



<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (-1,000,000,000&nbsp;≤ A<sub>i</sub>&nbsp;≤ 1,000,000,000)</p>



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



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



<p>첫째 줄에 수열 A의 가장 긴 증가하는 부분 수열의 길이를 출력한다.</p>



<p>둘째 줄에는 정답이 될 수 있는 가장 긴 증가하는 부분 수열을 출력한다.</p>



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



<div class="wp-block-uagb-container uagb-block-aa78ee06 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-70c24c24">
<h3 class="wp-block-heading">예제 입력 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="">6
10 20 10 30 20 50</pre>
</div>



<div class="wp-block-uagb-container uagb-block-2c88bb50">
<h3 class="wp-block-heading">예제 출력 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="">4
10 20 30 50</pre>
</div>
</div></div>



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



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



<ul class="wp-block-list">
<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/baekjoon">baekjoon</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/harinboy">harinboy</a>, <a href="https://www.acmicpc.net/user/jh05013">jh05013</a>, <a href="https://www.acmicpc.net/user/kkw564">kkw564</a>, <a href="https://www.acmicpc.net/user/ldw0318">ldw0318</a>, <a href="https://www.acmicpc.net/user/minho6428">minho6428</a>, <a href="https://www.acmicpc.net/user/sohnryang">sohnryang</a>, <a href="https://www.acmicpc.net/user/surung9898">surung9898</a>, <a href="https://www.acmicpc.net/user/yeo2507">yeo2507</a></li>



<li>빠진 조건을 찾은 사람: <a href="https://www.acmicpc.net/user/Acka">Acka</a></li>
</ul>



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



<h3 class="wp-block-heading">비슷한 문제</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/11053" target="_blank" rel="noreferrer noopener">11053번. 가장 긴 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11054" target="_blank" rel="noreferrer noopener">11054번. 가장 긴 바이토닉 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11055" target="_blank" rel="noreferrer noopener">11055번. 가장 큰 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11722" target="_blank" rel="noreferrer noopener">11722번. 가장 긴 감소하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/12015" target="_blank" rel="noreferrer noopener">12015번. 가장 긴 증가하는 부분 수열 2</a></li>



<li><a href="https://www.acmicpc.net/problem/12738" target="_blank" rel="noreferrer noopener">12738번. 가장 긴 증가하는 부분 수열 3</a></li>



<li><a href="https://www.acmicpc.net/problem/14002" target="_blank" rel="noreferrer noopener">14002번. 가장 긴 증가하는 부분 수열 4</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/12" target="_blank" rel="noreferrer noopener">이분 탐색</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/235" target="_blank" rel="noreferrer noopener">역추적</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/43" 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--60);margin-bottom:var(--wp--preset--spacing--60)"/>



<h3 class="wp-block-heading">1차 시도 &#8211; 로직 그대로 구현</h3>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1043" height="115" src="https://lycos7560.com/wp-content/uploads/2025/10/image-4.png" alt="" class="wp-image-40317" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-4.png 1043w, https://lycos7560.com/wp-content/uploads/2025/10/image-4-300x33.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-4-768x85.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></figure>



<p>한 번의 입력(<code>num</code>)에 의해 생성된 새로운 시퀀스들을 임시로 저장</p>



<p>어떤 수열에도 붙을 수 없으면 독립된 시퀀스 시작</p>



<p>나중에 불필요한 시퀀스(같은 길이인데 꼬리가 큰 것)는 제거</p>



<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>
#include &lt;vector>

using namespace std;

int n; // 수열의 개수
vector&lt;vector&lt;int>> Sequences;

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

    cin >> n;
    if (n &lt;= 0) return 0;

    int num;
    cin >> num;

    // 첫 번째 수로 초기 시퀀스 하나 생성
    vector&lt;int> sequence;
    sequence.push_back(num);
    Sequences.push_back(sequence);

    for (int i = 1; i &lt; n; i++)
    {
        cin >> num;
        int length = Sequences.size();

        bool canExtend = false;
        vector&lt;vector&lt;int>> newSequences;

        // 기존 시퀀스들을 모두 검사
        for (int j = 0; j &lt; length; j++)
        {
            int last = Sequences[j].back();

            // 확장 가능한 경우 : 새로운 시퀀스 생성
            if (last &lt; num)
            {
                canExtend = true;
                vector&lt;int> extendedSeq = Sequences[j];
                extendedSeq.push_back(num);
                newSequences.push_back(extendedSeq);
            }
        }

        // 어떤 시퀀스에도 붙지 못하면 새로운 시퀀스 시작
        if (!canExtend)
        {
            vector&lt;int> newSeq;
            newSeq.push_back(num);
            newSequences.push_back(newSeq);
        }

        // 새로 생긴 시퀀스들을 기존에 합침
        for (auto&amp; seq : newSequences)
            Sequences.push_back(seq);

        // 최적 시퀀스 길이 및 꼬리값 계산
        int maxSeqSize = 0;
        int bestIndex = 0;
        int bestTail = 1000000001;

        for (int j = 0; j &lt; (int)Sequences.size(); j++)
        {
            int sz = Sequences[j].size();
            int tail = Sequences[j].back();
            if (sz > maxSeqSize || (sz == maxSeqSize &amp;&amp; tail &lt; bestTail))
            {
                maxSeqSize = sz;
                bestTail = tail;
                bestIndex = j;
            }
        }

        // 가지치기: 같은 길이인데 꼬리값이 큰 시퀀스 제거
        for (int j = 0; j &lt; (int)Sequences.size();)
        {
            int sz = Sequences[j].size();
            int tail = Sequences[j].back();

            if (sz == maxSeqSize &amp;&amp; tail > bestTail)
            {
                Sequences.erase(Sequences.begin() + j);
            }
            else
            {
                j++;
            }
        }
    }

	// 결과 중 가장 긴 시퀀스 찾기
    int maxSize = 0;
    int bestIdx = 0;
    for (int i = 0; i &lt; (int)Sequences.size(); i++)
    {
        if (Sequences[i].size() > maxSize)
        {
            maxSize = Sequences[i].size();
            bestIdx = i;
        }
    }

    cout &lt;&lt; maxSize &lt;&lt; "\n";
    for (int x : Sequences[bestIdx])
        cout &lt;&lt; x &lt;&lt; " ";

    return 0;
}
</pre>



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



<p>브루트포스 방식으로 모든 가능한 증가 수열을 시도하는 방법은 시간 초과 및 메모리 초과로 인하여 통과할 수 없음.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="604" height="509" src="https://lycos7560.com/wp-content/uploads/2025/10/image-3.png" alt="" class="wp-image-40318" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-3.png 604w, https://lycos7560.com/wp-content/uploads/2025/10/image-3-300x253.png 300w" sizes="(max-width: 604px) 100vw, 604px" /></figure>



<p><strong>매 반복마다</strong> <code>Sequences</code>의 크기가 <strong>기하급수적으로 늘어나는 구조</strong></p>



<p>n개의 원소가 들어오면 2ⁿ에 비례하는 시퀀스로 통과하기 힘들다. (O(2ⁿ))</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>단계</th><th>가능한 Sequences 수</th></tr></thead><tbody><tr><td>1</td><td>[ [1] ]</td></tr><tr><td>2</td><td>[ [1], [2], [1,2] ]</td></tr><tr><td>3</td><td>[ [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3] ]</td></tr><tr><td>4</td><td>거의 모든 가능한 증가 부분 수열이 등장함 (약 15개)</td></tr></tbody></table></figure>



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



<h3 class="wp-block-heading">2차 시도 &#8211; <strong>이분 탐색 + 인덱스 추적</strong>을 이용한 LIS 복원 / O(N log N)</h3>



<figure class="wp-block-image size-full"><img decoding="async" width="1042" height="39" src="https://lycos7560.com/wp-content/uploads/2025/10/image-7.png" alt="" class="wp-image-40322" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-7.png 1042w, https://lycos7560.com/wp-content/uploads/2025/10/image-7-300x11.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-7-768x29.png 768w" sizes="(max-width: 1042px) 100vw, 1042px" /></figure>



<p>최장 증가 수열 (LIS, Longest Increasing Subsequence)</p>



<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>
#include &lt;vector>
#include &lt;algorithm>

using namespace std;

// (LIS, Longest Increasing Subsequence)

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

    int N;
    std::cin >> N;

    vector&lt;int> input(N);
    for (int i = 0; i &lt; N; i++)
        std::cin >> input[i];

    vector&lt;int> tail;  // LIS를 구성하는 인덱스들
    vector&lt;int> prev_idx(N, -1);  //각 요소가 LIS에 포함될 때, 그 직전 요소의 인덱스를 저장


    for (int i = 0; i &lt; N; i++)
    {
        // 이분 탐색으로 input[i]가 tail 배열에서 들어가야 할 위치 찾기
        int lo = 0;
        int hi = (int)tail.size() - 1;
        int pos = (int)tail.size(); // 기본값은 맨 뒤

        while (lo &lt;= hi)
        {
            int mid = (lo + hi) / 2;
            // input[i]보다 크거나 같은 첫 번째 tail 원소 위치(pos)를 찾
            if (input[tail[mid]] &lt; input[i])
            {
                lo = mid + 1;
            }
            else
            {
                pos = mid;
                hi = mid - 1;
            }
        }

        // 이전 인덱스 설정
        if (pos > 0)
        {
            prev_idx[i] = tail[pos - 1];
        }
        else
        {
            prev_idx[i] = -1;  // 첫 번째 요소
        }

        // tail 업데이트
        if (pos == (int)tail.size())
        {
            tail.push_back(i);
        }
        else
        {
            tail[pos] = i;
        }
    }

    // 결과: LIS 길이
    int lis_len = (int)tail.size();
    std::cout &lt;&lt; lis_len &lt;&lt; "\n";

    // 실제 LIS 수열 재구성
    vector&lt;int> lis;
    for (int idx = tail.back(); idx != -1; idx = prev_idx[idx])
    {
        lis.push_back(input[idx]);
    }
    reverse(lis.begin(), lis.end());

    for (int v : lis)
        std::cout &lt;&lt; v &lt;&lt; " ";
    std::cout &lt;&lt; "\n";

    return 0;
}</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="651" height="324" src="https://lycos7560.com/wp-content/uploads/2025/10/image-5.png" alt="" class="wp-image-40319" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-5.png 651w, https://lycos7560.com/wp-content/uploads/2025/10/image-5-300x149.png 300w" sizes="(max-width: 651px) 100vw, 651px" /></figure>



<p><code>input[i]</code>가 들어가야 할 위치(<code>pos</code>)를 찾음</p>



<p>즉, <code>tail</code> 배열 내에서 <strong>input[i]보다 작지만 가장 큰 값 다음 위치</strong>를 찾는다.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="513" height="466" src="https://lycos7560.com/wp-content/uploads/2025/10/image-6.png" alt="" class="wp-image-40320" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-6.png 513w, https://lycos7560.com/wp-content/uploads/2025/10/image-6-300x273.png 300w" sizes="(max-width: 513px) 100vw, 513px" /></figure>



<p><code>prev_idx[i]</code>는 <code>input[i]</code> 바로 앞에 올 LIS 원소의 인덱스입니다.</p>



<p><code>pos > 0</code>이면 → 현재 원소가 &#8220;길이 pos+1&#8243;짜리 LIS의 마지막이 되므로 &#8220;그 바로 이전 원소&#8221;는 <code>tail[pos - 1]</code></p>



<p><code>pos == 0</code>이면 → 수열의 첫 번째 원소이므로 이전이 없음</p>



<p><code>pos == tail.size()</code> → <code>input[i]</code>가 지금 까지의 모든 수보다 크므로 LIS 길이 1 증가</p>



<p>그렇지 않으면 → 기존 LIS 후보 중 &#8220;끝값이 더 큰 것&#8221;을 <code>input[i]</code>로 <strong>치환</strong> (더 작은 끝값 유지)</p>



<h4 class="wp-block-heading">예시 1</h4>



<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="">input = [5, 2, 8, 6, 3, 6, 9, 7]</pre>



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



<figure class="wp-block-table"><table><thead><tr><th class="has-text-align-left" data-align="left">단계</th><th class="has-text-align-left" data-align="left">i</th><th class="has-text-align-left" data-align="left">input[i]</th><th class="has-text-align-left" data-align="left">tail (인덱스)</th><th class="has-text-align-left" data-align="left">tail (값으로 표현)</th><th class="has-text-align-left" data-align="left">prev_idx</th><th class="has-text-align-left" data-align="left">설명</th></tr></thead><tbody><tr><td class="has-text-align-left" data-align="left">0</td><td class="has-text-align-left" data-align="left">0</td><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">[0]</td><td class="has-text-align-left" data-align="left">[5]</td><td class="has-text-align-left" data-align="left">[-1, ?, ?, ?, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">첫 번째 원소 → 새로운 LIS 시작</td></tr><tr><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">1</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">[1]</td><td class="has-text-align-left" data-align="left">[2]</td><td class="has-text-align-left" data-align="left">[-1, -1, ?, ?, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">2 &lt; 5 → 기존 tail[0] 교체 (더 작은 끝값 유지)</td></tr><tr><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">2</td><td class="has-text-align-left" data-align="left">8</td><td class="has-text-align-left" data-align="left">[1, 2]</td><td class="has-text-align-left" data-align="left">[2, 8]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, ?, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">8 &gt; 2 → 길이 2짜리 LIS 형성</td></tr><tr><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">[1, 3]</td><td class="has-text-align-left" data-align="left">[2, 6]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, ?, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">6은 2보다 크지만 8보다 작음 → 8을 6으로 교체</td></tr><tr><td class="has-text-align-left" data-align="left">4</td><td class="has-text-align-left" data-align="left">4</td><td class="has-text-align-left" data-align="left">3</td><td class="has-text-align-left" data-align="left">[1, 4]</td><td class="has-text-align-left" data-align="left">[2, 3]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, ?, ?, ?]</td><td class="has-text-align-left" data-align="left">3은 2보다 크고 6보다 작음 → 6을 3으로 교체</td></tr><tr><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">5</td><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">[1, 4, 5]</td><td class="has-text-align-left" data-align="left">[2, 3, 6]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, 4, ?, ?]</td><td class="has-text-align-left" data-align="left">6은 3보다 크므로 길이 3짜리 LIS 생성</td></tr><tr><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">6</td><td class="has-text-align-left" data-align="left">9</td><td class="has-text-align-left" data-align="left">[1, 4, 5, 6]</td><td class="has-text-align-left" data-align="left">[2, 3, 6, 9]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, 4, 5, ?]</td><td class="has-text-align-left" data-align="left">9 &gt; 6 → 길이 4짜리 LIS 생성</td></tr><tr><td class="has-text-align-left" data-align="left">7</td><td class="has-text-align-left" data-align="left">7</td><td class="has-text-align-left" data-align="left">7</td><td class="has-text-align-left" data-align="left">[1, 4, 5, 7]</td><td class="has-text-align-left" data-align="left">[2, 3, 6, 7]</td><td class="has-text-align-left" data-align="left">[-1, -1, 1, 1, 1, 4, 5, 5]</td><td class="has-text-align-left" data-align="left">7은 6보다 크지만 9보다 작음 → 9를 7로 교체</td></tr></tbody></table></figure>



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



<h4 class="wp-block-heading">예시 2</h4>



<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="">이분 탐색을 이용한 LIS 구성 로직

input = [3, 5, 2, 7, 4, 1, 8]

--------------------------------

i=0 (값: 3)
tail: [] (빈 배열)
이분탐색: tail.size() = 0 → while문 스킵
pos = 0 (기본값)

prev_idx[0] = -1 (pos=0이므로)
tail 업데이트: pos == tail.size() → push_back(0)
결과: tail = [0] (값: [3])

--------------------------------

i=1 (값: 5)
tail: [0] (값: [3])
이분탐색:
  lo=0, hi=0, mid=0
  input[tail[0]] = 3 &lt; 5 → lo = mid+1 = 1
  lo=1 > hi=0 → 종료
pos = tail.size() = 1

prev_idx[1] = tail[0] = 0 (5 앞에 3)
tail 업데이트: push_back(1)
결과: tail = [0,1] (값: [3,5])

--------------------------------

i=2 (값: 2)
tail: [0,1] (값: [3,5])
이분탐색:
  lo=0, hi=1, mid=0
  input[tail[0]] = 3 >= 2 → pos=0, hi=-1
  lo=0 > hi=-1 → 종료
pos = 0

prev_idx[2] = -1 (pos=0이므로)
tail 업데이트: tail[0] = 2
결과: tail = [2,1] (값: [2,5])

--------------------------------

i=3 (값: 7)
tail: [2,1] (값: [2,5])
이분탐색:
  lo=0, hi=1, mid=0: input[tail[0]]=2 &lt; 7 → lo=1
  lo=1, hi=1, mid=1: input[tail[1]]=5 &lt; 7 → lo=2
  lo=2 > hi=1 → 종료
pos = tail.size() = 2

prev_idx[3] = tail[1] = 1 (7 앞에 5)
tail 업데이트: push_back(3)
결과: tail = [2,1,3] (값: [2,5,7])

--------------------------------

i=4 (값: 4)
tail: [2,1,3] (값: [2,5,7])
이분탐색:
  lo=0, hi=2, mid=1: input[tail[1]]=5 >= 4 → pos=1, hi=0
  lo=0, hi=0, mid=0: input[tail[0]]=2 &lt; 4 → lo=1
  lo=1 > hi=0 → 종료
pos = 1

prev_idx[4] = tail[0] = 2 (4 앞에 2)
tail 업데이트: tail[1] = 4
결과: tail = [2,4,3] (값: [2,4,7])

--------------------------------

i=5 (값: 1)
tail: [2,4,3] (값: [2,4,7])
이분탐색:
  lo=0, hi=2, mid=1: input[tail[1]]=4 >= 1 → pos=1, hi=0
  lo=0, hi=0, mid=0: input[tail[0]]=2 >= 1 → pos=0, hi=-1
  lo=0 > hi=-1 → 종료
pos = 0

prev_idx[5] = -1
tail 업데이트: tail[0] = 5
결과: tail = [5,4,3] (값: [1,4,7])

--------------------------------

i=6 (값: 8)

tail: [5,4,3] (값: [1,4,7])
이분탐색:
  lo=0, hi=2, mid=1: input[tail[1]]=4 &lt; 8 → lo=2
  lo=2, hi=2, mid=2: input[tail[2]]=7 &lt; 8 → lo=3
  lo=3 > hi=2 → 종료
pos = tail.size() = 3

prev_idx[6] = tail[2] = 3 (8 앞에 7)
tail 업데이트: push_back(6)
결과: tail = [5,4,3,6] (값: [1,4,7,8])

</pre>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/">백준 14003번 (가장 긴 증가하는 부분 수열 5, C++)</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-14003%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ec%a6%9d%ea%b0%80%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-5-c/40314/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>백준 1655번 (가운데를 말해요, C++)</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1655%eb%b2%88-%ea%b0%80%ec%9a%b4%eb%8d%b0%eb%a5%bc-%eb%a7%90%ed%95%b4%ec%9a%94-c/40309/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1655%eb%b2%88-%ea%b0%80%ec%9a%b4%eb%8d%b0%eb%a5%bc-%eb%a7%90%ed%95%b4%ec%9a%94-c/40309/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 13 Oct 2025 02:09:45 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[1655]]></category>
		<category><![CDATA[1655번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ 구현]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[O(log n)]]></category>
		<category><![CDATA[push-pop 연산]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[Two Heaps]]></category>
		<category><![CDATA[가운데를 말해요]]></category>
		<category><![CDATA[개발 능력]]></category>
		<category><![CDATA[게임 구현]]></category>
		<category><![CDATA[경계 조건]]></category>
		<category><![CDATA[계산적 사고]]></category>
		<category><![CDATA[고급 기법]]></category>
		<category><![CDATA[공간 복잡도]]></category>
		<category><![CDATA[공간 효율성]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[교육적 예제]]></category>
		<category><![CDATA[구현 기술]]></category>
		<category><![CDATA[균형 유지]]></category>
		<category><![CDATA[기본 개념]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[논리적 사고]]></category>
		<category><![CDATA[다이어그램]]></category>
		<category><![CDATA[단계별 설명]]></category>
		<category><![CDATA[단계별 진행]]></category>
		<category><![CDATA[대량 데이터]]></category>
		<category><![CDATA[대안 접근법]]></category>
		<category><![CDATA[데이터 구조]]></category>
		<category><![CDATA[데이터 스트림]]></category>
		<category><![CDATA[동생 게임]]></category>
		<category><![CDATA[동작 원리]]></category>
		<category><![CDATA[동적 데이터]]></category>
		<category><![CDATA[동적 중간값]]></category>
		<category><![CDATA[디자인 패턴]]></category>
		<category><![CDATA[로그 시간]]></category>
		<category><![CDATA[메모리 사용]]></category>
		<category><![CDATA[문제 분석]]></category>
		<category><![CDATA[문제 해결]]></category>
		<category><![CDATA[문제 해결력]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 1655]]></category>
		<category><![CDATA[백준 1655번]]></category>
		<category><![CDATA[백준이]]></category>
		<category><![CDATA[범위 제한]]></category>
		<category><![CDATA[복잡도 분석]]></category>
		<category><![CDATA[상세 설명]]></category>
		<category><![CDATA[상태 변화]]></category>
		<category><![CDATA[성능 개선]]></category>
		<category><![CDATA[성능 튜닝]]></category>
		<category><![CDATA[성능 평가]]></category>
		<category><![CDATA[소프트웨어 공학]]></category>
		<category><![CDATA[속도 개선]]></category>
		<category><![CDATA[수열 처리]]></category>
		<category><![CDATA[숫자 스트림]]></category>
		<category><![CDATA[스트리밍 알고리즘]]></category>
		<category><![CDATA[시각화]]></category>
		<category><![CDATA[시간 복잡도]]></category>
		<category><![CDATA[시간 효율성]]></category>
		<category><![CDATA[실시간 응용]]></category>
		<category><![CDATA[실시간 중간값]]></category>
		<category><![CDATA[실시간 처리]]></category>
		<category><![CDATA[실용적 구현]]></category>
		<category><![CDATA[실제 구현]]></category>
		<category><![CDATA[심화 학습]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[알고리즘 문제]]></category>
		<category><![CDATA[알고리즘 비교]]></category>
		<category><![CDATA[알고리즘 최적화]]></category>
		<category><![CDATA[알고리즘 패턴]]></category>
		<category><![CDATA[알고리즘 학습]]></category>
		<category><![CDATA[연속된 데이터]]></category>
		<category><![CDATA[연습 문제]]></category>
		<category><![CDATA[예외 상황]]></category>
		<category><![CDATA[예제 시뮬레이션]]></category>
		<category><![CDATA[온라인 알고리즘]]></category>
		<category><![CDATA[우선순위 큐]]></category>
		<category><![CDATA[우선순위 큐 활용]]></category>
		<category><![CDATA[음수 처리]]></category>
		<category><![CDATA[응용 기술]]></category>
		<category><![CDATA[이해하기 쉬운]]></category>
		<category><![CDATA[입력 처리]]></category>
		<category><![CDATA[자료구조 공부]]></category>
		<category><![CDATA[자료구조 선택]]></category>
		<category><![CDATA[자료구조 설계]]></category>
		<category><![CDATA[작은 값 선택]]></category>
		<category><![CDATA[접근 방법]]></category>
		<category><![CDATA[조건 처리]]></category>
		<category><![CDATA[주요 개념]]></category>
		<category><![CDATA[중간 위치]]></category>
		<category><![CDATA[중간값 계산]]></category>
		<category><![CDATA[중간값 유지]]></category>
		<category><![CDATA[중간값 찾기]]></category>
		<category><![CDATA[중앙값]]></category>
		<category><![CDATA[짝수 경우]]></category>
		<category><![CDATA[최대 힙]]></category>
		<category><![CDATA[최소 힙]]></category>
		<category><![CDATA[최적 해법]]></category>
		<category><![CDATA[최적화 기법]]></category>
		<category><![CDATA[출력 생성]]></category>
		<category><![CDATA[컴퓨터 과학]]></category>
		<category><![CDATA[코드 분석]]></category>
		<category><![CDATA[코드 예시]]></category>
		<category><![CDATA[코딩 기법]]></category>
		<category><![CDATA[코딩 테스트]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[프로그래밍 스킬]]></category>
		<category><![CDATA[프로그래밍 연습]]></category>
		<category><![CDATA[프로그래밍 팁]]></category>
		<category><![CDATA[학습 포인트]]></category>
		<category><![CDATA[해결 전략]]></category>
		<category><![CDATA[핵심 아이디어]]></category>
		<category><![CDATA[효율성]]></category>
		<category><![CDATA[효율적 알고리즘]]></category>
		<category><![CDATA[효율적 처리]]></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=40309</guid>

					<description><![CDATA[<p>가운데를 말해요 https://www.acmicpc.net/problem/1655 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 0.1 초 128 MB 80245 24515 18393 31.195% 문제 백준이는 동생에게 &#8220;가운데를 말해요&#8221; 게임을 가르쳐주고 있다. 백준이가&#160;정수를 하나씩 외칠때마다 동생은 지금까지 백준이가 말한 수 중에서 중간값을 말해야 한다. 만약, 그동안 백준이가 외친 수의 개수가 짝수개라면 중간에 있는 두 수 중에서 작은 수를 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1655%eb%b2%88-%ea%b0%80%ec%9a%b4%eb%8d%b0%eb%a5%bc-%eb%a7%90%ed%95%b4%ec%9a%94-c/40309/">백준 1655번 (가운데를 말해요, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-87ca5ab3      "
					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><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#입력" class="uagb-toc-link__trigger">입력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출력" class="uagb-toc-link__trigger">출력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-1" class="uagb-toc-link__trigger">예제 입력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-1" class="uagb-toc-link__trigger">예제 출력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출처" class="uagb-toc-link__trigger">출처</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#알고리즘-분류" class="uagb-toc-link__trigger">알고리즘 분류</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#1차-시도-배열-인덱스-실패-시간-초과" class="uagb-toc-link__trigger">1차 시도 &#8211; 배열 인덱스 (실패 / 시간 초과)</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#2차-시도-two-heaps-방식-성공" class="uagb-toc-link__trigger">2차 시도 &#8211; Two Heaps 방식 (성공)</a></ul></ol>					</div>
									</div>
				</div>
			


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



<h2 class="wp-block-heading">가운데를 말해요 <img decoding="async" width="35" height="45" class="wp-image-37903" style="width: 35px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_2-234x300.jpg 234w" sizes="(max-width: 35px) 100vw, 35px" /></h2>



<p><a href="https://www.acmicpc.net/problem/1655" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/1655</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>시간 제한</th><th>메모리 제한</th><th>제출</th><th>정답</th><th>맞힌 사람</th><th>정답 비율</th></tr></thead><tbody><tr><td>0.1 초</td><td>128 MB</td><td>80245</td><td>24515</td><td>18393</td><td>31.195%</td></tr></tbody></table></figure>



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



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



<p>백준이는 동생에게 &#8220;가운데를 말해요&#8221; 게임을 가르쳐주고 있다. </p>



<p>백준이가&nbsp;정수를 하나씩 외칠때마다 동생은 지금까지 백준이가 말한 수 중에서 중간값을 말해야 한다. </p>



<p>만약, 그동안 백준이가 외친 수의 개수가 짝수개라면 중간에 있는 두 수 중에서 작은 수를 말해야 한다.</p>



<p>예를 들어 백준이가 동생에게 1, 5, 2, 10, -99, 7, 5를 순서대로 외쳤다고 하면, 동생은 1, 1, 2, 2, 2, 2, 5를 차례대로 말해야 한다. </p>



<p>백준이가 외치는 수가 주어졌을 때, 동생이 말해야 하는 수를 구하는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에는 백준이가 외치는 정수의 개수 N이 주어진다. </p>



<p>N은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수이다. </p>



<p>그 다음 N줄에 걸쳐서 백준이가 외치는 정수가 차례대로 주어진다. </p>



<p>정수는 -10,000보다 크거나 같고, 10,000보다 작거나 같다.</p>



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



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



<p>한 줄에 하나씩 N줄에 걸쳐 백준이의 동생이 말해야 하는 수를 순서대로 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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="">7
1
5
2
10
-99
7
5</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">1
1
2
2
2
2
5</pre>



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



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



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



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



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



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/59" 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--70);margin-bottom:var(--wp--preset--spacing--70)"/>



<h3 class="wp-block-heading">1차 시도 &#8211; 배열 인덱스 (실패 / 시간 초과)</h3>



<figure class="wp-block-image size-full"><img decoding="async" width="1041" height="34" src="https://lycos7560.com/wp-content/uploads/2025/10/image-1.png" alt="" class="wp-image-40311" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-1.png 1041w, https://lycos7560.com/wp-content/uploads/2025/10/image-1-300x10.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-1-768x25.png 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></figure>



<p>배열을 기반으로, 중간값의 인덱스(<code>currentMedian</code>)를 유지하면서 새로운 값이 들어올 때마다 <code>leftCount</code>, <code>rightCount</code>를 조정하는 방식</p>



<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 = 0;
int num[20001] = { 0 };  // 값의 출현 횟수 (index = value + 10000)
int currentMedian = 0;   // 현재 중간값의 인덱스
int leftCount = 0;       // 중간값보다 작은 값들의 개수
int rightCount = 0;      // 중간값보다 큰 값들의 개수

// 실제 값 => 배열 인덱스로 변환
inline int GetIndex(int value) 
{
    return value + 10000;
}

// 배열 인덱스 => 실제 값으로 변환
inline int GetValue(int index) 
{
    return index - 10000;
}

int FindMedianOptimized(int newValue, int total)
{
    int newIdx = GetIndex(newValue);
    num[newIdx]++;

    // 새 값의 위치에 따라 좌우 카운트 조정
    if (newIdx &lt; currentMedian) leftCount++;
    else if (newIdx > currentMedian) rightCount++;
    else rightCount++; // 같은 값일 경우 간단히 오른쪽으로 분류

    // 목표 중간 위치 계산
    int targetPos = (total + 1) / 2;

    // 중간값 인덱스를 왼쪽으로 이동 (왼쪽 원소가 너무 많을 때)
    while (leftCount >= targetPos) {
        currentMedian--;
        leftCount -= num[currentMedian];
        rightCount += num[currentMedian];
    }

    // 중간값 인덱스를 오른쪽으로 이동 (왼쪽 원소가 부족할 때)
    while (leftCount + num[currentMedian] &lt; targetPos) {
        leftCount += num[currentMedian];
        currentMedian++;
        rightCount -= num[currentMedian];
    }

    // 현재 중간값 반환
    return GetValue(currentMedian);
}

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

    cin >> n;
    int value = 0;

    // 첫 번째 입력값으로 초기화
    cin >> value;
    num[GetIndex(value)]++;
    currentMedian = GetIndex(value);
    leftCount = 0;
    rightCount = 0;

    // 첫 번째 값 => 중간값
    cout &lt;&lt; value &lt;&lt; "\n"; 

    for (int i = 2; i &lt;= n; ++i) 
    {
        cin >> value;
        cout &lt;&lt; FindMedianOptimized(value, i) &lt;&lt; "\n";
    }

    return 0;
}
</pre>



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



<h4 class="wp-block-heading">시간 초과의 원인</h4>



<figure class="wp-block-image size-full"><img decoding="async" width="610" height="311" src="https://lycos7560.com/wp-content/uploads/2025/10/image-2.png" alt="" class="wp-image-40312" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-2.png 610w, https://lycos7560.com/wp-content/uploads/2025/10/image-2-300x153.png 300w" sizes="(max-width: 610px) 100vw, 610px" /></figure>



<p>새로운 값이 입력될 때마다 <strong><code>currentMedian</code>을 1씩 이동</strong>하며 왼쪽/오른쪽 카운트를 갱신</p>



<p><code>currentMedian</code>의 이동 거리는 <strong>입력된 값의 분포에 따라서</strong> 문제가 생길 수 있음.</p>



<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="">// 최악의 경우
-10000, -9999, -9998, ... , 9999, 10000
</pre>



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



<p>매번 새로운 값이 들어올 때마다 <code>currentMedian</code>이 <strong>오른쪽 끝으로 한 칸씩 이동</strong></p>



<p>매번 <code>while</code> 루프가 <strong>최대 20,000번 반복</strong>되는 불상사가 생길 수 있음</p>



<p>입력 n = 100,000일 때 <strong>최악의 복잡도: <code>O(n * 20000)</code> = 2,000,000,000 (20억)</strong> </p>



<p><code>currentMedian</code> 부분을 개선 또는 다른 알고리즘을 사용하는 방법이 더 좋아보임</p>



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



<h3 class="wp-block-heading">2차 시도 &#8211; Two Heaps 방식 (성공)</h3>



<figure class="wp-block-image size-full"><img decoding="async" width="1044" height="42" src="https://lycos7560.com/wp-content/uploads/2025/10/image.png" alt="" class="wp-image-40310" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image.png 1044w, https://lycos7560.com/wp-content/uploads/2025/10/image-300x12.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-768x31.png 768w" sizes="(max-width: 1044px) 100vw, 1044px" /></figure>



<p><code>maxHeap</code>(왼쪽) + <code>minHeap</code>(오른쪽)으로 균형 유지</p>



<p><code>maxHeap</code>: 중간값 이하의 값들 저장 (왼쪽 절반)</p>



<p><code>minHeap</code>: 중간값 이상의 값들 저장 (오른쪽 절반)</p>



<p>균형 조정 시 힙에서 한 번씩 <code>push/pop</code> → <code>O(log n)</code></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>
#include &lt;queue>
using namespace std;

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

    int n;
    cin >> n;

    priority_queue&lt;int> maxHeap;                            // 왼쪽 절반 (최대 힙)
    priority_queue&lt;int, vector&lt;int>, greater&lt;int>> minHeap;  // 오른쪽 절반 (최소 힙)

    for (int i = 0; i &lt; n; ++i) 
    {
        int value;
        cin >> value;

        // 값 삽입: 왼쪽/오른쪽 균형 유지
        if (maxHeap.empty() || value &lt;= maxHeap.top())
            maxHeap.push(value);
        else
            minHeap.push(value);

        // 두 힙의 크기 균형 조정 (왼쪽 = 오른쪽 또는 +1)
        if (maxHeap.size() > minHeap.size() + 1) {
            minHeap.push(maxHeap.top());
            maxHeap.pop();
        }
        else if (minHeap.size() > maxHeap.size()) {
            maxHeap.push(minHeap.top());
            minHeap.pop();
        }

        // 현재 중간값 출력
        cout &lt;&lt; maxHeap.top() &lt;&lt; "\n";
    }

    return 0;
}
</pre>



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



<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, 5, 2, 10, -5, 8, 3]

단계별 힙 상태:

1. value=1
   maxHeap: [1]      minHeap: []
   중간값: 1

2. value=5
   maxHeap: [1]      minHeap: [5]
   중간값: 1

3. value=2
   maxHeap: [2, 1]   minHeap: [5]
   중간값: 2

4. value=10
   maxHeap: [2, 1]   minHeap: [5, 10]
   중간값: 2

5. value=-5
   maxHeap: [2, 1, -5] minHeap: [5, 10]
   중간값: 2

6. value=8
   maxHeap: [2, 1, -5] minHeap: [5, 8, 10]
   중간값: 2

7. value=3
   maxHeap: [3, 2, -5, 1] minHeap: [5, 8, 10]
   중간값: 3</pre>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-1655%eb%b2%88-%ea%b0%80%ec%9a%b4%eb%8d%b0%eb%a5%bc-%eb%a7%90%ed%95%b4%ec%9a%94-c/40309/">백준 1655번 (가운데를 말해요, C++)</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-1655%eb%b2%88-%ea%b0%80%ec%9a%b4%eb%8d%b0%eb%a5%bc-%eb%a7%90%ed%95%b4%ec%9a%94-c/40309/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11375번 (열혈강호, C++)</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 20 May 2025 07:14:20 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11375]]></category>
		<category><![CDATA[11375번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11375]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[열혈강호]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[이분 매칭]]></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=40040</guid>

					<description><![CDATA[<p>열혈강호 https://www.acmicpc.net/problem/11375 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 256 MB 16854 7509 4363 42.261% 문제 강호네 회사에는 직원이 N명이 있고, 해야할 일이 M개가 있다. 직원은 1번부터 N번까지 번호가 매겨져 있고, 일은 1번부터 M번까지 번호가 매겨져 있다. 각 직원은 자신이 할 수 있는 일들 중 한 개의 일만 담당할 수 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/">백준 11375번 (열혈강호, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">열혈강호 <img decoding="async" width="30" height="38" class="wp-image-37908" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/11375" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11375</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">256 MB</td><td class="has-text-align-left" data-align="left">16854</td><td class="has-text-align-left" data-align="left">7509</td><td class="has-text-align-left" data-align="left">4363</td><td class="has-text-align-left" data-align="left">42.261%</td></tr></tbody></table></figure>



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



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



<p>강호네 회사에는 직원이 N명이 있고, 해야할 일이 M개가 있다. </p>



<p>직원은 1번부터 N번까지 번호가 매겨져 있고, 일은 1번부터 M번까지 번호가 매겨져 있다.</p>



<p>각 직원은 자신이 할 수 있는 일들 중 한 개의 일만 담당할 수 있고, 각각의 일을 담당하는 사람은 1명이어야 한다.</p>



<p>각각의 직원이 할 수 있는 일의 목록이 주어졌을 때, M개의 일 중에서 최대 몇 개를 할 수 있는지 구하는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에 직원의 수 N과 일의 개수 M이 주어진다. (1 ≤ N, M ≤ 1,000)</p>



<p>둘째 줄부터 N개의 줄의 i번째 줄에는 i번 직원이 할 수 있는 일의 개수와 할 수 있는 일의 번호가 주어진다.</p>



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



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



<p>첫째 줄에 강호네 회사에서 할 수 있는 일의 개수를 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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 5
2 1 2
1 1
2 2 3
3 3 4 5
1 1</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">4</pre>



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



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



<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/doju" target="_blank" rel="noreferrer noopener">doju</a></li>



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



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/13" 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)"/>



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



<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;vector>
#include &lt;cstring>

using namespace std;

int N, M;
vector&lt;int> employee[1001];
int works[1001];       
bool visited[1001];      

bool DFS(int n) 
{

    for (int work : employee[n]) 
    {
        if (visited[work]) 
            continue;

        visited[work] = true;

        if (works[work] == 0 || DFS(works[work])) 
        {
            works[work] = n;
            return true;
        }
    }

    return false;
}

int main() 
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N >> M;

    for (int i = 1; i &lt;= N; i++) 
    {
        int k, work;
        cin >> k;

        for (int j = 0; j &lt; k; j++) 
        {
            cin >> work;
            employee[i].push_back(work);
        }
    }

    int result = 0;

    for (int i = 1; i &lt;= N; i++) 
    {
        memset(visited, false, sizeof(visited));

        if (DFS(i)) 
            result++;
    }

    cout &lt;&lt; result &lt;&lt; "\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="79" src="https://lycos7560.com/wp-content/uploads/2025/05/image-10.png" alt="" class="wp-image-40041" srcset="https://lycos7560.com/wp-content/uploads/2025/05/image-10.png 1041w, https://lycos7560.com/wp-content/uploads/2025/05/image-10-300x23.png 300w, https://lycos7560.com/wp-content/uploads/2025/05/image-10-768x58.png 768w" sizes="(max-width: 1041px) 100vw, 1041px" /></figure>



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



<p>해당 문제와 코드가 같은 문제</p>



<p><a href="https://www.acmicpc.net/problem/2188" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2188</a></p>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/">백준 11375번 (열혈강호, C++)</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-11375%eb%b2%88-%ec%97%b4%ed%98%88%ea%b0%95%ed%98%b8-c/40040/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 2188번 (축사 배정, C++) / 추가 반례 [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/</link>
					<comments>https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 18 May 2025 19:39:58 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[2188]]></category>
		<category><![CDATA[2188번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 2188]]></category>
		<category><![CDATA[백준 2188번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[예제]]></category>
		<category><![CDATA[이분 매칭]]></category>
		<category><![CDATA[추가]]></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=40032</guid>

					<description><![CDATA[<p>축사 배정 https://www.acmicpc.net/problem/2188 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 128 MB 13113 6284 4114 48.785% 문제 농부 존은 소 축사를 완성하였다. 축사 환경을 쾌적하게 유지하기 위해서, 존은 축사를 M개의 칸으로 구분하고, 한 칸에는 최대 한 마리의 소만 들어가게 계획했다. 첫 주에는 소를&#160;임의 배정해서&#160;축사를 운영했으나, 곧 문제가 발생하게 되었다. 바로 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/">백준 2188번 (축사 배정, C++) / 추가 반례 [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">축사 배정 <img decoding="async" width="30" height="38" class="wp-image-37908" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Platinum_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/2188" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/2188</a></p>



<div style="height:50px" 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">13113</td><td class="has-text-align-left" data-align="left">6284</td><td class="has-text-align-left" data-align="left">4114</td><td class="has-text-align-left" data-align="left">48.785%</td></tr></tbody></table></figure>



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



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



<p>농부 존은 소 축사를 완성하였다. 축사 환경을 쾌적하게 유지하기 위해서, 존은 축사를 M개의 칸으로 구분하고, 한 칸에는 최대 한 마리의 소만 들어가게 계획했다.</p>



<p>첫 주에는 소를&nbsp;임의 배정해서&nbsp;축사를 운영했으나, 곧 문제가 발생하게 되었다. 바로 소가 자신이&nbsp;희망하는 몇 개의 축사 외에는 들어가기를 거부하는 것이다.</p>



<p>농부 존을&nbsp;도와 최대한 많은 수의 소가 축사에 들어갈 수 있도록 하는 프로그램을 작성하시오. </p>



<p>축사의 번호는 1부터 M까지 매겨져 있다.</p>



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



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



<p>첫째 줄에 소의 수 N과 축사의 수 M이 주어진다. (1 ≤ N, M ≤ 200)</p>



<p>둘째 줄부터 N개의 줄에는 각 소가 들어가기 원하는 축사에 대한 정보가 주어진다. </p>



<p>i번째 소가 들어가기 원하는 축사의 수 S<sub>i</sub>&nbsp;(0 ≤ S<sub>i</sub>&nbsp;≤ M)이 먼저 주어지고, 이후 S<sub>i</sub>개의 축사 번호가 주어진다. </p>



<p>같은 축사 번호가 두 번 이상 주어지는 경우는 없다.</p>



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



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



<p>첫째 줄에 축사에 들어갈 수 있는 소의 최댓값을 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">4</pre>



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



<p><a href="https://www.acmicpc.net/board/view/137221" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/board/view/137221</a></p>



<h3 class="wp-block-heading">예제 입력 A</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="">4 3
1 1
1 2
1 3
1 3</pre>



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



<h3 class="wp-block-heading">예제 출력 A</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</pre>



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



<h3 class="wp-block-heading">예제 입력 B</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="">4 3
0
0
0
0</pre>



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



<h3 class="wp-block-heading">예제 출력 B</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="">0</pre>



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



<h3 class="wp-block-heading">예제 입력 C</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="">4 3
1 1
1 2
2 1 2
2 1 2</pre>



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



<h3 class="wp-block-heading">예제 출력 C</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="">2</pre>



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



<h3 class="wp-block-heading">예제 입력 D</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="">50 40
2 24 21
1 39
4 30 31 15 18
2 21 3
4 21 40 39 30
1 20
5 20 30 4 29 25
3 39 9 33
1 32
5 27 22 36 2 11
1 9
5 35 29 14 38 1
3 28 22 1
4 18 20 29 22
3 5 11 38
2 36 30
5 9 17 35 10 19
3 18 31 7
3 4 36 30
4 24 12 9 14
3 28 2 27
3 13 6 2
3 6 37 39
4 5 26 15 16
3 25 6 36
2 9 31
3 17 6 7
5 27 4 13 29 30
1 1
1 12
5 3 1 38 8 5
2 14 12
3 17 39 12
3 30 15 26
2 21 3
3 7 6 11
2 28 13
2 25 16
2 19 16
1 13
5 39 24 34 29 33
2 40 10
3 6 35 40
1 37
2 24 35
4 11 22 3 29
3 19 7 16
1 14
1 38
5 31 14 28 19 6</pre>



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



<h3 class="wp-block-heading">예제 출력 D</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="">39</pre>



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



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



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



<li>문제의 오타를 찾은 사람:&nbsp;<a href="https://www.acmicpc.net/user/hun3555" target="_blank" rel="noreferrer noopener">hun3555</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/13" target="_blank" rel="noreferrer noopener">이분 매칭</a></li>
</ul>



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



<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)"/>



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



<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>
#include &lt;vector>
#include &lt;cstring>

using namespace std;

int N, M;
vector&lt;int> cowInfo[201]; // 각 소가 원하는 축사 리스트
int barnMatch[201];       // barnMatch[b] = 소 번호 (b번 축사에 배정된 소)
bool visited[201];        // DFS 방문 체크

// 축사 매칭 시도
bool DFS(int cow) 
{

    for (int barn : cowInfo[cow]) 
    {
        if (visited[barn]) 
            continue;

        visited[barn] = true;

        // 이 축사가 비었거나, 이미 배정된 소가 다른 축사로 갈 수 있다면
        if (barnMatch[barn] == 0 || DFS(barnMatch[barn])) 
        {
            barnMatch[barn] = cow;
            return true;
        }
    }

    return false;
}

int main() 
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N >> M;

    for (int i = 1; i &lt;= N; i++) 
    {
        int k, barn;
        cin >> k;

        for (int j = 0; j &lt; k; j++) 
        {
            cin >> barn;
            cowInfo[i].push_back(barn);
        }
    }

    int result = 0;

    for (int i = 1; i &lt;= N; i++) 
    {
        memset(visited, false, sizeof(visited));

        if (DFS(i)) 
            result++;
    }

    cout &lt;&lt; result &lt;&lt; "\n";

    return 0;
}
</pre>



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



<figure class="wp-block-image size-full"><img decoding="async" width="1043" height="40" src="https://lycos7560.com/wp-content/uploads/2025/05/image-9.png" alt="" class="wp-image-40035" srcset="https://lycos7560.com/wp-content/uploads/2025/05/image-9.png 1043w, https://lycos7560.com/wp-content/uploads/2025/05/image-9-300x12.png 300w, https://lycos7560.com/wp-content/uploads/2025/05/image-9-768x29.png 768w" sizes="(max-width: 1043px) 100vw, 1043px" /></figure>



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



<p>백준에 해당 문제와 코드가 같은 문제</p>



<p><a href="https://www.acmicpc.net/problem/11375" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11375</a></p>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/baekjoon_online_judge/%eb%b0%b1%ec%a4%80-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/">백준 2188번 (축사 배정, 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-2188%eb%b2%88-%ec%b6%95%ec%82%ac-%eb%b0%b0%ec%a0%95-c-%ec%b6%94%ea%b0%80-%eb%b0%98%eb%a1%80-baekjoon/40032/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11722번 (가장 긴 감소하는 부분 수열, C++)</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11722%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ea%b0%90%ec%86%8c%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c/40330/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11722%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ea%b0%90%ec%86%8c%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c/40330/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 13 May 2025 17:48:00 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11722번]]></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[백준 11722]]></category>
		<category><![CDATA[백준 11722번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=40330</guid>

					<description><![CDATA[<p>가장 긴 감소하는 부분 수열 https://www.acmicpc.net/problem/11722 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 256 MB 43124 26527 21831 62.422% 문제 수열 A가 주어졌을 때, 가장 긴 감소하는&#160;부분&#160;수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 30, 10, 20, 20, 10} 인 경우에 가장 긴 감소하는 부분&#160;수열은&#160;A = {10,&#160;30, 10,&#160;20, [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11722%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ea%b0%90%ec%86%8c%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c/40330/">백준 11722번 (가장 긴 감소하는 부분 수열, C++)</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[				<div class="wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-87ca5ab3      "
					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><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#입력" class="uagb-toc-link__trigger">입력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출력" class="uagb-toc-link__trigger">출력</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-1" class="uagb-toc-link__trigger">예제 입력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-1" class="uagb-toc-link__trigger">예제 출력 1</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-2" class="uagb-toc-link__trigger">예제 입력 2</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-2" class="uagb-toc-link__trigger">예제 출력 2</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-3" class="uagb-toc-link__trigger">예제 입력 3</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-3" class="uagb-toc-link__trigger">예제 출력 3</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-입력-3" class="uagb-toc-link__trigger">예제 입력 3</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#예제-출력-3" class="uagb-toc-link__trigger">예제 출력 3</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#출처" class="uagb-toc-link__trigger">출처</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#비슷한-문제" class="uagb-toc-link__trigger">비슷한 문제</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#알고리즘-분류" class="uagb-toc-link__trigger">알고리즘 분류</a><li class="uagb-toc__list"><li class="uagb-toc__list"><a href="#이진-탐색을-활용한-lds-longest-decreasing-subsequence-최장-감소-부분-수열" class="uagb-toc-link__trigger">이진 탐색을 활용한 LDS (Longest Decreasing Subsequence, 최장 감소 부분 수열)</a></ul></ol>					</div>
									</div>
				</div>
			


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



<h2 class="wp-block-heading">가장 긴 감소하는 부분 수열 <img decoding="async" width="35" height="45" class="wp-image-37910" style="width: 35px;" 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: 35px) 100vw, 35px" /></h2>



<p><a href="https://www.acmicpc.net/problem/11722" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11722</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>시간 제한</th><th>메모리 제한</th><th>제출</th><th>정답</th><th>맞힌 사람</th><th>정답 비율</th></tr></thead><tbody><tr><td>1 초</td><td>256 MB</td><td>43124</td><td>26527</td><td>21831</td><td>62.422%</td></tr></tbody></table></figure>



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



<p>수열 A가 주어졌을 때, 가장 긴 감소하는&nbsp;부분&nbsp;수열을 구하는 프로그램을 작성하시오.</p>



<p>예를 들어, 수열 A = {10, 30, 10, 20, 20, 10} 인 경우에 가장 긴 감소하는 부분&nbsp;수열은&nbsp;A = {10,&nbsp;<strong>30</strong>, 10,&nbsp;<strong>20</strong>, 20,&nbsp;<strong>10</strong>}&nbsp; 이고, 길이는 3이다.</p>



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



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



<p>첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000)이 주어진다.</p>



<p>둘째 줄에는 수열 A를 이루고 있는 A<sub>i</sub>가 주어진다. (1 ≤ A<sub>i</sub>&nbsp;≤ 1,000)</p>



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



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



<p>첫째 줄에 수열 A의 가장 긴 감소하는 부분 수열의 길이를 출력한다.</p>



<div class="wp-block-uagb-container uagb-block-aa78ee06 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-70c24c24">
<h3 class="wp-block-heading">예제 입력 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="">6
10 30 10 20 20 10</pre>
</div>



<div class="wp-block-uagb-container uagb-block-2c88bb50">
<h3 class="wp-block-heading">예제 출력 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="">3</pre>
</div>
</div></div>



<div class="wp-block-uagb-container uagb-block-12f8eb1d alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-a9d72474">
<h3 class="wp-block-heading">예제 입력 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="">5
5 5 5 5 5</pre>
</div>



<div class="wp-block-uagb-container uagb-block-38b0760c">
<h3 class="wp-block-heading">예제 출력 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="">1</pre>
</div>
</div></div>



<div class="wp-block-uagb-container uagb-block-ca1b92f9 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-323f918b">
<h3 class="wp-block-heading">예제 입력 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="">6
10 9 8 7 6 5</pre>
</div>



<div class="wp-block-uagb-container uagb-block-031cf3d6">
<h3 class="wp-block-heading">예제 출력 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="">6</pre>
</div>
</div></div>



<div class="wp-block-uagb-container uagb-block-bd255242 alignfull uagb-is-root-container"><div class="uagb-container-inner-blocks-wrap">
<div class="wp-block-uagb-container uagb-block-633fd84b">
<h3 class="wp-block-heading">예제 입력 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="">8
50 40 30 60 20 10 70 5</pre>
</div>



<div class="wp-block-uagb-container uagb-block-5996b6b7">
<h3 class="wp-block-heading">예제 출력 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="">5</pre>
</div>
</div></div>



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



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



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



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



<h3 class="wp-block-heading">비슷한 문제</h3>



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/11053" target="_blank" rel="noreferrer noopener">11053번. 가장 긴 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11054" target="_blank" rel="noreferrer noopener">11054번. 가장 긴 바이토닉 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/11055" target="_blank" rel="noreferrer noopener">11055번. 가장 큰 증가하는 부분 수열</a></li>



<li><a href="https://www.acmicpc.net/problem/12015" target="_blank" rel="noreferrer noopener">12015번. 가장 긴 증가하는 부분 수열 2</a></li>



<li><a href="https://www.acmicpc.net/problem/12738" target="_blank" rel="noreferrer noopener">12738번. 가장 긴 증가하는 부분 수열 3</a></li>



<li><a href="https://www.acmicpc.net/problem/14002" target="_blank" rel="noreferrer noopener">14002번. 가장 긴 증가하는 부분 수열 4</a></li>



<li><a href="https://www.acmicpc.net/problem/14003" target="_blank" rel="noreferrer noopener">14003번. 가장 긴 증가하는 부분 수열 5</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/25" 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--70);margin-bottom:var(--wp--preset--spacing--70)"/>



<h3 class="wp-block-heading"><strong>이진 탐색을 활용한 <strong>LDS (Longest Decreasing Subsequence, 최장 감소 부분 수열)</strong></strong></h3>



<p><strong>O(N log N)</strong></p>



<figure class="wp-block-image size-full"><img decoding="async" width="1150" height="81" src="https://lycos7560.com/wp-content/uploads/2025/10/image-8.png" alt="" class="wp-image-40331" srcset="https://lycos7560.com/wp-content/uploads/2025/10/image-8.png 1150w, https://lycos7560.com/wp-content/uploads/2025/10/image-8-300x21.png 300w, https://lycos7560.com/wp-content/uploads/2025/10/image-8-768x54.png 768w" sizes="(max-width: 1150px) 100vw, 1150px" /></figure>



<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;vector>
#include &lt;algorithm>

using namespace std;

pair&lt;int, vector&lt;int>> FindLDS(const vector&lt;int>&amp; input)
{
    int n = input.size();
    vector&lt;int> tail; // 각 길이별 LDS의 마지막 원소 인덱스
    vector&lt;int> prev_idx(n, -1); // 이전 원소의 인덱스 추적

    for (int i = 0; i &lt; n; i++)
    {
        int lo = 0;
        int hi = (int)tail.size() - 1;
        int pos = (int)tail.size();

        while (lo &lt;= hi)
        {
            int mid = (lo + hi) / 2;
            if (input[tail[mid]] > input[i])
            {
                lo = mid + 1;
            }
            else
            {
                pos = mid;
                hi = mid - 1;
            }
        }

        if (pos > 0)
        {
            prev_idx[i] = tail[pos - 1];
        }
        else
        {
            prev_idx[i] = -1;
        }

        if (pos == (int)tail.size())
        {
            tail.push_back(i);
        }
        else
        {
            tail[pos] = i;
        }
    }

    vector&lt;int> lds;
    for (int idx = tail.back(); idx != -1; idx = prev_idx[idx])
    {
        lds.push_back(input[idx]);
    }
    reverse(lds.begin(), lds.end());

    return { tail.size(), lds };
}

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

    int N;
    std::cin >> N;

    vector&lt;int> input(N);
    for (int i = 0; i &lt; N; i++)
        std::cin >> input[i];

    auto lds = FindLDS(input);

    cout &lt;&lt; lds.first;

    return 0;
}</pre>



<p></p>



<h4 class="wp-block-heading">input = [9, 4, 3, 2, 5]</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>단계</th><th>i</th><th>input[i]</th><th>tail 내용 (저장된 인덱스)</th><th>의미</th></tr></thead><tbody><tr><td>0</td><td>0</td><td>9</td><td>[0]</td><td>길이 1 LDS → 끝 원소는 input[0] = 9</td></tr><tr><td>1</td><td>1</td><td>4</td><td>[0, 1]</td><td>길이 2 LDS → 끝 원소는 input[1] = 4</td></tr><tr><td>2</td><td>2</td><td>3</td><td>[0, 1, 2]</td><td>길이 3 LDS → 끝 원소는 input[2] = 3</td></tr><tr><td>3</td><td>3</td><td>2</td><td>[0, 1, 2, 3]</td><td>길이 4 LDS → 끝 원소는 input[3] = 2</td></tr><tr><td>4</td><td>4</td><td>5</td><td>[0, 4, 2, 3]</td><td>길이 1~4까지의 각 LDS의 끝 인덱스 갱신</td></tr></tbody></table></figure>



<p></p>



<h4 class="wp-block-heading">input = [10, 8, 6, 5, 7, 4, 3, 2, 9]</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>단계</th><th>i</th><th>input[i]</th><th>이진 탐색 결과 <code>pos</code></th><th><code>tail</code> 내용 (저장된 인덱스)</th><th><code>tail</code>에 대응되는 실제 값</th><th><code>prev_idx[i]</code></th><th>동작 설명</th></tr></thead><tbody><tr><td>0</td><td>0</td><td>10</td><td>0</td><td>[0]</td><td>[10]</td><td>-1</td><td>첫 원소 → 길이 1 LDS 시작</td></tr><tr><td>1</td><td>1</td><td>8</td><td>1</td><td>[0, 1]</td><td>[10, 8]</td><td>0</td><td>10→8 감소, 길이 2 LDS</td></tr><tr><td>2</td><td>2</td><td>6</td><td>2</td><td>[0, 1, 2]</td><td>[10, 8, 6]</td><td>1</td><td>8→6 감소, 길이 3 LDS</td></tr><tr><td>3</td><td>3</td><td>5</td><td>3</td><td>[0, 1, 2, 3]</td><td>[10, 8, 6, 5]</td><td>2</td><td>6→5 감소, 길이 4 LDS</td></tr><tr><td>4</td><td>4</td><td>7</td><td>2</td><td>[0, 1, 4, 3]</td><td>[10, 8, 7, 5]</td><td>1</td><td>8→7으로 갱신 (길이 3의 마지막 갱신)</td></tr><tr><td>5</td><td>5</td><td>4</td><td>4</td><td>[0, 1, 4, 3, 5]</td><td>[10, 8, 7, 5, 4]</td><td>3</td><td>5→4로 확장</td></tr><tr><td>6</td><td>6</td><td>3</td><td>5</td><td>[0, 1, 4, 3, 5, 6]</td><td>[10, 8, 7, 5, 4, 3]</td><td>5</td><td>4→3 감소 확장</td></tr><tr><td>7</td><td>7</td><td>2</td><td>6</td><td>[0, 1, 4, 3, 5, 6, 7]</td><td>[10, 8, 7, 5, 4, 3, 2]</td><td>6</td><td>3→2 감소 확장</td></tr><tr><td>8</td><td>8</td><td>9</td><td>1</td><td>[0, 8, 4, 3, 5, 6, 7]</td><td>[10, 9, 7, 5, 4, 3, 2]</td><td>0</td><td>10→9으로 교체 (2번째 LDS 끝 갱신)</td></tr></tbody></table></figure>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>항목</th><th>의미</th></tr></thead><tbody><tr><td><code>tail.size()</code></td><td><strong>7</strong> → 최장 감소 부분 수열의 길이 = 7</td></tr><tr><td><code>tail.back()</code></td><td><code>7</code> → input[7] = 2 → LDS의 마지막 원소</td></tr><tr><td><code>prev_idx</code></td><td><code>[ -1, 0, 1, 2, 1, 3, 5, 6, 0 ]</code> → 각 원소가 연결된 이전 원소 인덱스</td></tr></tbody></table></figure>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11722%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ea%b0%90%ec%86%8c%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c/40330/">백준 11722번 (가장 긴 감소하는 부분 수열, C++)</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-11722%eb%b2%88-%ea%b0%80%ec%9e%a5-%ea%b8%b4-%ea%b0%90%ec%86%8c%ed%95%98%eb%8a%94-%eb%b6%80%eb%b6%84-%ec%88%98%ec%97%b4-c/40330/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 10813번 (공 바꾸기, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-10813%eb%b2%88-%ea%b3%b5-%eb%b0%94%ea%be%b8%ea%b8%b0-c-baekjoon/39908/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-10813%eb%b2%88-%ea%b3%b5-%eb%b0%94%ea%be%b8%ea%b8%b0-c-baekjoon/39908/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Fri, 28 Feb 2025 02:32:58 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[10813]]></category>
		<category><![CDATA[10813번]]></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[백준]]></category>
		<category><![CDATA[백준 10813]]></category>
		<category><![CDATA[백준 10813번]]></category>
		<category><![CDATA[시뮬레이션]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=39908</guid>

					<description><![CDATA[<p>공 바꾸기 https://www.acmicpc.net/problem/10813 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 256 MB 90321 56671 50445 63.108% 문제 도현이는 바구니를 총 N개 가지고 있고, 각각의 바구니에는 1번부터 N번까지 번호가 매겨져 있다. 바구니에는 공이 1개씩 들어있고, 처음에는 바구니에 적혀있는 번호와 같은 번호가 적힌 공이 들어있다. 도현이는 앞으로 M번 공을 바꾸려고 한다. 도현이는 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-10813%eb%b2%88-%ea%b3%b5-%eb%b0%94%ea%be%b8%ea%b8%b0-c-baekjoon/39908/">백준 10813번 (공 바꾸기, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">공 바꾸기 <img decoding="async" width="35" height="45" class="wp-image-37920" style="width: 35px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Bronze_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Bronze_2.jpg 1250w, https://lycos7560.com/wp-content/uploads/2024/03/Bronze_2-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2024/03/Bronze_2-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2024/03/Bronze_2-1200x1536.jpg 1200w" sizes="(max-width: 35px) 100vw, 35px" /></h2>



<p><a href="https://www.acmicpc.net/problem/10813" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/10813</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">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">90321</td><td class="has-text-align-left" data-align="left">56671</td><td class="has-text-align-left" data-align="left">50445</td><td class="has-text-align-left" data-align="left">63.108%</td></tr></tbody></table></figure>



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



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



<p>도현이는 바구니를 총 N개 가지고 있고, 각각의 바구니에는 1번부터 N번까지 번호가 매겨져 있다. </p>



<p>바구니에는 공이 1개씩 들어있고, 처음에는 바구니에 적혀있는 번호와 같은 번호가 적힌 공이 들어있다.</p>



<p>도현이는 앞으로 M번 공을 바꾸려고 한다. 도현이는 공을 바꿀 바구니 2개를 선택하고, 두 바구니에 들어있는 공을 서로 교환한다.</p>



<p>공을 어떻게 바꿀지가 주어졌을 때, M번 공을 바꾼 이후에 각 바구니에 어떤 공이 들어있는지 구하는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에 N (1 ≤ N ≤ 100)과 M (1 ≤ M ≤ 100)이 주어진다.</p>



<p>둘째 줄부터 M개의 줄에 걸쳐서 공을 교환할 방법이 주어진다. </p>



<p>각 방법은 두 정수 i j로 이루어져 있으며, i번 바구니와 j번 바구니에 들어있는 공을 교환한다는 뜻이다. (1 ≤ i ≤ j ≤ N)</p>



<p>도현이는 입력으로 주어진 순서대로 공을 교환한다.</p>



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



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



<p>1번 바구니부터 N번 바구니에 들어있는 공의 번호를 공백으로 구분해 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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 4
1 2
3 4
1 4
2 2</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">3 1 4 2 5</pre>



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



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



<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/onjo0127" target="_blank" rel="noreferrer noopener">onjo0127</a></li>
</ul>



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



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



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



<li><a href="https://www.acmicpc.net/problem/tag/141" 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)"/>



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



<p>단순한 구현 문제</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 N, M;
int Arr[101] = { 0 };

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

	cin >> N >> M;

    for (int i = 1; i &lt;= N; i++)
        Arr[i] = i;

	int t1, t2;

    for (int i = 0; i &lt; M; i++) {
        cin >> t1 >> t2;
		swap(Arr[t1], Arr[t2]);
    }

    for (int i = 1; i &lt;= N; i++)
		cout &lt;&lt; Arr[i] &lt;&lt; " ";

    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="965" height="82" src="https://lycos7560.com/wp-content/uploads/2025/03/image.png" alt="" class="wp-image-39909" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image.png 965w, https://lycos7560.com/wp-content/uploads/2025/03/image-300x25.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-768x65.png 768w" sizes="(max-width: 965px) 100vw, 965px" /></figure>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-10813%eb%b2%88-%ea%b3%b5-%eb%b0%94%ea%be%b8%ea%b8%b0-c-baekjoon/39908/">백준 10813번 (공 바꾸기, 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-10813%eb%b2%88-%ea%b3%b5-%eb%b0%94%ea%be%b8%ea%b8%b0-c-baekjoon/39908/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 9935번 (문자열 폭발, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Thu, 13 Feb 2025 10:37:52 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[9935]]></category>
		<category><![CDATA[9935번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[FRULA]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[문자열 폭발]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 9935번]]></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=39929</guid>

					<description><![CDATA[<p>문자열 폭발 https://www.acmicpc.net/problem/9935 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 (추가 시간 없음) 128 MB 97727 27262 19242 27.023% 문제 상근이는 문자열에 폭발 문자열을 심어 놓았다. 폭발 문자열이 폭발하면 그 문자는 문자열에서 사라지며, 남은 문자열은 합쳐지게 된다. 폭발은 다음과 같은 과정으로 진행된다. 상근이는 모든 폭발이 끝난 후에 어떤 문자열이 남는지 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/">백준 9935번 (문자열 폭발, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">문자열 폭발 <img decoding="async" width="30" height="38" class="wp-image-37904" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_4.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_4-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/9935" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/9935</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">128 MB</td><td class="has-text-align-left" data-align="left">97727</td><td class="has-text-align-left" data-align="left">27262</td><td class="has-text-align-left" data-align="left">19242</td><td class="has-text-align-left" data-align="left">27.023%</td></tr></tbody></table></figure>



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



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



<p>상근이는 문자열에 폭발 문자열을 심어 놓았다. </p>



<p>폭발 문자열이 폭발하면 그 문자는 문자열에서 사라지며, 남은 문자열은 합쳐지게 된다.</p>



<p>폭발은 다음과 같은 과정으로 진행된다.</p>



<ul class="wp-block-list">
<li>문자열이 폭발 문자열을 포함하고 있는 경우에, 모든 폭발 문자열이 폭발하게 된다. <br>남은 문자열을 순서대로 이어 붙여 새로운 문자열을 만든다.</li>



<li>새로 생긴 문자열에 폭발 문자열이 포함되어 있을 수도 있다.</li>



<li>폭발은 폭발 문자열이 문자열에 없을 때까지 계속된다.</li>
</ul>



<p>상근이는 모든 폭발이 끝난 후에 어떤 문자열이 남는지 구해보려고 한다. </p>



<p>남아있는 문자가 없는 경우가 있다. 이때는 &#8220;FRULA&#8221;를 출력한다.</p>



<p>폭발 문자열은 같은 문자를 두 개 이상 포함하지 않는다.</p>



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



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



<p>첫째 줄에 문자열이 주어진다. 문자열의 길이는 1보다 크거나 같고, 1,000,000보다 작거나 같다.</p>



<p>둘째 줄에 폭발 문자열이 주어진다. 길이는 1보다 크거나 같고, 36보다 작거나 같다.</p>



<p>두 문자열은 모두 알파벳 소문자와 대문자, 숫자 0, 1, &#8230;, 9로만 이루어져 있다.</p>



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



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



<p>첫째 줄에 모든 폭발이 끝난 후 남은 문자열을 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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="">mirkovC4nizCC44
C4</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">mirkovniz</pre>



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



<h3 class="wp-block-heading">예제 입력 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="">12ab112ab2ab
12ab</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">FRULA</pre>



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



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



<p><a href="https://www.acmicpc.net/category/45">Contest</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/17">Croatian Open Competition in Informatics</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/272">COCI 2013/2014</a>&nbsp;&gt;&nbsp;<a href="https://www.acmicpc.net/category/detail/1230">Contest #5</a>&nbsp;3번</p>



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



<li>문제의 오타를 찾은 사람: <a href="https://www.acmicpc.net/user/chan4928">chan4928</a></li>



<li>시간 제한을 수정한 사람: <a href="https://www.acmicpc.net/user/jh05013">jh05013</a></li>



<li>데이터를 추가한 사람: <a href="https://www.acmicpc.net/user/limequeen">limequeen</a>, <a href="https://www.acmicpc.net/user/mrseos">mrseos</a></li>
</ul>



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



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



<ul class="wp-block-list">
<li><a href="https://www.acmicpc.net/problem/tag/175" target="_blank" rel="noreferrer noopener">자료 구조</a></li>



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



<li><a href="https://www.acmicpc.net/problem/tag/71" 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)"/>



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



<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>
#include &lt;string>

using namespace std;

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

    string S, C;
    cin >> S >> C;

    string Res; 

    for (char ch : S)
    {
        Res.push_back(ch);

        // Res의 마지막 부분이 c와 같은지 확인
        if (Res.size() >= C.size() &amp;&amp; Res.substr(Res.size() - C.size()) == C)
        {
            // 문자열에서 폭발 문자열 제거
            Res.erase(Res.size() - C.size()); 
        }
    }

    cout &lt;&lt; (Res.empty() ? "FRULA" : Res);

    return 0;
}
</pre>



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



<p><strong>예시 </strong><br><code>ch = 'm'</code>: <code>Res = "m"</code><br><code>ch = 'i'</code>: <code>Res = "mi"</code><br><code>ch = 'r'</code>: <code>Res = "mir"</code><br><code>ch = 'k'</code>: <code>Res = "mirk"</code><br><code>ch = 'o'</code>: <code>Res = "mirko"</code><br><code>ch = 'v'</code>: <code>Res = "mirkov"</code><br><code>ch = 'C'</code>: <code>Res = "mirkovC"</code><br><code>ch = '4'</code>: <code>Res = "mirkovC4"</code><br><code>ch = 'n'</code>: <code>Res = "mirkovn"</code><br><code>ch = 'i'</code>: <code>Res = "mirkovni"</code><br><code>ch = 'z'</code>: <code>Res = "mirkoviz"</code><br><code>ch = 'C'</code>: <code>Res = "mirkovnizC"</code><br><code>ch = 'C'</code>: <code>Res = "mirkovnizCC"</code><br><code>ch = '4'</code>: <code>Res = "mirkovnizCC4"</code><br><code>ch = '4'</code>: <code>Res = "mirkovnizC4"</code><br><em><strong>Res = &#8220;mirkovniz&#8221;</strong></em></p>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/">백준 9935번 (문자열 폭발, 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-9935%eb%b2%88-%eb%ac%b8%ec%9e%90%ec%97%b4-%ed%8f%ad%eb%b0%9c-c-baekjoon/39929/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 11444번 (피보나치 수 6, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Mon, 10 Feb 2025 01:13:51 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[11444]]></category>
		<category><![CDATA[11444번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 11444번]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<category><![CDATA[피보나치]]></category>
		<category><![CDATA[피보나치 수]]></category>
		<category><![CDATA[피보나치 수 6]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=39936</guid>

					<description><![CDATA[<p>피보나치 수 6 https://www.acmicpc.net/problem/11444 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 1 초 256 MB 27890 12508 10367 47.603% 문제 피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. 그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다. 이를 식으로 써보면 Fn&#160;= Fn-1&#160;+ Fn-2&#160;(n ≥ 2)가 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/">백준 11444번 (피보나치 수 6, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">피보나치 수 6 <img decoding="async" width="30" height="38" class="wp-image-37903" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Gold_2.jpg 400w, https://lycos7560.com/wp-content/uploads/2024/03/Gold_2-234x300.jpg 234w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/11444" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/11444</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">1 초</td><td class="has-text-align-left" data-align="left">256 MB</td><td class="has-text-align-left" data-align="left">27890</td><td class="has-text-align-left" data-align="left">12508</td><td class="has-text-align-left" data-align="left">10367</td><td class="has-text-align-left" data-align="left">47.603%</td></tr></tbody></table></figure>



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



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



<p>피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. </p>



<p>그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다.</p>



<p>이를 식으로 써보면 F<sub>n</sub>&nbsp;= F<sub>n-1</sub>&nbsp;+ F<sub>n-2</sub>&nbsp;(n ≥ 2)가 된다.</p>



<p>n=17일때 까지 피보나치 수를 써보면 다음과 같다.</p>



<p>0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597</p>



<p>n이 주어졌을 때, n번째 피보나치 수를 구하는 프로그램을 작성하시오.</p>



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



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



<p>첫째 줄에 n이 주어진다. n은 1,000,000,000,000,000,000보다 작거나 같은 자연수이다.</p>



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



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



<p>첫째 줄에 n번째 피보나치 수를 1,000,000,007으로 나눈 나머지를 출력한다.</p>



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



<h3 class="wp-block-heading">예제 입력 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="">1000</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">517691607</pre>



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



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



<ul class="wp-block-list">
<li>문제를 만든 사람: <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>



<h3 class="wp-block-heading">알고리즘 분류</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/39" target="_blank" rel="noreferrer noopener">분할 정복을 이용한 거듭제곱</a></li>
</ul>



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



<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)"/>



<p>문제를 그대로 구현한 코드</p>



<p>답은 정확하게 나오지만 N의 크기가 증가하면 증가할 수록 연산량이 증가하여 시간초과 결과가 나오는 것은 당연하다.</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;

long long int N, t1, t2, t3;

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

	cin >> N;
    t1 = 0;
    t2 = 1;
    t3 = 1;

	if (N == 0) cout &lt;&lt; t1;
	else if (N == 1) cout &lt;&lt; t2;
	else if (N == 2) cout &lt;&lt; t3;
    else 
    {
        for (int i = 2; i &lt; N; i++)
        {
            t1 = t2 % 1000000007;
            t2 = t3 % 1000000007;
            t3 = (t1 + t2) % 1000000007;
        }
    }

	cout &lt;&lt; t3 &lt;&lt; endl;
    return 0;
}
</pre>



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



<p>피보나치 수를 더 좋은 방법으로 해결하는 방법은 아래 링크에 자세하게 설명되어 있다. </p>



<p><a href="https://www.acmicpc.net/blog/view/28" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/blog/view/28</a></p>



<p>위의 게시글의 내용을 살펴보면 2가지로 문제를 해결하고 있다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="844" height="369" src="https://lycos7560.com/wp-content/uploads/2025/03/image-14.png" alt="" class="wp-image-39937" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-14.png 844w, https://lycos7560.com/wp-content/uploads/2025/03/image-14-300x131.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-14-768x336.png 768w" sizes="(max-width: 844px) 100vw, 844px" /><figcaption class="wp-element-caption"><strong>행렬을 이용한 풀이법</strong></figcaption></figure>



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



<figure class="wp-block-image size-full"><img decoding="async" width="850" height="218" src="https://lycos7560.com/wp-content/uploads/2025/03/image-15.png" alt="" class="wp-image-39938" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-15.png 850w, https://lycos7560.com/wp-content/uploads/2025/03/image-15-300x77.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-15-768x197.png 768w" sizes="(max-width: 850px) 100vw, 850px" /><figcaption class="wp-element-caption"><strong>메모이제이션, 홀수/짝수를 이용한 풀이법</strong></figcaption></figure>



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



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



<p>메모이제이션, 홀수/짝수를 이용한 풀이법을 적용한 코드</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>
#include &lt;map>

using namespace std;

map&lt;long long, long long> d; // 메모이제이션을 위한 map
const long long mod = 1000000007; 

// 피보나치 수를 재귀적으로 계산하는 함수
long long Fibonacci_recursive(long long n) {
    if (n &lt;= 0) {
        return 0;
    }
    else if (n == 1 || n == 2) {
        return 1;
    }
    else if (d.count(n) > 0) {
        return d[n];
    }
    else {
        // n이 홀수
        if (n % 2 == 1) {
            long long m = (n + 1) / 2;
            long long t1 = Fibonacci_recursive(m);
            long long t2 = Fibonacci_recursive(m - 1);
            d[n] = t1 * t1 + t2 * t2;
            d[n] %= mod;
            return d[n];
        }
        else { // n이 짝수
            long long m = n / 2;
            long long t1 = Fibonacci_recursive(m - 1);
            long long t2 = Fibonacci_recursive(m);
            d[n] = (2LL * t1 + t2) * t2;
            d[n] %= mod;
            return d[n];
        }
    }
}

int main() {
    long long n;
    cin >> n; // 입력값 받기
    cout &lt;&lt; Fibonacci_recursive(n) &lt;&lt; '\n'; // n번째 피보나치 수 출력
    return 0;
}
</pre>



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



<p>개인적으로 이런 수학적인 문제는 어느 정도 고민해보다 검색해보는 것이 공부하는 데 더 도움이 되는 것 같다.</p>



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



<figure class="wp-block-image size-full"><img decoding="async" width="964" height="76" src="https://lycos7560.com/wp-content/uploads/2025/03/image-16.png" alt="" class="wp-image-39939" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-16.png 964w, https://lycos7560.com/wp-content/uploads/2025/03/image-16-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-16-768x61.png 768w" sizes="(max-width: 964px) 100vw, 964px" /></figure>



<p></p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/">백준 11444번 (피보나치 수 6, 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-11444%eb%b2%88-%ed%94%bc%eb%b3%b4%eb%82%98%ec%b9%98-%ec%88%98-6-c-baekjoon/39936/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 30804번 (과일 탕후루, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-30804%eb%b2%88-%ea%b3%bc%ec%9d%bc-%ed%83%95%ed%9b%84%eb%a3%a8-c-baekjoon/39911/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-30804%eb%b2%88-%ea%b3%bc%ec%9d%bc-%ed%83%95%ed%9b%84%eb%a3%a8-c-baekjoon/39911/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Sun, 05 Jan 2025 05:16:08 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[30804]]></category>
		<category><![CDATA[30804번]]></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[백준]]></category>
		<category><![CDATA[백준 30804]]></category>
		<category><![CDATA[백준 30804번]]></category>
		<category><![CDATA[브루트 포스]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=39911</guid>

					<description><![CDATA[<p>과일 탕후루 https://www.acmicpc.net/problem/30804 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 2 초 1024 MB 13648 4768 3826 33.976% 문제 은하는 긴 막대에 N개의 과일이 꽂혀있는 과일 탕후루를 만들었습니다. 과일의 각 종류에는 1부터 9까지의 번호가 붙어있고, 앞쪽부터 차례로  번 과일이 꽂혀있습니다. 과일 탕후루를 다 만든 은하가 주문을 다시 확인해보니 과일을 두 종류 이하로 사용해 달라는 요청이 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-30804%eb%b2%88-%ea%b3%bc%ec%9d%bc-%ed%83%95%ed%9b%84%eb%a3%a8-c-baekjoon/39911/">백준 30804번 (과일 탕후루, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">과일 탕후루 <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" /></h2>



<p><a href="https://www.acmicpc.net/problem/30804" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/30804</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">1024 MB</td><td class="has-text-align-left" data-align="left">13648</td><td class="has-text-align-left" data-align="left">4768</td><td class="has-text-align-left" data-align="left">3826</td><td class="has-text-align-left" data-align="left">33.976%</td></tr></tbody></table></figure>



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



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



<p>은하는 긴 막대에 N개의 과일이 꽂혀있는 과일 탕후루를 만들었습니다. </p>



<p>과일의 각 종류에는 1부터 9까지의 번호가 붙어있고, 앞쪽부터 차례로 <img decoding="async" width="104" height="22" class="wp-image-39912" style="width: 104px;" src="https://lycos7560.com/wp-content/uploads/2025/03/image-1.png" alt=""> 번 과일이 꽂혀있습니다. </p>



<p>과일 탕후루를 다 만든 은하가 주문을 다시 확인해보니 과일을 두 종류 이하로 사용해 달라는 요청이 있었습니다.</p>



<p>탕후루를 다시 만들 시간이 없었던 은하는, 막대의 앞쪽과 뒤쪽에서 몇 개의 과일을 빼서 두 종류 이하의 과일만 남기기로 했습니다. </p>



<p>앞에서 a개, 뒤에서 b개의 과일을 빼면 <img decoding="async" width="200" height="17" class="wp-image-39913" style="width: 200px;" src="https://lycos7560.com/wp-content/uploads/2025/03/image-2.png" alt=""> 번 과일, 총 N &#8211; (a + b)개가 꽂혀있는 탕후루가 됩니다. <img decoding="async" width="150" height="27" class="wp-image-39914" style="width: 150px;" src="https://lycos7560.com/wp-content/uploads/2025/03/image-3.png" alt=""></p>



<p>이렇게 만들 수 있는 과일을 두 종류 이하로 사용한 탕후루 중에서, 과일의 개수가 가장 많은 탕후루의 과일 개수를 구하세요.</p>



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



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



<p>첫 줄에 과일의 개수 N이 주어집니다. <img decoding="async" width="145" height="23" class="wp-image-39915" style="width: 145px;" src="https://lycos7560.com/wp-content/uploads/2025/03/image-4.png" alt=""></p>



<p>둘째 줄에 탕후루에 꽂힌 과일을 의미하는 N개의 정수 <img decoding="async" width="78" height="24" class="wp-image-39917" style="width: 78px;" src="https://lycos7560.com/wp-content/uploads/2025/03/image-6.png" alt=""> 이 공백으로 구분되어 주어집니다. <img decoding="async" width="98" height="22" class="wp-image-39916" style="width: 98px;" src="https://lycos7560.com/wp-content/uploads/2025/03/image-5.png" alt=""></p>



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



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



<p>문제의 방법대로 만들 수 있는 과일을 두 종류 이하로 사용한 탕후루 중에서, 과일의 개수가 가장 많은 탕후루의 과일 개수를 첫째 줄에 출력하세요.</p>



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



<h3 class="wp-block-heading">예제 입력 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
5 1 1 2 1</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">4</pre>



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



<p>과일을 앞에서 1개, 뒤에서 0개의 과일을 빼면 남은 과일은 1, 1, 2, 1번 과일이 꽂혀있는 탕후루가 됩니다. </p>



<p>과일의 개수는 4개입니다.</p>



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



<h3 class="wp-block-heading">예제 입력 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 1</pre>



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



<h3 class="wp-block-heading">예제 출력 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</pre>



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



<p>탕후루가 이미 두 종류 이하의 과일로만 이루어져 있습니다.</p>



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



<h3 class="wp-block-heading">예제 입력 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
1 2 3 4 5 6 7 8 9</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">2</pre>



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



<p>과일을 앞에서 3개, 뒤에서 4개의 과일을 빼면 남은 과일은 4, 5번 과일이 꽂혀있는 탕후루가 됩니다. </p>



<p>과일의 개수는 2개입니다.</p>



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



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



<p><a href="https://www.acmicpc.net/category/45" target="_blank" rel="noreferrer noopener">Contest</a> > <a href="https://www.acmicpc.net/category/859" target="_blank" rel="noreferrer noopener">solved.ac</a> > <a href="https://www.acmicpc.net/category/1003" target="_blank" rel="noreferrer noopener">solved.ac Grand Arena #3</a> > <a href="https://www.acmicpc.net/category/detail/4059" target="_blank" rel="noreferrer noopener">Division 2</a> C번</p>



<p><a href="https://www.acmicpc.net/category/45" target="_blank" rel="noreferrer noopener">Contest</a> > <a href="https://www.acmicpc.net/category/859" target="_blank" rel="noreferrer noopener">solved.ac</a> > <a href="https://www.acmicpc.net/category/1003" target="_blank" rel="noreferrer noopener">solved.ac Grand Arena #3</a> > <a href="https://www.acmicpc.net/category/detail/4060" target="_blank" rel="noreferrer noopener">Division 1</a> A번</p>



<ul class="wp-block-list">
<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/cologne">cologne</a>, <a href="https://www.acmicpc.net/user/havana723">havana723</a>, <a href="https://www.acmicpc.net/user/solvedac">solvedac</a></li>



<li>문제를 검수한 사람: <a href="https://www.acmicpc.net/user/cologne">cologne</a>, <a href="https://www.acmicpc.net/user/cozyyg">cozyyg</a>, <a href="https://www.acmicpc.net/user/gs18115">gs18115</a>, <a href="https://www.acmicpc.net/user/moonrabbit2">moonrabbit2</a>, <a href="https://www.acmicpc.net/user/seonah">seonah</a></li>
</ul>



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



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



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



<li><a href="https://www.acmicpc.net/problem/tag/125" target="_blank" rel="noreferrer noopener">브루트포스 알고리즘</a></li>



<li><a href="https://www.acmicpc.net/problem/tag/80" 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)"/>



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



<p>map 자료구조, 두 포인터 알고리즘을 이용하여 문제를 해결</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>
#include &lt;map>

using namespace std;

int N;
int Arr[200000];

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 >> Arr[i];

    map&lt;int, int> fruitCount;
    int left = 0, right = 0, maxLen = 0;

    while (right &lt; N) 
    {
        // 오른쪽 과일 추가
        fruitCount[Arr[right]]++;

        while (fruitCount.size() > 2) 
        { 
            // 과일 종류가 2개 초과하면 줄이기
            fruitCount[Arr[left]]--;
            if (fruitCount[Arr[left]] == 0)
                fruitCount.erase(Arr[left]);
            left++;
        }

        // 최대 길이 갱신
        maxLen = max(maxLen, right - left + 1);
        right++;
    }

    cout &lt;&lt; maxLen &lt;&lt; "\n";

    return 0;
}
</pre>



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



<p>Arr[] 범위를 20,000으로 오타를 내고 제출하여 계속 오답을 받았다. (올바른 범위는 200,000) </p>



<figure class="wp-block-image size-full"><img decoding="async" width="961" height="181" src="https://lycos7560.com/wp-content/uploads/2025/03/image-7.png" alt="" class="wp-image-39919" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-7.png 961w, https://lycos7560.com/wp-content/uploads/2025/03/image-7-300x57.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-7-768x145.png 768w" sizes="(max-width: 961px) 100vw, 961px" /></figure>



<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-30804%eb%b2%88-%ea%b3%bc%ec%9d%bc-%ed%83%95%ed%9b%84%eb%a3%a8-c-baekjoon/39911/">백준 30804번 (과일 탕후루, 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-30804%eb%b2%88-%ea%b3%bc%ec%9d%bc-%ed%83%95%ed%9b%84%eb%a3%a8-c-baekjoon/39911/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>백준 28702번 (FizzBuzz, C++) [BAEKJOON]</title>
		<link>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-28702%eb%b2%88-fizzbuzz-c-baekjoon/39926/</link>
					<comments>https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-28702%eb%b2%88-fizzbuzz-c-baekjoon/39926/#respond</comments>
		
		<dc:creator><![CDATA[lycos7560]]></dc:creator>
		<pubDate>Tue, 24 Dec 2024 09:35:33 +0000</pubDate>
				<category><![CDATA[BaekjoonOnlineJudge]]></category>
		<category><![CDATA[C++/CPP]]></category>
		<category><![CDATA[28702]]></category>
		<category><![CDATA[28702번]]></category>
		<category><![CDATA[Baekjoon]]></category>
		<category><![CDATA[Buzz]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[Fizz]]></category>
		<category><![CDATA[FizzBuzz]]></category>
		<category><![CDATA[study]]></category>
		<category><![CDATA[공부]]></category>
		<category><![CDATA[기초]]></category>
		<category><![CDATA[문자열]]></category>
		<category><![CDATA[백준]]></category>
		<category><![CDATA[백준 28702번]]></category>
		<category><![CDATA[수학]]></category>
		<category><![CDATA[알고리즘]]></category>
		<category><![CDATA[코딩테스트]]></category>
		<category><![CDATA[코테]]></category>
		<guid isPermaLink="false">https://lycos7560.com/?p=39926</guid>

					<description><![CDATA[<p>FizzBuzz https://www.acmicpc.net/problem/28702 시간 제한 메모리 제한 제출 정답 맞힌 사람 정답 비율 0.5 초 1024 MB 13102 8022 7260 61.593% 문제 FizzBuzz 문제는 i=1,2,⋯ 에 대해 다음 규칙에 따라 문자열을 한 줄에 하나씩 출력하는 문제입니다. FizzBuzz 문제에서 연속으로 출력된 세 개의 문자열이 주어집니다. 이때, 이 세 문자열 다음에 올 문자열은 무엇일까요? 입력 FizzBuzz 문제에서 연속으로 출력된 세 [&#8230;]</p>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-28702%eb%b2%88-fizzbuzz-c-baekjoon/39926/">백준 28702번 (FizzBuzz, C++) [BAEKJOON]</a> appeared first on <a href="https://lycos7560.com">어제와 내일의 나 그 사이의 이야기</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">FizzBuzz <img decoding="async" width="30" height="38" class="wp-image-37923" style="width: 30px;" src="https://lycos7560.com/wp-content/uploads/2024/03/Bronze_1.jpg" alt="" srcset="https://lycos7560.com/wp-content/uploads/2024/03/Bronze_1.jpg 1250w, https://lycos7560.com/wp-content/uploads/2024/03/Bronze_1-234x300.jpg 234w, https://lycos7560.com/wp-content/uploads/2024/03/Bronze_1-768x983.jpg 768w, https://lycos7560.com/wp-content/uploads/2024/03/Bronze_1-1200x1536.jpg 1200w" sizes="(max-width: 30px) 100vw, 30px" /></h2>



<p><a href="https://www.acmicpc.net/problem/28702" target="_blank" rel="noreferrer noopener">https://www.acmicpc.net/problem/28702</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">0.5 초</td><td class="has-text-align-left" data-align="left">1024 MB</td><td class="has-text-align-left" data-align="left">13102</td><td class="has-text-align-left" data-align="left">8022</td><td class="has-text-align-left" data-align="left">7260</td><td class="has-text-align-left" data-align="left">61.593%</td></tr></tbody></table></figure>



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



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



<p>FizzBuzz 문제는 i=1,2,⋯ 에 대해 다음 규칙에 따라 문자열을 한 줄에 하나씩 출력하는 문제입니다.</p>



<ul class="wp-block-list">
<li> i가 3의 배수이면서 5의 배수이면 “<code>FizzBuzz</code>”를 출력합니다.</li>



<li> i가 3의 배수이지만 5의 배수가 아니면 “<code>Fizz</code>”를 출력합니다.</li>



<li> i가 3의 배수가 아니지만 5의 배수이면 “<code>Buzz</code>”를 출력합니다.</li>



<li> i가 3의 배수도 아니고 5의 배수도 아닌 경우 i를 그대로 출력합니다.</li>
</ul>



<p>FizzBuzz 문제에서 연속으로 출력된 세 개의 문자열이 주어집니다. </p>



<p>이때, 이 세 문자열 다음에 올 문자열은 무엇일까요?</p>



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



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



<p>FizzBuzz 문제에서 연속으로 출력된 세 개의 문자열이 한 줄에 하나씩 주어집니다. </p>



<p>각 문자열의 길이는 8 이하입니다. 입력이 항상 FizzBuzz 문제에서 연속으로 출력된 세 개의 문자열에 대응됨이 보장됩니다.</p>



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



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



<p>연속으로 출력된 세 개의 문자열 다음에 올 문자열을 출력하세요. </p>



<p>여러 문자열이 올 수 있는 경우, 아무거나 하나 출력하세요.</p>



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



<h3 class="wp-block-heading">예제 입력 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="">Fizz
Buzz
11</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">Fizz</pre>



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



<h3 class="wp-block-heading">예제 입력 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="">980803
980804
FizzBuzz</pre>



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



<h3 class="wp-block-heading">예제 출력 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="">980806</pre>



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



<h3 class="wp-block-heading">힌트</h3>



<p>FizzBuzz 문제의 i = 1 ,⋯, 20에 대한 출력은 다음과 같습니다.</p>



<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
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz</pre>



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



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



<p><a href="https://www.acmicpc.net/category/45" target="_blank" rel="noreferrer noopener">Contest</a> > <a href="https://www.acmicpc.net/category/859" target="_blank" rel="noreferrer noopener">solved.ac</a> > <a href="https://www.acmicpc.net/category/detail/3707" target="_blank" rel="noreferrer noopener">solved.ac Grand Arena #2</a> B번</p>



<ul class="wp-block-list">
<li>문제를 검수한 사람: <a href="https://www.acmicpc.net/user/ai4youej" target="_blank" rel="noreferrer noopener">ai4youej</a>, <a href="https://www.acmicpc.net/user/bnb2011" target="_blank" rel="noreferrer noopener">bnb2011</a>, <a href="https://www.acmicpc.net/user/chansol" target="_blank" rel="noreferrer noopener">chansol</a>, <a href="https://www.acmicpc.net/user/cologne" target="_blank" rel="noreferrer noopener">cologne</a>, <a href="https://www.acmicpc.net/user/cozyyg" target="_blank" rel="noreferrer noopener">cozyyg</a>, <a href="https://www.acmicpc.net/user/gs18115" target="_blank" rel="noreferrer noopener">gs18115</a>, <a href="https://www.acmicpc.net/user/havana723" target="_blank" rel="noreferrer noopener">havana723</a>, <a href="https://www.acmicpc.net/user/jh05013" target="_blank" rel="noreferrer noopener">jh05013</a>, <a href="https://www.acmicpc.net/user/moonrabbit2" target="_blank" rel="noreferrer noopener">moonrabbit2</a>, <a href="https://www.acmicpc.net/user/pichulia" target="_blank" rel="noreferrer noopener">pichulia</a>, <a href="https://www.acmicpc.net/user/shiftpsh" target="_blank" rel="noreferrer noopener">shiftpsh</a></li>



<li>문제를 만든 사람: <a href="https://www.acmicpc.net/user/cologne">cologne</a>, <a href="https://www.acmicpc.net/user/solvedac">solvedac</a></li>
</ul>



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



<h3 class="wp-block-heading">알고리즘 분류</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/158" 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)"/>



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



<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>
#include &lt;string>

using namespace std;

string Input[3];

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

    for (int i = 0; i &lt; 3; i++)
        cin >> Input[i];

    for (int i = 0; i &lt; 3; i++) {
        if (Input[i][0] == 'F' || Input[i][0] == 'B')
            continue;

        int Num = stoi(Input[i]) + 3 - i;
        if (Num % 15 == 0) cout &lt;&lt; "FizzBuzz";
        else if (Num % 5 == 0) cout &lt;&lt; "Buzz";
        else if (Num % 3 == 0) cout &lt;&lt; "Fizz";
        else cout &lt;&lt; Num;
        return 0;
    }

    cout &lt;&lt; "FizzBuzz";

    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="960" height="76" src="https://lycos7560.com/wp-content/uploads/2025/03/image-11.png" alt="" class="wp-image-39927" srcset="https://lycos7560.com/wp-content/uploads/2025/03/image-11.png 960w, https://lycos7560.com/wp-content/uploads/2025/03/image-11-300x24.png 300w, https://lycos7560.com/wp-content/uploads/2025/03/image-11-768x61.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>
<p>The post <a href="https://lycos7560.com/cpp/%eb%b0%b1%ec%a4%80-28702%eb%b2%88-fizzbuzz-c-baekjoon/39926/">백준 28702번 (FizzBuzz, 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-28702%eb%b2%88-fizzbuzz-c-baekjoon/39926/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
