Pagini recente » Cod sursa (job #751143) | Cod sursa (job #1874654) | Profil CocoTheCoconut42 | Cod sursa (job #1600969) | Cod sursa (job #2009936)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
class AIB
{
public:
AIB(int sz)
{
v = new int[sz + 1];
n = sz;
for(int i = 1; i <= n; ++i)
v[i] = 0;
}
AIB(int vec[], int sz)
{
v = new int[sz + 1];
n = sz;
for(int i = 1; i <= n; ++i)
v[i] = 0;
for(int i = 1; i <= n; ++i)
Update(i, vec[i]);
}
~AIB()
{
delete[] v;
}
void Update(int ind, int val)
{
while(ind <= n)
{
v[ind] = max(v[ind], val);
ind += ind & (-ind);
}
}
int Query(int dr)
{
int ret = 0;
while(dr >= 1)
{
ret = max(ret, v[dr]);
dr -= dr & (-dr);
}
return ret;
}
private:
int * v;
int n;
};
const int nMax = 100005;
int n;
int v[nMax], cpy[nMax], norm[nMax], dp[nMax], last[nMax], pos[nMax];
int rasp, raspPos;
void citire()
{
ifstream in("scmax.in");
in >> n;
for(int i = 1; i <= n; ++i)
in >> v[i];
in.close();
}
void rezolvare()
{
for(int i = 1; i <= n; ++i)
cpy[i] = norm[i] = v[i];
sort(norm + 1, norm + n + 1);
for(int i = 1; i <= n; ++i)
v[i] = lower_bound(norm + 1, norm + n + 1, v[i]) - norm;
AIB aib(n);
int mx;
for(int i = 1; i <= n; ++i)
{
mx = aib.Query(v[i] - 1);
dp[i] = 1 + mx;
pos[dp[i]] = i;
last[i] = pos[mx];
aib.Update(v[i], dp[i]);
if(dp[i] > rasp)
{
rasp = dp[i];
raspPos = i;
}
}
}
void afisare()
{
ofstream out("scmax.out");
out << rasp << "\n";
vector<int> ans;
int pos = raspPos;
for(int i = 1; i <= rasp; ++i)
{
ans.push_back(cpy[pos]);
pos = last[pos];
}
for(int i = ans.size() - 1; i >= 0; --i)
out << ans[i] << " ";
out.close();
}
int main()
{
citire();
rezolvare();
afisare();
return 0;
}