Cod sursa(job #798964)

Utilizator andreitaleanuAndrei Taleanu andreitaleanu Data 17 octombrie 2012 17:29:58
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <stdio.h>
#include <stdlib.h>

#define MAX 100000

int scsm(int *v, int n, int *poz)
{
     int i, max, p, best[MAX];
     
     best[0] = 1;
     for (p = -1, max = 0, i = 1; i < n; ++i)
     {   if (v[i] < v[i-1])
             best[i] = 1;
         else if (v[i] == v[i-1])
         {
             best[i] = best[i-1]; 
             if (max < best[i])
             {   max = best[i];  
                 p = i;
             }
          }
         else
         {   best[i] = best[i-1] + 1;
             if (max < best[i])
             {   max = best[i];  
                 p = i;
             }
         }
     }
     *poz = p;
     return max;
}

int main()
{
    FILE *f, *g;
    int n, v[MAX], i, max, p;
    
    f = fopen("scmax.in", "r");
    
    fscanf(f, "%i", &n);
    for (i = 0; i < n; ++i)
        fscanf(f, "%i", v+i);
    
//    for (i = 0; i < n; i++)
//        printf("v[%i] = %i\n", i, v[i]);
    
    g = fopen("scmax.out", "w");
    max = scsm(v, n, &p);
    fprintf(g, "%i\n", max);
    for (i = p - max + 1; max > 0; i++, max--)
        fprintf(g, "%i ", v[i]);
    
    return 0;
}