Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int inf = 0x3f3f3f3f;
- vector<int>sequence,LIS_sequence,L;
- int n;
- int LisNlogN()
- {
- int i;
- int I[n+1];
- I[0] = -inf;
- for(i=1; i<=n; i++)
- {
- I[i] = inf;
- }
- int Lislength = 0;
- for(i=0; i<n; i++)
- {
- int low,high,mid;
- low = 0;
- high = Lislength;
- while(low<=high)
- {
- mid = (low+high)/2;
- if(I[mid]<sequence[i])
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- I[low] = sequence[i];
- L.push_back(low);
- if(Lislength<low)
- {
- Lislength = low;
- }
- }
- return Lislength;
- }
- void findSequence(int maxLength)
- {
- int i,j;
- i = 0;
- for(j=1; j<n; j++)
- {
- if(L[j]>=L[i])
- {
- i = j;
- }
- }
- int top = L[i] - 1;
- LIS_sequence.push_back(sequence[i]);
- for(j = i-1 ; j>=0 ; j--)
- {
- if( sequence[j]<sequence[i] && (L[j] == L[i]-1) )
- {
- i = j;
- LIS_sequence.push_back(sequence[i]);
- }
- }
- for(i=maxLength-1; i>=0; i--)
- {
- printf("%d\n",LIS_sequence[i]);
- }
- return ;
- }
- int main()
- {
- int i,a;
- while(scanf("%d",&a)!=EOF)
- {
- sequence.push_back(a);
- }
- n = sequence.size();
- int res = LisNlogN();
- printf("%d\n-\n",res);
- findSequence(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment