UVa : 11495 (Bubbles and Buckets)



// http://uva.onlinejudge.org/external/114/11495.html
// Runtime : 0.660s
// Binary Search Tree ( Inversion Count ) 

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL unsigned long long
using namespace std;

LL tree [4 * N];

LL greater_than_input (int at, int lo, int hi, int x)
{
    if ( hi <= x ) return 0;
    if ( lo > x ) return tree [at];

    int mid = ( lo + hi ) / 2;

    return greater_than_input (2 * at, lo, mid, x) +
    greater_than_input (2 * at + 1, mid + 1, hi, x);
}

void insert (int at, int lo, int hi, int x)
{
    if ( lo == hi ) {
        tree [at] = 1;
        return;
    }

    if ( x < lo || x > hi ) return;

    int mid = ( lo + hi ) / 2;

    if ( x <= mid ) insert (2 * at, lo, mid, x);
    else insert (2 * at + 1, mid + 1, hi, x);

    tree [at]++;
}

int main ()
{
    int n;

    while ( scanf ("%d", &n) && n ) {
        memset (tree, 0, sizeof (tree));
        int input;
        LL swaps = 0;

        for ( int i = 0; i < n; i++ ) {
            scanf ("%d", &input);
            insert (1, 1, n, input);
            swaps += greater_than_input (1, 1, n, input);
        }

        if ( swaps % 2 ) printf ("Marcelo\n");
        else printf ("Carlos\n");
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s