fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n; // declare n to be a number
  6. cin>>n; // read n from the input
  7. // declare "books" to be a sequence of numbers of length n:
  8. vector<int> books(n);
  9. for(int i=0;i<n;i=i+1)
  10. cin>>books[i]; // read books from the input
  11. // declare current to be a sequence of numbers of length n+1;
  12. // initially, all elements are 0
  13. vector<int> current(n,0);
  14. int result=1; // declare result to be a number which is initially 1
  15. for(int i=0;i<n;i=i+1){ // for each i from 0 to n in turn
  16. result=max(result,current[i]);
  17. for(int j=0;j<i;j=j+1){ // for each j from 0 to i-1 in turn
  18. // if the number of the book at position i is larger
  19. // than the number of the book at position j:
  20. if(books[i]>books[j]){
  21. // if current at position [i] is smaller
  22. // than current at position j
  23. if(current[i]<current[j]+1){
  24. // update current at position i to current at position j:
  25. current[i]=current[j]+1;
  26. }
  27. }
  28. }
  29. }
  30. cout<<n-result<<'\n'; // write n-result to the output
  31. }
  32.  
Success #stdin #stdout 0s 3460KB
stdin
7
3 1 2 6 5 7 4
stdout
6