Saturday, October 28, 2017

1259 - Even and Odd

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    int tc,n,len,i;

    while (sf ("%d",&tc) != EOF)
    {
        vector <int> even,odd;

        while (tc--)
        {
            sf ("%d",&n);

            if (n & 1)
                odd.pb(n);
            else
                even.pb(n);
        }

        sort (even.begin(),even.end());
        sort (odd.begin(),odd.end());

        len = even.size();

        for (i=0; i<len; ++i)
            pf ("%d\n",even[i]);

        len = odd.size();

        for (i=len-1; i>=0; i--)
            pf ("%d\n",odd[i]);
    }

    return 0;
}

Friday, October 27, 2017

1239 - Bloggo Shortcuts

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    char str[55];
    int len,i,c1,c2;

    while (gets(str))
    {
        len = strlen(str), c1 = c2 = 0;

        for (i=0; i<len; ++i)
        {
            if (str[i] == '_')
            {
                ++c1;

                if (c1 & 1)
                    pf ("<i>");
                else
                    pf ("</i>");
            }
            else if (str[i] == '*')
            {
                ++c2;

                if (c2 & 1)
                    pf ("<b>");
                else
                    pf ("</b>");
            }
            else
                pf ("%c",str[i]);
        }

        pf ("\n");
    }

    return 0;
}

1241 - Fit or Dont Fit II

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    char a[1005],b[1005];
    int tc,l1,l2,d,i,j;

    sf ("%d",&tc);

    while (tc--)
    {
        sf ("%s %s",a,b);

        l1 = strlen(a), l2 = strlen(b);

        if (l2 > l1)
            pf ("nao encaixa\n");
        else
        {
            d = l1-l2;

            for (i=d,j=0; i<l1,j<l2; i++,j++)
            {
                if (a[i] != b[j])
                {
                    pf ("nao encaixa\n");
                    break;
                }
            }

            if (j == l2)
                pf ("encaixa\n");
        }
    }

    return 0;
}

1240 - Fit or Dont Fit I

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    char a[12],b[12];
    int tc,l1,l2,d,i,j;

    sf ("%d",&tc);

    while (tc--)
    {
        sf ("%s %s",a,b);

        l1 = strlen(a), l2 = strlen(b);

        if (l2 > l1)
            pf ("nao encaixa\n");
        else
        {
            d = l1-l2;

            for (i=d,j=0; i<l1,j<l2; i++,j++)
            {
                if (a[i] != b[j])
                {
                    pf ("nao encaixa\n");
                    break;
                }
            }

            if (j == l2)
                pf ("encaixa\n");
        }
    }

    return 0;
}

SPOJ Solutions

URI Solutions

1238 - Combiner

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    int tc,l1,l2,i,j;
    char a[55],b[55];

    sf ("%d",&tc);

    while (tc--)
    {
        sf ("%s %s",a,b);
        string str;

        l1 = strlen(a),l2 = strlen(b),i=j=0;

        if (l1 == l2)
        {
            for (i=0; i<l1; i++)
            {
                str.pb (a[i]);
                str.pb (b[i]);
            }
        }
        else if (l1 > l2)
        {
            for (i=0; i<l2; i++)
            {
                str.pb (a[i]);
                str.pb (b[i]);
            }

            for (i=l2; i<l1; i++)
                str.pb (a[i]);
        }
        else
        {
            for (i=0; i<l1; i++)
            {
                str.pb (a[i]);
                str.pb (b[i]);
            }

            for (i=l1; i<l2; i++)
                str.pb (b[i]);
        }

        pf ("%s\n",str.c_str());
    }

    return 0;
}

1235 - Inside Out

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    int n,len,h,i;
    char str[105];

    sf ("%d",&n);

    getchar ();

    while (n--)
    {
        gets (str);
        len = strlen(str);
        h = len/2;

        string s;

        for (i=h-1; i>=0; i--)
            s.pb(str[i]);

        for (i=len-1; i>=h; i--)
            s.pb(str[i]);

        pf ("%s\n",s.c_str());
    }

    return 0;
}

1199 - Simple Base Conversion

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define u unsigned int

long long power (long long n, long long p)
{
    long long res = 1;

    while (p--)
        res *= n;

    return res;
}

int main (void)
{
    string str,hexa;
    long long len,i,val,deci,p,l,rem;

    while (cin >> str)
    {
        if (str[0] == '-')
            break;

        len = str.length();
        hexa = "";
        deci = 0;

        if (str[0] == '0' && str[1] == 'x')
        {
            l = len-3;

            for (i=2; i<len; i++)
            {
                if (isalpha(str[i]))
                {
                    if (isupper(str[i]))
                        val = str[i] - 55;
                    else
                        val = str[i] - 87;
                }
                else
                    val = str[i] - '0';

                p = power (16,l--);
                deci += val * p;
            }

            cout << deci << endl;
        }
        else
        {
            l = len-1;

            for (i=0; i<len; i++)
            {
                val = str[i] - '0';
                deci += val * power (10,l--);
            }

            while (deci != 0)
            {
                rem = deci % 16;

                if (rem == 10)
                    hexa += "A";
                else if (rem == 11)
                    hexa += "B";
                else if (rem == 12)
                    hexa += "C";
                else if (rem == 13)
                    hexa += "D";
                else if (rem == 14)
                    hexa += "E";
                else if (rem == 15)
                    hexa += "F";
                else
                    hexa += to_string(rem);

                deci /= 16;
            }

            reverse (hexa.begin(),hexa.end());

            cout << "0x"+hexa << endl;
        }
    }

    return 0;
}

1120 - Contract Revision

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

int main (void)
{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    string str;
    char ch;
    int len,i,k;
    bool x,y;

    while (cin >> ch >> str)
    {
        if (ch == '0' && str == "0")
            break;

        x = false;
        len = str.size();
        string a;

        for (i=0; i<len; i++)
        {
            if (str[i] == ch)
                continue;

            if (str[i] != '0')
                x = true;

            if (x)
                a += str[i];
        }

        len = a.size();
        k = 0;

        if (len == 0)
            a = "0";
        else
        {
            for (i=0; i<len; i++)
                if (a[i] == '0')
                    k++;

            if (k == len)
                a = "0";
        }

        cout << a << endl;
    }

    return 0;
}

Wednesday, October 25, 2017

12527 - Different Digits (II)

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int arr[5005];

void preCalc ()
{
    int i,t,rem;
    bool k;

    arr[0] = 0;

    for (i=1; i<5005; i++)
    {
        t = i;
        k = 1;

        map <int,int> mp;
        map <int,int> :: iterator it;

        while (t)
        {
            rem = t % 10;
            mp[rem]++;
            t /= 10;
        }

        for (it=mp.begin(); it!=mp.end(); it++)
        {
            if (it->second > 1)
            {
                k = 0;
                break;
            }
        }

        if (k == 1)
            arr[i] = arr[i-1]+1;
        else
            arr[i] = arr[i-1];
    }
}

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    preCalc ();
    U l,u,i,temp;

    while (sf ("%u %u",&l,&u) != EOF)
        pf ("%u\n",arr[u]-arr[l-1]);

    return 0;
}

10281 - Average Speed

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define ssf sscanf
#define spf sprintf
#define fsf fscanf
#define fpf fprintf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    char str[20];
    int n,hr,mint,sec,sp=0,nsp,x=0,y;
    double dis;

    while (gets (str))
    {
        n = ssf (str,"%d:%d:%d %d",&hr,&mint,&sec,&nsp);

        y = (hr * 3600) + (mint * 60) + sec;
        dis += ((y - x) * sp) / 3600.0;
        x = y;

        if (n == 4)
            sp = nsp;
        else
            pf ("%02d:%02d:%02d %0.2lf km\n",hr,mint,sec,dis);
    }

    return 0;
}

10137 - The Trip

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

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

#define sf scanf
#define pf printf
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pii pair <int,int>
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int
#define endl "\n"

const int MOD = 1000000007;
const int MAX = 1000005;

int main (void)
{
    /*
    freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);
    */

    int n,i;
    double arr[1005],sum,avg,pos,neg,val;

    while (sf ("%d",&n) && n)
    {
        sum = 0;

        for (i=0; i<n; i++)
        {
            sf ("%lf",&arr[i]);
            sum += arr[i];
        }

        avg = sum/double(n);

        pos = neg = 0.0;

        for (i=0; i<n; i++)
        {
            val = (int) ((avg-arr[i])*100) / 100.0;

            if (val > 0)
                pos += val;
            else
                neg += -val;
        }

        pf ("$%0.2lf\n",pos > neg ? pos : neg);
    }

    return 0;
}