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

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


No comments:

Post a Comment

Your comment is valuable to us