Pagini recente » Cod sursa (job #2082379) | Cod sursa (job #2553338) | Cod sursa (job #1131238) | Cod sursa (job #2432469) | Cod sursa (job #2225048)
#include <bits/stdc++.h>
using namespace std;
int maximum [100005];
int before [100005];
int v[100005];
int dp[100005];
int reversed[100005];
map <int, int> normalization;
int mostUnsiginficantBit (int x) {
return x & (-x);
}
void updateValue (int n, int position, int value) {
while (position <= n) {
if (dp[maximum[position]] < dp[value]) {
maximum[position] = value;
}
position += mostUnsiginficantBit(position);
}
}
int getMaximum (int position) {
int current = 0;
while (position) {
if (dp[current] < dp[maximum[position]]) {
current = maximum[position];
}
position -= mostUnsiginficantBit(position);
}
return current;
}
int main() {
// ifstream fin ("input");
// ofstream fout ("output");
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
int n;
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> v[i];
normalization[v[i]] += 1;
}
int currentValue = 1;
for (auto &x : normalization) {
x.second = currentValue;
reversed[currentValue] = x.first;
currentValue += 1;
}
int theBest = 0;
for (int i = 1; i <= n; ++i) {
v[i] = normalization[v[i]];
int bestPosition = getMaximum(v[i] - 1);
dp[i] = dp[bestPosition] + 1;
before[i] = bestPosition;
if (dp[theBest] <= dp[i]) {
theBest = i;
}
updateValue(n, v[i], i);
}
fout << dp[theBest] << '\n';
vector <int> answer;
while (theBest) {
answer.push_back(reversed[v[theBest]]);
theBest = before[theBest];
}
reverse (answer.begin(), answer.end());
for (auto x : answer) {
fout << x << ' ';
}
return 0;
}