Pagini recente » Cod sursa (job #711479) | Cod sursa (job #1325427) | Cod sursa (job #908314) | Cod sursa (job #3238284) | Cod sursa (job #3037773)
#include <fstream>
#include <map>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int DIM = 100010;
int n, len;
int v[DIM], dp[DIM], pos[DIM], sol[DIM];
map<int, int> father;
int binarySearch(int val) {
int left = 1, right = len;
while (left <= right) {
int middle = (left + right) / 2;
if (dp[middle] < val)
left = middle + 1;
else
right = middle - 1;
}
return left;
}
void getPath() {
int j = n;
for (int i = len; i >= 1; i--) {
while (pos[j] != i)
j--;
sol[i] = v[j];
}
for (int i = 1; i <= len; i++)
fout << sol[i] << ' ';
}
int main() {
fin >> n;
for (int i = 1; i <= n; i++)
fin >> v[i];
for (int i = 1; i <= n; i++) {
if (v[i] > dp[len]) {
dp[++len] = v[i];
pos[i] = len;
} else {
int currPos = binarySearch(v[i]);
dp[currPos] = v[i];
pos[i] = currPos;
}
}
fout << len << '\n';
getPath();
return 0;
}