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

Sunday, August 17, 2014

CODFURY-Megatron and his rage

Megatron and his rage

below given code is for codefury spoj or Megatron and his range spoj.
Question is based on Two pointer method .

Here i am just checking the interval that sum of interval is equal to given autobots or not and select the highest range .
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int p,m;
        scanf("%d%d",&p,&m);
        int arr[p+9],flag=0;
        long long sum=0,max=-1,total=99999999,cumu[p+9];
        cumu[0]=0;
        for(int i=1;i<=p;i++)
        {
            scanf("%d",&arr[i]);
            sum+=arr[i];
            cumu[i] =sum;
        }
        int p1=0,p2=0;
        sum=0;
        for(int i=1;i<=p;i++)
        {
            if(flag==1)
                i--;
            sum= cumu[i] - cumu[p1];
            flag=0;
            if(sum>m)
            {
                flag=1;
                p1++;
            }
            if(max<(i-p1)){
                max=(i-p1);
                total = sum;
            }
            else if(max==(i-p1))
            {
                if(total>sum)
                    total=sum;
            }
        }
        if(max==0)
            total=0;
        if(total==99999999)
            printf("0 0\n");
        else
            printf("%lld %lld\n",total,max);
    }
    return 0;
}