기본 콘텐츠로 건너뛰기

2751 : 수 정렬하기 2 (TopDownMerge) [C++]

#include <iostream>

using namespace std;

void TopDownMergeSort(int A[], int B[], int n);
void TopDownSplitMerge(int B[], int iBegin, int iEnd, int A[]);
void TopDownMerge(int A[], int iBegin, int iMiddle, int iEnd, int B[]);
void CopyArray(int A[], int iBegin, int iEnd, int B[]);

int main()
{
 cin.tie(NULL);

 int N;
 cin >> N;

 int* X = new int[N];

 for (int i = 0; i < N; i++)
  cin >> X[i];

 int* Y = new int[N];

 TopDownMergeSort(X, Y, N);

 for (int i = 0; i < N; i++)
  cout << X[i] << "\n";
}


// Array A[] has the items to sort; array B[] is a work array.
void TopDownMergeSort(int A[], int B[], int n)
{
 CopyArray(A, 0, n, B);           // duplicate array A[] into B[]
 TopDownSplitMerge(B, 0, n, A);   // sort data from B[] into A[]
}

// Sort the given run of array A[] using array B[] as a source.
// iBegin is inclusive; iEnd is exclusive (A[iEnd] is not in the set).
void TopDownSplitMerge(int B[], int iBegin, int iEnd, int A[])
{
 if (iEnd - iBegin < 2)                       // if run size == 1
  return;                                 //   consider it sorted
            // split the run longer than 1 item into halves
 int iMiddle = (iEnd + iBegin) / 2;              // iMiddle = mid point
            // recursively sort both runs from array A[] into B[]
 TopDownSplitMerge(A, iBegin, iMiddle, B);  // sort the left  run
 TopDownSplitMerge(A, iMiddle, iEnd, B);  // sort the right run
            // merge the resulting runs from array B[] into A[]
 TopDownMerge(A, iBegin, iMiddle, iEnd, B);
}

//  Left source half is A[ iBegin:iMiddle-1].
// Right source half is A[iMiddle:iEnd-1   ].
// Result is            B[ iBegin:iEnd-1   ].
void TopDownMerge(int A[], int iBegin, int iMiddle, int iEnd, int B[])
{
 int i = iBegin, j = iMiddle;

 // While there are elements in the left or right runs...
 for (int k = iBegin; k < iEnd; k++) {
  // If left run head exists and is <= existing right run head.
  if (i < iMiddle && (j >= iEnd || A[i] <= A[j])) {
   B[k] = A[i];
   i = i + 1;
  }
  else {
   B[k] = A[j];
   j = j + 1;
  }
 }
}

void CopyArray(int A[], int iBegin, int iEnd, int B[])
{
 for (int k = iBegin; k < iEnd; k++)
  B[k] = A[k];
}

MergeSort는 https://ko.wikipedia.org/wiki/%ED%95%A9%EB%B3%91_%EC%A0%95%EB%A0%AC 에서 참조했다.

댓글

이 블로그의 인기 게시물

6359 : 만취한 상범 (Dynamic Programming) [C++]

# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...

11004 : K번째 수 [C++]

# include < iostream > # include < cstdio > # include < algorithm > int main ( ) { int * Number = new int [ 5000000 ] ; int N , K ; scanf ( " %d %d " , & N , & K ) ; for ( int i = 0 ; i < N ; i + + ) scanf ( " %d " , Number [ i ] ) ; std :: sort ( Number , Number + N ) ; printf ( " %d " , Number [ K - 1 ] ) ; return 0 ; }

1149 : RGB Street Coloring (Dynamic Programming) [C,C++]

The key to this problem lies in understanding the principles. Let me explain the algorithm to solve the problem by using DP. First, you need the same storage space like input data's size. When you draw any color of the nth house, the space will contain the minimum value. If you paint the red in the second house, this value is sum of blue or green of the first house.  You must use DP because you must use the previous value.  Of course, you can also use the recursive algorithm to solve it. But if it gets bigger, it will take a lot of time.  If you paint the red in the nth house in the same way, you should add the lower value of the blue and green of the n-1th house.  Therefore, the minimum value can be found in the value of the storage space (n-1) index. <pesudo code> *Source of the problem =  https://www.acmicpc.net/problem/1149 *문제 출처 : BAEKJOON ONLINE JUDGE