Pabon_SEC

Is Bigger Smarter

Feb 22nd, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int inf = 0x3f3f3f3f;
  6.  
  7. struct data
  8. {
  9.     int ojon,buddhi,idx;
  10.  
  11. } sequence[1005];
  12.  
  13. bool cmp(data l,data r)
  14. {
  15.     return l.buddhi>r.buddhi;
  16. }
  17.  
  18. vector<int>LIS_sequence,L;
  19.  
  20. int n;
  21.  
  22. int LisNlogN()
  23. {
  24.     int i;
  25.  
  26.     int I[n+1];
  27.  
  28.     I[0] = -inf;
  29.  
  30.     for(i=1; i<n; i++)
  31.     {
  32.         I[i] = inf;
  33.     }
  34.  
  35.     int Lislength = 0;
  36.  
  37.     for(i=0; i<n; i++)
  38.     {
  39.         int low,high,mid;
  40.  
  41.         low = 0;
  42.  
  43.         high = Lislength;
  44.  
  45.         while(low<=high)
  46.         {
  47.             mid = (low+high)/2;
  48.  
  49.             if(I[mid]<sequence[i].ojon)
  50.             {
  51.                 low = mid + 1;
  52.             }
  53.             else
  54.             {
  55.                 high = mid - 1;
  56.             }
  57.         }
  58.  
  59.         I[low] = sequence[i].ojon;
  60.  
  61.         L.push_back(low);
  62.  
  63.         if(Lislength<low)
  64.         {
  65.             Lislength = low;
  66.         }
  67.     }
  68.  
  69.     return Lislength;
  70. }
  71.  
  72. void findSequence(int maxLength)
  73. {
  74.     int i,j;
  75.  
  76.     i = 0;
  77.  
  78.     for(j=1; j<n; j++)
  79.     {
  80.         if(L[j]>L[i])
  81.         {
  82.             i = j;
  83.         }
  84.     }
  85.  
  86.     int top = L[i] - 1;
  87.  
  88.     LIS_sequence.push_back(sequence[i].idx);
  89.  
  90.     for(j = i-1 ; j>=0 ; j--)
  91.     {
  92.         if( sequence[j].ojon<sequence[i].ojon && (L[j] == L[i]-1) )
  93.         {
  94.             i = j;
  95.  
  96.             LIS_sequence.push_back(sequence[i].idx);
  97.  
  98.         }
  99.     }
  100.  
  101.     for(i=maxLength-1; i>=0; i--)
  102.     {
  103.         printf("%d\n",LIS_sequence[i]);
  104.     }
  105.  
  106.     return ;
  107. }
  108.  
  109. int main()
  110. {
  111.     int i,j=0;
  112.  
  113.     while(scanf("%d%d",&sequence[j].ojon,&sequence[j].buddhi)==2)
  114.     {
  115.         sequence[j].idx = j+1;
  116.  
  117.         j++;
  118.     }
  119.  
  120.     sort(sequence,sequence+j+1,cmp);
  121.  
  122.     n = j;
  123.  
  124.     int res = LisNlogN();
  125.  
  126.     printf("%d\n",res);
  127.  
  128.     findSequence(res);
  129.  
  130.     LIS_sequence.clear();
  131.  
  132.     L.clear();
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment