Description
Input
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.
Output
For each set of data the program prints the result to the standard output from the beginning of a line.
Sample Input
6 5 2 1 4 5 3 3 1 1 1 4 4 3 2 1
Sample Output
3 1 1
Hint
There are three data sets. In the first case, the length L of the sequence is 6. The sequence is 5, 2, 1, 4, 5, 3. The result for the data set is the length of the longest rising trend: 3.
#include<cstdio>
using namespace std;
int const size=100010;
int stack[size];
int main()
{
int L;
int top,temp;
while(scanf("%d",&L)!=EOF)
{
top=0;
stack[0]=-1;//第一個數可能爲0
for(int i=0;i<L;i++)
{
scanf("%d",&temp);
if(temp>stack[top])//比棧頂大的元素就入棧
{
stack[++top]=temp;
}
else
{
int left=1,right=top;
int mid;
while(left<=right)//二分檢索棧中第一個比temp大的元素
{
mid=(left+right)/2;
if(temp>stack[mid])
left=mid+1;
else
right=mid-1;
}
stack[left]=temp;
}
}
printf("%d\n",top);
}
return 0;
}