Pagini recente » Cod sursa (job #2699584) | Cod sursa (job #119753) | Cod sursa (job #1101182) | Cod sursa (job #1476793) | Cod sursa (job #2966835)
#include <fstream>
using namespace std;
ifstream in("scmax.in");
ofstream out("scmax.out");
const int N = 1e5;
int n , x[N + 1] , lung[N + 1] , val_max_capat[N + 1];
int BS(int arr[] , int l , int value){
int st = 1 , dr = l , rez = 0;
while(st <= dr){
int mid = (st + dr) / 2;
if(arr[mid] < value){
rez = mid;
st = mid + 1;
}
else{
dr = mid - 1;
}
}
return rez;
}
void refac_sir(int poz_val , int lung_max , int value){
if(lung_max == 0){
return;
}
if(x[poz_val] < value){
if(lung[poz_val] == lung_max){
refac_sir(poz_val - 1 , lung_max - 1 , x[poz_val]);
out << x[poz_val] << ' ';
}
}
else{
refac_sir(poz_val - 1 , lung_max , value);
}
}
int main(){
in >> n;
for(int i = 1 ; i <= n ; i++){
in >> x[i];
}
int lungime_val_max = 0 , poz_sir_max = 0;
for(int i = 1 ; i <= n ; i++){
int j = BS(val_max_capat , lungime_val_max , x[i]);
if(j == lungime_val_max){
lungime_val_max++;
}
lung[i] = 1 + j;
val_max_capat[j + 1] = x[i]; //
if(lung[i] > lung[poz_sir_max]){
poz_sir_max = i;
}
}
out << lung[poz_sir_max] << '\n';
refac_sir(poz_sir_max , lung[poz_sir_max] , x[poz_sir_max] + 1);
}