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
Showing posts with label sorting. Show all posts
Showing posts with label sorting. Show all posts

Sunday, August 17, 2014

ARRANGE-Arranging Amplifiers

Arranging Amplifiers

Given below code is for ARRANGE spoj or Arrange Amplifiers spoj.

This question is based on greedy approach.

Main idea is  a^b < b^a (if a>b and not valid for a=3 and b=3) , so place all the amplifiers with higher amplification first . If there is amplifier with amplification 1 the place all these amplifier first.



#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int array[n],count1=0,count=0,input,j=0;
        for (int i=0; i<n; i++)
        {
            scanf("%d",&input);
            if (input==1)
                count1++;
            else
            {
                array[j++]=input;
                count++;
            }
        }
        sort(array,array+count);
        for (int i=0; i<count1; i++)
            printf("1 ");
        if (count==2 && array[0]==2 && array[1]==3)
            cout<<2<<" "<<3;
        else
        {
            for (int i=count-1; i>=0; i--)
                printf("%d ",array[i]);
        }
        cout<<endl;
    }
    return 0;
}


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;
}