nasarouf

hackerrank.com/challenges/modify-the-sequence

Mar 9th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. #define MAX 1000001
  12. int x[MAX+1],n;
  13.  
  14. int LISlast[MAX+1];
  15. int LNDS(int *x, int n){
  16.     int l=0,*p;
  17.     LISlast[0]=-1;
  18.     for (int i=0; i<n; i++){
  19.  
  20.     if (x[i]<=0) continue;   // ONLY POSITIVE NUMBERS ALLOWED
  21.  
  22.         p=upper_bound(LISlast,LISlast+l,x[i]);
  23.         if (p==LISlast+l) ++l;
  24.         *p=x[i];
  25.     }
  26.     return l;
  27. }
  28.  
  29. int main() {
  30.     cin>>n;
  31.     for (int i=0; i<n; i++){
  32.         cin>>x[i]; x[i]-=i;
  33.     }
  34.     cout<<n-LNDS(x,n)<<endl;
  35.     return 0;
  36. }
Add Comment
Please, Sign In to add comment