Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Tuesday, March 11, 2014

GSHOP-Rama and Friends

Rama and Friends

given below code is for gshop spoj or Rama and Friends spoj
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n,k;
  scanf("%d%d",&n,&k);
  int count_n=0,count_z=0,count_p=0,i;
  long long sum=0,min=9999999,temp;
  int a[n+9];
  for(i=0;i<n;i++){
   scanf("%d",&a[i]);
   temp=a[i]>0?a[i]:-1*a[i];
   if(temp<min)
    min=temp;
   if(a[i]<0)
    count_n++;
  }
  if(k<=count_n)
  {
   int j=0;
   for(i=0;i<n;i++)
    if(a[i]<0 && j<k)
    {
     a[i]=-a[i];
     j++;
    }
   for(i=0;i<n;i++)
    sum+=a[i];
   printf("%lld\n",sum);
  }
  else
  {
   for(i=0;i<count_n;i++)
    a[i]=-a[i];
   for(i=0;i<n;i++){
    sum+=a[i];
   }
   if((k-count_n)%2!=0)
    sum+=-(2*min);
   printf("%lld\n",sum);
  }
 }
 return 0;
}

1 comment:

Your comment is valuable to us