Pagini recente » Cod sursa (job #1141350) | Cod sursa (job #1488853) | Cod sursa (job #2421138) | Cod sursa (job #1180033) | Cod sursa (job #1331609)
//
// main.cpp
// scmax
//
// Created by Ali Cerrahoglu on 30/01/15.
// Copyright (c) 2015 Ali Cerrahoglu. All rights reserved.
//
#include <iostream>
#include <fstream>
using namespace std;
const int N = 100005;
int x[N] = {0},
y[N],
a[N],
previ[N] = {0};
ofstream g("scmax.out");
void binarySearch( int val, int &endd, int currIndex ) {
int first = 1,
last = endd,
midd;
while( true ) {
midd = (last+first) / 2;
if( val > a[x[midd]] ) {
first = midd;
}
else {
last = midd;
}
if( last-first <= 1 ) {
if( val > a[x[first]] ) {
if( val < a[x[last]] ) {
x[last] = currIndex;
previ[currIndex] = x[first];
}
else if( val > a[x[last]] ) {
// this means last = end;
previ[currIndex] = x[endd];
x[++endd] = currIndex;
}
}
else if( val < a[x[first]] ) {
// this means that first hasn't changed from the beggining of this function
x[first] = currIndex;
previ[currIndex] = 0;
}
break;
}
}
}
void Print(int i){
if (previ[i] != 0){
Print(previ[i]);
g << a[previ[i]] << " ";
}
}
int main() {
int n,i; int endd=1;
ifstream f("scmax.in");
f >> n;
for ( i=1; i<=n; i++ ) {
f >> a[i];
}
x[1] = 1;
for( i=2; i<=n; i++ ) {
binarySearch( a[i], endd, i );
}
g << endd << "\n";
Print(x[endd]);
g << a[x[endd]];
return 0;
}