█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 1242

Problema 0xf2 - Partir un arreglo grande en 2

  • Autor: EduardoVega
  • Fecha: 2020-10-12 09:24:51 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.003 s 1 KBi
#2
Correcto
0.003 s 1 KBi
#3
Correcto
0.004 s 1 KBi
#4
Correcto
0.004 s 1 KBi
#5
Correcto
0.002 s 1 KBi
#6
Correcto
0.003 s 1 KBi
#7
Correcto
0.002 s 1 KBi
#8
Correcto
0.003 s 1 KBi
#9
Correcto
0.004 s 1 KBi
#10
Correcto
0.003 s 1 KBi
#11
Correcto
0.004 s 1 KBi
#12
Correcto
0.003 s 1 KBi
#13
Correcto
0.003 s 1 KBi
#14
Correcto
0.097 s 8 KBi
#15
Correcto
0.05 s 8 KBi
#16
Correcto
0.076 s 9 KBi
#17
Correcto
0.048 s 9 KBi
#18
Correcto
0.06 s 9 KBi
#19
Correcto
0.058 s 9 KBi
#20
Correcto
0.054 s 9 KBi
Puntos totales: 100 / 100

Código

/* Code by Eduardo Vega */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  long size = 0, num = 0, i;
  long *array = NULL;
  long *arr_aux = NULL;

  /* Get size. Dinamically allocate two arrays */
  scanf("%li", &size);
  array = malloc(sizeof(long int) * size);  
  arr_aux = malloc(sizeof(long int) * size);

  /* Fill both arrays with the input numbers */
  for (i = 0; i < size; i++)
  {
    scanf("%li", &num);    
    array[i] = num;
    if (i == 0 || i == size - 1)      
        arr_aux[i] = num;    
  }

  /* We want to know the sum of the right numbers 
     and the left numbers at any index in real 
     time. */
  /* This is more efficient than traversing 
     the whole array to know the right and left 
     sums every time we move to a specific 
     index */
  /* To do that, we create a second array. */
  /* This array will contain the sum of every 
     value in original array with its right 
     value, starting at position [size - 1] until 
     position [1] */
  
  for (i = size - 2; i > 0; i--)
      arr_aux[i] = arr_aux[i + 1] + array[i];

  /* Iterate and compare left and right sums in 
     the second array */
  for(i = 1; i < size; i++)
  {
    if (arr_aux[i] < 0 && arr_aux[i - 1] > 0)
    {
      printf("%li\n", i);
      free(array);
      return 0;
    }
    /* Update left sum if moving to the right */
    arr_aux[i] = arr_aux[i - 1] + array[i];    
  }

  printf("Impossible\n");
  free(array);
  return 0;
}