Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int inf = 0x3f3f3f3f;
- struct data
- {
- int ojon,buddhi,idx;
- } sequence[1005];
- bool cmp(data l,data r)
- {
- return l.buddhi>r.buddhi;
- }
- vector<int>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].ojon)
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- I[low] = sequence[i].ojon;
- 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].idx);
- for(j = i-1 ; j>=0 ; j--)
- {
- if( sequence[j].ojon<sequence[i].ojon && (L[j] == L[i]-1) )
- {
- i = j;
- LIS_sequence.push_back(sequence[i].idx);
- }
- }
- for(i=maxLength-1; i>=0; i--)
- {
- printf("%d\n",LIS_sequence[i]);
- }
- return ;
- }
- int main()
- {
- int i,j=0;
- while(scanf("%d%d",&sequence[j].ojon,&sequence[j].buddhi)==2)
- {
- sequence[j].idx = j+1;
- j++;
- }
- sort(sequence,sequence+j+1,cmp);
- n = j;
- int res = LisNlogN();
- printf("%d\n",res);
- findSequence(res);
- LIS_sequence.clear();
- L.clear();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment