您的位置:首页 > 其它

<OJ_Sicily>LIS最长上升子序列

2016-04-25 16:01 309 查看
Description

A numeric sequence of ai is ordered if a1 <= a2 <= ... <= aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, the sequence
(1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All the longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence L with the minimum aL

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10^9 each, separated by spaces. (1 <= N <= 5000)
Output

Output must contain two integers - the length of the longest ordered subsequence of the given sequence L and the minimum aL
问题解释:求解最长上升子序列
输入:第一行表示序列的长度N,也就是序列元素的个数;第二行则是包含n个数的一组数(表示一个序列)
输出:最长升序子序列的长度,最小aL(对于多个具有相同长度的升序子序列来说,我们选择的是众多升序子序列最后一个数最小的序列,将最后一个数标记为aL)
解题思路:使用动态规划的算法实现求解

#include <iostream>
#include <string.h>
using namespace std;
long arr[5000];
long result[5000];
long minimumalL[5000];

void getLISArray(int &L, long &aL,int len){
result[0] = 1;
for(int i = 1 ; i < len; i++){ //逐个求出第0个索引到第i个索引这段序列的最长升序子序列的长度以及对应的aL
int maxid = 0;
long sum = 0;
bool change = false;
for(int j = i; j >= 0; j--){   // 从arr[0]~arr[i]中找出不大于arr[i]且最长子序列长度最大的值
if(arr[j] <= arr[i] && result[j] >= sum){
change = true;
maxid = j;
sum = result[j];
}
}
if (change) {
result[i] = result[maxid] + 1; //arr[0]~arr[i]的最长升序子序列的长度等于 arr[0]~arr[i-1]的最长升序子序列的长度 + 1
minimumalL[i] = arr[maxid];
}
else{
result[i] = 1;  // 说明没有比arr[i]小的数
minimumalL[i] = arr[i];
}
if (result[i] > L) {   //及时更新最值
L = result[i];
aL = arr[i];
}
else if(result[i] == L && aL > arr[i]){
aL = arr[i];
}
}
}

int main(int argc, const char * argv[]) {
int N;

while (cin >> N) {
int i = 0;
memset(arr, 0, 5000);
memset(result, 0, 5000);
memset(minimumalL, 0, 5000);
for(i = 0; i < N; i++){
cin >> arr[i];
}
int L = 0; long aL = 0;
getLISArray(L, aL,N);
cout << L << " " << aL << endl;
}
return 0;
}


后记:

这是一个动态规划的问题,每个大问题的求解都依赖于子问题的求解。

代码新手,欢迎各位交流分享,提出宝贵的建议和意见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: