Pagini recente » Cod sursa (job #2875694) | Cod sursa (job #1664957) | Cod sursa (job #170805) | Cod sursa (job #2144538) | Cod sursa (job #1233995)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "scmax.in";
const char outfile[] = "scmax.out";
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
int N, v[MAXN], aib[MAXN], dp[MAXN], res[MAXN], k, last[MAXN];
vector <int> S;
inline int lsb(int x) {
return x & (-x);
}
inline void Update(int x, int ind) {
for(int i = x ; i <= N ; i += lsb(i))
if(dp[aib[i]] < dp[ind])
aib[i] = ind;
}
inline int Query(int x) {
int mx = 0;
for(int i = x ; i > 0 ; i -= lsb(i))
if(dp[mx] < dp[aib[i]])
mx = aib[i];
return mx;
}
inline void Write(int ind) {
if(last[ind])
Write(last[ind]);
fout << res[ind] << ' ';
}
class Scanner {
public:
Scanner(string file, int buffer_size = 32768):
buffer_size_(buffer_size) {
file_ = fopen(file.c_str(), "r");
buffer_ = new char[buffer_size];
pointer_ = buffer_ + buffer_size_;
}
Scanner& operator>>(int &object) {
object = 0;
while (next() < '0' or next() > '9')
advance();
while (next() >= '0' and next() <= '9') {
object = object * 10 + next() - '0';
advance();
}
return *this;
}
private:
char next() {
if (pointer_ == buffer_ + buffer_size_) {
pointer_ = buffer_;
fread(buffer_, 1, buffer_size_, file_);
}
return *pointer_;
}
void advance() {
++pointer_;
}
FILE *file_;
int buffer_size_;
char *buffer_, *pointer_;
};
Scanner fin(infile);
int main() {
fin >> N;
for(int i = 1 ; i <= N ; ++ i) {
fin >> v[i];
res[i] = v[i];
S.push_back(v[i]);
}
sort(S.begin(), S.end());
S.resize(unique(S.begin(), S.end()) - S.begin());
for(int i = 1 ; i <= N ; ++ i)
v[i] = 1 + lower_bound(S.begin(), S.end(), v[i]) - S.begin();
int indmax = 1;
for(int i = 1 ; i <= N ; ++ i) {
last[i] = Query(v[i] - 1);
dp[i] = dp[last[i]] + 1;
Update(v[i], i);
if(dp[indmax] < dp[i])
indmax = i;
}
fout << dp[indmax] << '\n';
Write(indmax);
fout.close();
return 0;
}