Cod sursa(job #1525150)

Utilizator kassay_akosKassay Akos kassay_akos Data 14 noiembrie 2015 19:38:56
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define Infinity 30000
typedef int Vector[10001];

Vector V, P, Q, *S;
int N,Len; // Len = lungimea vectorului Q

void ReadData(void) {
    freopen("scmax.in","r",stdin);
    int i ;
    scanf("%d",&N);
    for(i=1;i<=N;i++)
        scanf("%d",&V[i]);
    fclose(stdin);
}

int Insert(int K, int Lo, int Hi) {
    int mid = (Lo+Hi)/2;
    if (Lo==Hi) {
        if (Hi>Len) Q[(++Len+1)]=Infinity;
        Q[Lo]=K;
        return Lo;
    }
    if (K<=Q[mid])   return Insert(K,Lo,mid);
    else            return Insert(K,mid+1,Hi);
}

void BuildPQ(void) {
    Len=0;
    Q[1]=Infinity;
    for(int i=1; i<=N;i++) {
        P[i]=Insert(V[i],1,Len+1);
    }
}

void BuildS(void){
    int i,K=N;
    for (i=Len;i;i--) {
        while (P[K]!=i) K-- ;
        Q[i] = V[K];                // i use the Q since i have no use for him but i should have use the S-solution
    }
}

void WriteSolution(void) {
    freopen("scmax.out","w",stdout);
    int i;

    printf("%d\n",Len);
    for(i=1;i<=Len; i++)
        printf("%d ",Q[i]);
    fclose(stdout);
}

int main()
{
    ReadData();
    BuildPQ();
    BuildS();
    WriteSolution();
    return 0;
}