最长连续递增子序列
2017-07-02 14:29
387 查看
习题3.4 最长连续递增子序列 (20分)
给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
10^5≤105);第2行给出nn个整数,其间以空格分隔。
#include <stdio.h>
#define N 100
int main(){
int i,n,from=0,to=0,len=0,tfrom=0,tto=0,tlen=0,Data
;
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &Data[i]);
for(i=0;i<n;i++){
if(Data[i]<Data[i+1]){
tto++;
}else{
tlen=tto-tfrom+1;
printf("tfrom = %d, tto = %d, tlen = %d\n", tfrom, tto, tlen);
if(tlen>len){
from=tfrom;
to=tto;
len=to-from+1;
printf("from = %d, to = %d, len = %d\n", from, to, len);
tfrom=tto=i+1;
}else{
tfrom=tto=i+1;
}
}
}
for(i=from;i<=to;i++)
printf("%d ", Data[i]);
printf("\n");
return 0;
}
给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
输入格式:
输入第1行给出正整数nn(\le10^5≤105);第2行给出nn个整数,其间以空格分隔。
输出格式:
在一行中输出第一次出现的最长连续递增子序列,数字之间用空格分隔,序列结尾不能有多余空格。输入样例:
15 1 9 2 5 7 3 4 6 8 0 11 15 17 17 10
输出样例:
3 4 6 8
#include <stdio.h>
#define N 100
int main(){
int i,n,from=0,to=0,len=0,tfrom=0,tto=0,tlen=0,Data
;
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d", &Data[i]);
for(i=0;i<n;i++){
if(Data[i]<Data[i+1]){
tto++;
}else{
tlen=tto-tfrom+1;
printf("tfrom = %d, tto = %d, tlen = %d\n", tfrom, tto, tlen);
if(tlen>len){
from=tfrom;
to=tto;
len=to-from+1;
printf("from = %d, to = %d, len = %d\n", from, to, len);
tfrom=tto=i+1;
}else{
tfrom=tto=i+1;
}
}
}
for(i=from;i<=to;i++)
printf("%d ", Data[i]);
printf("\n");
return 0;
}
相关文章推荐
- 最长连续递增子序列
- 矩阵中最长连续递增子序列
- HDU 3308——LCIS(线段树,区间合并,最长连续递增子序列)
- 湖北民族学院oj 1668 之 最长连续递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- 用二分法寻找最长连续单调递增子序列
- 【EPI-6.6】最长连续递增子序列-启发式搜索
- 最长单调"连续"递增子序列
- 最长连续递增子序列
- 最长连续递增子序列
- 线段树区间合并+最长连续递增子序列——HDU 3308
- 最长连续递增子序列
- 求数组中最长连续递增子序列
- 5-2 最长连续递增子序列
- 天梯赛2 重现5-4 最长连续递增子序列
- 51Nod - 1241 特殊的排序(求最长连续递增子序列)
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列