Pagini recente » Cod sursa (job #2284227) | Cod sursa (job #1399059) | Cod sursa (job #1933235) | Cod sursa (job #3178738) | Cod sursa (job #2115598)
#include<fstream>
#include<climits>
#include<stack>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int NMAX = 100005, INF = INT_MAX;
int LISLength[NMAX], LIS[NMAX], numbers[NMAX];
int numbersCount, length = 1;
inline void read_data(){
fin >> numbersCount;
int index;
for(index = 1; index <= numbersCount; ++index)
fin >> numbers[index];
}
inline int getBestPos(int value, int indexInLIS){
int left = 1, right = length, mid;
while(right - left){
mid = (left + right) >> 1;
if(LISLength[mid] < value)
left = mid + 1;
else
right = mid;
}
LISLength[left] = value;
if(left == length){
++length;
LISLength[length] = INF;
}
return left;
}
inline void getLIS(){
LISLength[1] = INF;
int index;
for(index = 1; index <= numbersCount; ++index)
LIS[index] = getBestPos(numbers[index], index);
--length;
}
inline void printLIS(){
fout << length << '\n';
stack<int> Stack;
int index;
for(index = numbersCount; index >= 1; --index)
if(LIS[index] == length){
Stack.push(numbers[index]);
--length;
}
while(!Stack.empty()){
fout << Stack.top() << ' ';
Stack.pop();
}
}
int main(){
read_data();
getLIS();
printLIS();
}