이 튜토리얼에서는 최대 합 증가 부분 수열을 찾는 프로그램에 대해 논의할 것입니다.
이를 위해 N개의 정수를 포함하는 배열이 제공됩니다. 우리의 임무는 배열에서 요소를 선택하여 요소가 정렬된 순서로 되도록 최대 합계를 추가하는 것입니다.
예시
#include <bits/stdc++.h>
using namespace std;
//returning the maximum sum
int maxSumIS(int arr[], int n) {
int i, j, max = 0;
int msis[n];
for ( i = 0; i < n; i++ )
msis[i] = arr[i];
for ( i = 1; i < n; i++ )
for ( j = 0; j < i; j++ )
if (arr[i] > arr[j] &&
msis[i] < msis[j] + arr[i])
msis[i] = msis[j] + arr[i];
for ( i = 0; i < n; i++ )
if ( max < msis[i] )
max = msis[i];
return max;
}
int main() {
int arr[] = {1, 101, 2, 3, 100, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Sum of maximum sum increasing subsequence is "<<
maxSumIS( arr, n ) << endl;
return 0;
} 출력
Sum of maximum sum increasing subsequence is 106