Cod sursa(job #500969)

Utilizator gvoicuVoicu Gabriel gvoicu Data 13 noiembrie 2010 21:54:12
Problema Subsecventa de suma maxima Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 3.91 kb
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

long int theta_nn(long int v[],long int n,long int *st,long int *dr)
{
   long int i,j, sum, sum_max=LONG_MIN;

   for(i=0;i<n-1;i++)
   {
      sum = 0;
      for(j=i; j<n; j++)
      {
         sum+=v[j];
         if(sum >= sum_max)
         {
            sum_max = sum;
            *st      = i;
            *dr      = j;
         }
      }
   }

   return sum_max;
}

typedef struct elemente
{
   long int sum;
   long int st;
   long int dr;
} elemente;

/*elemente theta_nlgn(long int v[], long int n, long int st, long int dr)
{
   elemente primul, al_doilea;

   if(dr-st == 1)
   {
      elemente element_sum;
      element_sum.st = st;
      element_sum.dr = dr;
      element_sum.sum = v[st]+v[dr];
      return element_sum;
   }
   if(st == dr)
   {
      elemente element_sum;
      element_sum.st = st;
      element_sum.dr = dr;
      element_sum.sum = v[st];
      return element_sum;
   }

   long int m = (st+dr) >> 1;

   primul    = theta_nlgn(v, n, st, m);
   al_doilea = theta_nlgn(v, n, m+1, dr);

   if((primul.sum + al_doilea.sum >= primul.sum) && (primul.sum + al_doilea.sum >= al_doilea.sum)
                    && (al_doilea.st - primul.dr == 1 ))
   {
     elemente element_suma;
     element_suma.st = primul.st;
     element_suma.dr = al_doilea.dr;
     element_suma.sum = primul.sum + al_doilea.sum;
     return element_suma;
   }
   else if(primul.sum > al_doilea.sum)
   {
      return primul;
   }
   else
   {
      return al_doilea;
   }
}*/

elemente theta_nlgn(long int v[], long int n, long int st,long int dr)
{
   long int i;
   elemente best_element, aux_element;
   best_element.sum = LONG_MIN;
   if(st == dr)
   {
      best_element.st  = st;
      best_element.dr  = dr;
      best_element.sum = v[st];
      return best_element;
   }
   long int m = (st+dr) >> 1;
   aux_element = theta_nlgn(v,n,st,m);
   if(aux_element.sum >= best_element.sum)
   {
      best_element.sum = aux_element.sum;
      best_element.st  = aux_element.st;
      best_element.dr  = aux_element.dr;
   }
   aux_element = theta_nlgn(v,n, m+1, dr);
   if(aux_element.sum >= best_element.sum)
   {
      best_element.sum = aux_element.sum;
      best_element.st  = aux_element.st;
      best_element.dr  = aux_element.dr;
   }
   long int sum_l = LONG_MIN, sum_r = LONG_MIN, sum = 0, st_aux, dr_aux;
   for(i = m; i >= st; i--)
   {
      sum += v[i];
      if(sum_l <= sum)
      {
          sum_l  = sum;
          st_aux = i;
      }
   }
   sum=0;
   for(i = m+1; i<=dr; i++)
   {
      sum += v[i];
      if(sum_r <= sum)
      {
         sum_r = sum;
         dr_aux = i;
      }
   }
   if(sum_r + sum_l >= best_element.sum)
   {
      best_element.sum = sum_r + sum_l;
      best_element.st = st_aux;
      best_element.dr = dr_aux;
      return best_element;
   }
   else
      return best_element;
}

elemente theta_n(long int v[],long int n)
{
   long int sum = 0, i;
   elemente best_element;
   best_element.st = 0;
   best_element.sum = LONG_MIN;

   for(i=0; i<n; i++)
   {
      if(sum < 0)
      {
         sum = v[i];
         best_element.st = i;
         best_element.dr = i;
      }
      else
      {
         sum+=v[i];
      }

      if(best_element.sum < sum)
      {
         best_element.sum = sum;
         best_element.dr = i;
      }
   }
   return best_element;
}

int main()
{
   long int i,n=0, *v;//, sum=0, st=0, dr=0;
   FILE* in  = fopen("ssm.in", "r");
   FILE* out = fopen("ssm.out", "w");

   fscanf(in,"%ld", &n);
   v = (long int*) malloc((sizeof(long int))*(n+1));
   for(i=0; i<n; i++)
   {
      fscanf(in, "%ld", &v[i]);
   }
   //sum = theta_nn(v, n, &st, &dr);
   //fprintf(stdout, "%ld %ld %ld", sum, st+1, dr+1);

   //elemente rez = theta_nlgn(v,n,0,n-1);
   //fprintf(out, "%ld %ld %ld", rez.sum, rez.st+1, rez.dr+1);

   elemente best = theta_n(v,n);
   fprintf(stdout, "%ld %ld %ld", best.sum, best.st+1, best.dr+1);

   fclose(in);
   fclose(out);

   return 0;
}