It is necessary from a given numerical sequence A of length n to cross out the minimum number of elements so that the remaining elements form a strictly increasing almost everywhere subsequence, i.e. containing no more than one discontinuity - pairs (ai, ai + 1) of successive elements, the second of which more than the first. The constructed algorithm must have O (n log n) labor input. In the output file to display the length of the greatest sequence. Here is my code, but I don’t take into account quite a few options here, as it turned out. Tell me, please, the solution algorithm
#include<iostream> #include<fstream> #include<algorithm> #include<vector> #include<iomanip> using namespace std; int const INF=2000000000; int main() { ifstream fin("input.txt"); ofstream fout("output.txt"); int n; fin>>n; vector<int>A; int i; int N=0; while(fin>>i) { A.push_back(i); N++; } vector<int>d(N); d[0] = -INF; for(int i=1;i<N;i++) { d[i]=INF; } int len; for (int i=0; i<n; i++) { int j = int (upper_bound (d.begin(), d.end(), A[i]) - d.begin()); if (d[j-1] < A[i] && A[i] < d[j]) { d[j] = A[i]; len=j; } } if(len==n) { fout<<len; } else fout<<len+1; return 0; } 