Pagini recente » Cod sursa (job #1444072) | Cod sursa (job #3124614) | Cod sursa (job #2634278) | Cod sursa (job #2649894) | Cod sursa (job #2944066)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, before[100005], BIT[100005];
vector<int> numbers;
int get_bit(int x)
{
return (x & (-x));
}
void update(int poz, int val)
{
for (; poz <= n; poz += get_bit(poz))
BIT[poz] = max(BIT[poz], val);
}
int query(int &poz)
{
const int reper = poz + 1;
int ans = 0, i;
for (i = poz; i > 0; i -= get_bit(i))
if (BIT[i] > ans && numbers[i] < numbers[reper])
{
ans = BIT[i];
poz = i;
}
return ans;
}
int main()
{
fin >> n;
for (int i = 1; i <= n; ++i)
{
int x;
fin >> x;
if (numbers.empty() || numbers.back() != x)
numbers.push_back(x);
}
n = numbers.size();
numbers.insert(numbers.begin(), 0);
numbers.insert(numbers.end(), INT32_MAX);
for (int i = 1; i <= n; ++i)
{
int poz = i;
if (poz == 1)
{
before[i] = -1;
update(i, 1);
continue;
}
poz = i - 1;
int ans = query(poz);
if (ans == 0)
before[i] = -1;
else
before[i] = poz;
update(i, ans + 1);
}
int ans = 0, poz = 0;
for (int i = 1; i <= n; ++i)
if (BIT[i] > ans)
{
ans = BIT[i];
poz = i;
}
fout << ans << '\n';
vector<int> sol;
while (poz != -1)
{
sol.push_back(numbers[poz]);
poz = before[poz];
}
for (int i = sol.size() - 1; i >= 0; --i)
fout << sol[i] << ' ';
return 0;
}