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 test,i,a,tc = 0 ;
- char s[15];
- scanf("%d",&test);
- getchar();
- getchar();
- while(test--)
- {
- n = 0;
- while(gets(s))
- {
- int len = strlen(s);
- a=0;
- if(len==0)
- break;
- for(i=0;i<len;i++)
- a = a*10+(s[i]-48);
- sequence.push_back(a);
- n++;
- }
- int res = LisNlogN();
- if(tc) puts("");
- printf("Max hits: %d\n",res);
- findSequence(res);
- tc = 1;
- sequence.clear();
- LIS_sequence.clear();
- L.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment