Pagini recente » Cod sursa (job #91191) | Cod sursa (job #2936312) | Cod sursa (job #682055) | Cod sursa (job #1902724) | Cod sursa (job #823572)
Cod sursa(job #823572)
#include<stdio.h>
#include<stdlib.h>
void inc(int* a,int n){
int c;
for (c = 0 ; c < n ; c++){
a[c] += 5;
}
}
void afisare(int *a,int n){
FILE *out;
out = fopen("scmax.out","w");
int c;
fprintf(out,"%d\n",n);
for (c = 0 ; c < n ; c++){
fprintf(out,"%d ",a[c]);
}
}
int binarySearch(int *a,int x,int first, int last){
if( first == last) {
if (a[first] >= x) return first;
else return (first + 1);
}
else{
int mid = (first + last)/ 2;
if ( a[mid] == x){
return mid;
}
else{
if (a[mid] > x ) return binarySearch(a,x,first,mid);
else return binarySearch(a,x,mid+1,last);
}
}
}
int createVec(int *c, int *a, int n){
int i = 0;
int poz = -1;
int length_b = 1;
int *b;
b = (int*) malloc(n*sizeof(int));
b[0] = a[0];
c[0] = 0;
for (i = 1 ; i < n ; i++){
poz = binarySearch(b,a[i],0,length_b -1);
c[i] = poz;
if ( poz == length_b ) {
b[length_b] = a[i];
length_b ++;}
else{
b[poz] = a[i];
}
}
return length_b;
}
int lastIndex(int *vec,int last,int look){
int i;
for (i = last ; i >= 0; i--){
if( vec[i] == look){
return i;
}
}
}
int main(int argc, char* argv[]){
int* a;
int* c;
int* d;
int i;
int n,x,y;
FILE *in;
in = fopen("scmax.in","r");
fscanf(in,"%d",&n);
a = (int*) malloc(n*sizeof(int));
c = (int*) malloc(n*sizeof(int));
for (i = 0 ; i < n; i++){
fscanf(in,"%d",&a[i]);
}
y = createVec(c,a,n);
d = (int*) malloc(y*sizeof(int));
int poz = n-1;
for (i = (y-1); i >= 0; i--){
poz = lastIndex(c,poz,i);
d[i] = a[poz];
}
afisare(d,y);
return 0;
}