Friday 7 February 2014

Shop Associations -Code Chef Problem

The shop are categorised as type I. The shop from which he has not brought are categorised as type II. Each shop has initially its own association. One assosciation will merge with another assosciation to form a new assosciation if the shops in both assosciations are of same type (i.e either of type I or type II) and there exists any two shops in the merging assosciations(one in each), distance between whome is 1m.help him to maximise the the sum of the prices by shops of asscosciation of type I by helping him choosing the shops to pick for buying the book. You should provide him the desired sum.

Solution:

#include<stdio.h>
#include<iostream>
using namespace std;
int max(int x, int y)
{ return (y > x)? y : x; }

int maxSubArraySum(int *a, int size)
{
   int max_so_far = a[0], i;
   int curr_max = a[0];

   for (i = 1; i < size; i++)
   {
        curr_max = max(a[i], curr_max+a[i]);
        max_so_far = max(max_so_far, curr_max);
   }
   return max_so_far;
}


int main()
{
   int T = 0;
   cin>>T;
   int *a;
   int n = 0;
   
   int max_sum = 0 ;
   for(int i = 0 ; i < T ; i++)
   {
           cin>>n;
           a = new int[n];
           for(int j = 0 ; j < n ; j++)
           {
                   cin>>a[j];
           }
       max_sum = maxSubArraySum(a, n);
   cout<<max_sum<<endl;
   delete [] a;
   }
  
   return 0;
}



No comments:

Post a Comment