Pagini recente » Cod sursa (job #1803656) | Cod sursa (job #2010279) | Cod sursa (job #2879876) | Istoria paginii utilizator/madalina.popescu | Cod sursa (job #3215032)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
vector<int> ans;
const int Nmax = 100005;
int n,i,j,a[Nmax];
void scmax()
{
//In ans mereu pastram cel mai optim de pana acum, cu cele mai
//mici numere, ca sa fie loc de cat mai multe dupa
ans.push_back(a[1]);
for (int i = 2; i <= n; i++) {
if (a[i] > ans.back()) {
// If the current number is greater
// than the last element of the answer
// vector, it means we have found a
// longer increasing subsequence.
// Hence, we append the current number
// to the answer vector.
ans.push_back(a[i]);
}
else {
// If the current number is not
// greater than the last element of
// the answer vector, we perform
// a binary search to find the smallest
// element in the answer vector that
// is greater than or equal to the
// current number.
// The lower_bound function returns
// an iterator pointing to the first
// element that is not less than
// the current number.
int low = lower_bound(ans.begin(), ans.end(),
a[i]) - ans.begin();
// We update the element at the
// found position with the current number.
// By doing this, we are maintaining
// a sorted order in the answer vector.
ans[low] = a[i];
}
}
// The length of the answer vector
// represents the length of the
// longest increasing subsequence.
}
// Driver program to test above function
int main()
{
fin>>n;
for(i=1; i<=n; i++)fin>>a[i];
scmax();
fout<<ans.size()<<'\n';
for(i=0; i<ans.size(); i++)
fout<<ans[i]<<' ';
return 0;
}