1263: What Goes Up
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 896 | 219 | Standard |
Write a program that will select the longest strictly increasing subsequence from a sequence of integers.
Input
The input file will contain a sequence of integers (positive, negative, and/or zero). Each line of the input file will contain one integer.
Output
The output for this program will be a line indicating the length of the longest subsequence, a newline, a dash character ('-'), a newline, and then the subsequence itself printed with one integer per line. If the input contains more than one longest subsequence, the output file should print the one that occurs last in the input file.
Notice that the second 8 was not included -- the subsequence must be strictly increasing.
Sample Input
-7 10 9 2 3 8 8 6
Sample Output
4 - -7 2 3 6
#include<iostream>
using namespace std;
int main()
{
int n=0,number,a[10000],pre[10000]={-1},num[10000],dp[10000]={1};
while(scanf("%d",&number)==1) a[n++]=number;
for(int i=1;i<n;i++)
{
dp[i]=1;pre[i]=-1;
for(int j=0;j<i;j++)
{
if(a[j]<a[i]&&dp[i]<=dp[j]+1)//"="
{
dp[i]=dp[j]+1;
pre[i]=j;
}
}
}
int max=0,max_i=0;
for(int i=0;i<n;i++) if(dp[i]>=max) max=dp[i],max_i=i;//"="
int l=0;
int k=max_i;
while(k!=-1)
{
num[l++]=a[k];
k=pre[k];
}
printf("%d/n-/n",max);
for(int j=l-1;j>=0;j--) printf("%d/n",num[j]);
return 0;
}
本文介绍了一个编程问题的解决方案:从一组整数中找出最长的严格递增子序列。输入包含一系列整数,输出则是该序列的长度及具体数值。采用动态规划方法实现,通过示例说明了程序运行结果。
222

被折叠的 条评论
为什么被折叠?



