Search This Blog

Showing posts with label ad-hoc problem. Show all posts
Showing posts with label ad-hoc problem. Show all posts

Monday, 22 August 2016

SPOJ - A Game with Numbers O(1) solution

SPOJ -  A Game with Numbers O(1) solution

Solution:-

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin>>N;
    if(N%10==0)
        cout<<"2";
    else
        cout<<"1"<<endl<<N%10;
    return 0;

}

Sunday, 21 August 2016

SPOJ - Girls And Boys Problem Solution

SPOJ - Girls And Boys Problem Solution

Solution:-

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int g,b;
    while(1)
    {
        cin>>g>>b;
        if(g==-1 && b==-1)
            break;
        else if(g==0 && b==0)
            cout<<g;
        else if(g>b)
            cout<<ceil(double(g)/(b+1));
        else
            cout<<ceil(double(b)/(g+1));

    cout<<endl;
    }

}

Friday, 19 August 2016

SPOJ - Build a Fence Problem Solution

SPOJ - Build a Fence Problem Solution

Solution :-

#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592654

int main()
{
    double area,n;
    while(1)
    {
        cin>>n;
        if(!n)
            break;

        area=(n*n)/(2*PI);
        cout<<fixed<<setprecision(2)<<area<<endl;
    }
    return 0;

}

SPOJ - Street Parade Problem Solution (Using Stack)

SPOJ - Street Parade Problem Solution (Using Stack)

Solution:-

#include<bits/stdc++.h>
using namespace std;

int arr[1010];
bool flag;

int main()
{
    int n,prev,index;
    while(1)
    {
        stack<int> s;
        cin>>n;
        if(n==0)
            break;

        for(int i=0;i<n;i++)
            cin>>arr[i];

        prev=index=0;
        flag=false;
        while(index<n)
        {
            if(arr[index]==prev+1)
            {
                prev=arr[index];
                index++;
            }
            else if(s.size() && s.top()==prev+1)
            {
                prev=s.top();
                s.pop();
            }
            else
            {
                s.push(arr[index]);
                index++;
            }
        }
        while(s.size() && s.top()==prev+1)
        {
            prev=s.top();
            s.pop();
        }
        if(!s.size())
            flag=true;

        if(flag)
            cout<<"yes\n";
        else
            cout<<"no\n";

    }
    return 0;

}

Thursday, 18 August 2016

SPOJ- Counting Triangles Solution

SPOJ - Counting Triangles Problem Solution

Solution:-

#include<bits/stdc++.h>
using namespace std;
#define MAX 1000010

long long up[MAX],down[MAX];

void init()
{
    up[1]=1;
    up[2]=4;
    down[2]=1;

    for(int i=3;i<=MAX;i++)
    {
        up[i]=2*up[i-1]+i-up[i-2];
        down[i]=up[i-1]-down[i-1];
    }
}

int main()
{
    init();
    int tc,num;;
    cin>>tc;
    while(tc--)
    {
        cin>>num;
        cout<<up[num]+down[num]<<endl;
    }
    return 0;
}


SPOJ- Army Strength Problem Solution

SPOJ- Army Strength Problem Solution #ad-hoc problem

Solution:-

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int tc;
    string ex;
    cin>>tc;
    getline(cin,ex);
    while(tc--)
    {
        int NG,NM,num;
        int max_NG=-1,max_NM=-1;

        cin>>NG>>NM;

        for(int i=0;i<NG;i++)
        {
            cin>>num;
            max_NG=max_NG>num?max_NG:num;
        }

        for(int i=0;i<NM;i++)
        {
            cin>>num;
            max_NM=max_NM>num?max_NM:num;
        }

        if(max_NM<=max_NG)
            cout<<"Godzilla\n";
        else
            cout<<"MechaGodzilla\n";
        getline(cin,ex);
    }
    return 0;

}

SPOJ- Ambiguous Permutations Problem Solution

SPOJ- Ambiguous Permutations Problem Solution

Solution:-

#include<bits/stdc++.h>
using namespace std;
#define MAX 100010

int arr[MAX];

bool cal_per(int n)
{
    for(int i=1;i<=n;i++)
    {
        if(i!=arr[arr[i]])
            return false;
    }
    return true;
}

int main()
{
    int n;
    while(1)
    {
        cin>>n;
        if(n==0)
            break;

        for(int i=1;i<=n;i++)
            cin>>arr[i];

        if(cal_per(n))
            cout<<"ambiguous\n";
        else
            cout<<"not ambiguous\n";
    }
    return 0;

}