daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  4. #define MIN(x, y) (((x) > (y)) ? (y) : (x))
  5.  
  6. int main()
  7. {
  8.     int64_t a, data;
  9.     scanf("%lld", &a);
  10.     int64_t tab[a];
  11.     int64_t dist_forward[a];
  12.     int64_t dist_backward[a];
  13.     int64_t result[a -1];
  14.  
  15.     for(int64_t i = 0; i < a; i++)
  16.     {
  17.         scanf("%lld", &data);
  18.         tab[i] = data;
  19.     }
  20.  
  21.     int64_t sum = 0;
  22.     dist_forward[0] = 0;
  23.     for(int64_t i = 0; i < a; i++)
  24.     {
  25.         if (i == 0) {
  26.             dist_forward[i] =0;
  27.             continue;
  28.         }
  29.         sum += tab[i - 1];
  30.         dist_forward[i] = sum;
  31.     }
  32.  
  33.     sum = 0;
  34.     dist_backward[0] = 0;
  35.     for(int64_t i = a-1; i > 0; i--)
  36.     {
  37.         sum += tab[i];
  38.         dist_backward[i] = sum;
  39.     }
  40.    
  41.     int64_t x = 0;
  42.     int64_t y = 1;
  43.     int64_t max_dist = 0;
  44.     int64_t curr_dist = 0;
  45.     int64_t next_dist = 0;
  46.    
  47.     while(y != x)
  48.     {
  49.         //printf("%d %d\n", x, y);
  50.         curr_dist = MIN(
  51.             MIN(abs(dist_forward[y] - dist_forward[x]),
  52.                 abs(dist_forward[y] + dist_backward[x])),
  53.             MIN(abs(dist_backward[y] + dist_forward[x]),
  54.                 abs(dist_backward[y] - dist_backward[x])));
  55.         next_dist = MIN( MIN(abs(dist_forward[y + 1] - dist_forward[x]), abs(dist_forward[y + 1] + dist_backward[x])), MIN(abs(dist_backward[y + 1] + dist_forward[x]), abs(dist_backward[y + 1] - dist_backward[x])));
  56.         //printf("Curr dist: %lld\n", curr_dist);
  57.         //printf("Next dist: %lld\n", next_dist);
  58.         if(y == (a - 1))
  59.         {
  60.             x++;
  61.         }
  62.         else
  63.         {
  64.             if( next_dist >= curr_dist)
  65.             {
  66.                 y++;
  67.             }
  68.             else
  69.             {
  70.                 x++;
  71.             }
  72.         }
  73.         max_dist = MAX(max_dist, curr_dist);
  74.     }
  75.     //printf("\n%d %d\n", x, y);
  76.     //printf("\nMax distance: %d\n", max_dist);  
  77.     printf("%lld", max_dist);  
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top