Friday, January 22, 2016

Facebook Hacker Cup 2015 Qualification Round, Cooking the Books

Facebook Hacker Cup 2015 Qualification Round, Cooking the Books

Problem Statements:
https://www.facebook.com/hackercup/problem/582062045257424/

Solution:
We could solve the problem by testing all possible numbers when any 2 digits are swapped.
We swap position i and j for every possible i and j where 1<i<9 and 1<j<9, so the number of all possible combination is small enough for brute force attack.

Example codes:

            long T = inp.NextInt ( );
            for ( int t = 0; t < T; t++ )
            {
                long N = inp.NextLong ( );
                string sn = N.ToString ( );
                char[] cn = sn.ToCharArray ( );
                string ans = "";
                if ( N < 10 )
                {
                    ans = string.Format ( "{0} {1}", N, N );
                }
                else
                {
                    string snmin = sn;
                    string snmax = sn;
                    for ( int i = 0; i < sn.Length; i++ )
                    {
                        for ( int j = i + 1; j < sn.Length; j++ )
                        {
                            //swap then test
                            char[] cc = cn.ToArray ( );
                            char tmp = cc[i];
                            cc[i] = cc[j];
                            cc[j] = tmp;
                            string st = new string ( cc );
                            if ( st.CompareTo ( snmin ) < 0 && st[0] != '0' )
                                snmin = st;
                            if ( st.CompareTo ( snmax ) > 0 )
                                snmax = st;
                        }
                    }

                    ans = string.Format ( "{0} {1}", snmin, snmax );
                }

                Console.WriteLine ( "Case #{0}: {1}", t + 1, ans );
            }


Saturday, January 16, 2016

Facebook Hacker Cup 2015 Round 1, Winning at Sports

Problem Statement:
https://www.facebook.com/hackercup/problem/688426044611322/

Solution:
This could be solved by using dynamic programming.
Let's define dp[ i, j ] as the number of ways to reach the final score of i-j.

For the stress-free victory:
The base cases:
  dp[ i, 0 ] = 1, 1 <= i <= N
  dp[ 0, i ] = 0, 1 <= i <= N

The recurrence:
  dp[ i, j ] = 0,                                          if i <= j
  dp[ i, j ] = dp[ i-1, j ] + dp[ i, j-1 ],        if i > j

For the stressful victory:
The base cases:
  dp[ i, 0 ] = 1, 1 <= i <= N
  dp[ 0, i ] = 1, 1 <= i <= N

The recurrence:
  dp[ i, j ] = dp[ j, j ],                                if i > j
  dp[ i, j ] = dp[ i-1, j ],                            if i = j
  dp[ i, j ] = dp[ i-1, j ] + dp[ i, j-1 ],        if i < j


Example codes:

            int T = inp.NextInt ( );
            long mod = 1000000000 + 7;
            long[,] dpstressfree = new long[2001, 2001];
            long[,] dpstressfull = new long[2001, 2001];
            dpstressfree[0, 0] = 0;
            for ( int i = 1; i < 2001; i++ )
            {
                dpstressfree[i, 0] = 1;
                dpstressfree[0, i] = 0;
            }
            for ( int i = 1; i < 2001; i++ )
            {
                for ( int j = 1; j < 2001; j++ )
                {
                    if ( i <= j )
                        dpstressfree[i, j] = 0;
                    else
                    {
                        dpstressfree[i, j] = ( dpstressfree[i - 1, j] + dpstressfree[i, j - 1] ) % mod;
                    }
                }
            }

            dpstressfull[0, 0] = 1;
            for ( int i = 1; i < 2001; i++ )
            {
                dpstressfull[i, 0] = 1;
                dpstressfull[0, i] = 1;
            }
            for ( int i = 1; i < 2001; i++ )
            {
                for ( int j = 1; j < 2001; j++ )
                {
                    if ( i > j )
                        dpstressfull[i, j] = dpstressfull[j, j];
                    else if ( i == j )
                        dpstressfull[i, j] = dpstressfull[i - 1, j];
                    else
                    {
                        dpstressfull[i, j] = ( dpstressfull[i - 1, j] + dpstressfull[i, j - 1] ) % mod;
                    }
                }
            }

            for ( int t = 0; t < T; t++ )
            {
                string[] s = inp.NextString ( ).Split ( '-' );
                int A = int.Parse ( s[0] );
                int B = int.Parse ( s[1] );
                long ans1=dpstressfree[A, B];
                long ans2=dpstressfull[A, B];
                Console.WriteLine ( "Case #{0}: {1} {2}", t + 1, ans1, ans2 );
            }

Friday, January 15, 2016

Facebook Hacker Cup 2015 Round 1, Homework

Problem Statement:

https://www.facebook.com/hackercup/problem/582396081891255/

Solution:

First we generate the list of prime numbers between 2 and 10^7.
Then we calculate the primacity of each number between 2 and 10^7 and save the results into an array.
To calculate the primacity of all numbers, we set all primacities to zero, then for each prime number we increase the primacity of each multiple of that prime by 1.
At the end we will have the primacity of all numbers completely filled, and it will be easy to count the requested answer.

Example codes in C#:

            int T = inp.NextInt ( );
            List<int> primes = GetPrimes ( 10000000 );

            int[] prmCity = new int[10000001];
            for ( int i = 0; i < primes.Count; i++ )
            {
                int p = primes[i];
                for ( int pp = p; pp < prmCity.Length; pp+=p )
                {
                    prmCity[pp] += 1;
                }
            }

            for ( int t = 0; t < T; t++ )
            {
                int A = inp.NextInt ( );
                int B = inp.NextInt ( );
                int K = inp.NextInt ( );

                int cnt=0;
                for ( int i = A; i <= B; i++ )
                {
                    if ( prmCity[i] == K )
                        cnt++;
                }

                int ans=cnt;
                Console.WriteLine ( "Case #{0}: {1}", t + 1, ans );
            }


        private static List<int> GetPrimes ( int maxPrimeNumber )
        {
            bool[] isPrime = GetIsPrime ( maxPrimeNumber );
            List<int> primes = new List<int> ( );
            for ( int i = 2; i < isPrime.Length; i++ )
            {
                if ( isPrime[i] )
                    primes.Add ( i );
            }
            return primes;
        }

        private static bool[] GetIsPrime ( int max )
        {
            // Sieve of Eratosthenes
            bool[] isPrime = new bool[max + 1];
            for ( int i = 0; i <= max; i++ )
                isPrime[i] = true;
            isPrime[0] = false;
            isPrime[1] = false;
            for ( int x = 2; x * x <= max; x++ )
            {
                if ( isPrime[x] )
                {
                    int xx = x;
                    while ( true )
                    {
                        xx += x;
                        if ( xx <= max )
                            isPrime[xx] = false;
                        else
                            break;
                    }
                }
            }
            return isPrime;
        }

Facebook Hacker Cup 2014 Qualification Round, Basketball Game

Problem Statement:
https://www.facebook.com/hackercup/problem/740733162607577/

Solution:
We just need to implement it correctly and simulate the algorithms as specified in the problem statement.
In the example solution below we prepare Player class, for example:

        class Player
        {
            public string Name;
            public int ShotPct;
            public int Height;
            public int Team;
            public int Draft;
            public int PlayTime;
        }

We then order the players by shot_percentage, and then by height. By using LINQ, we could do it easily:

allPlayers = allPlayers.OrderByDescending ( p => p.ShotPct ).ThenByDescending ( p => p.Height ).ToList ( );

We then assign the player into team 1 or 2 based on whether its draft number is odd or even.
We then provide four HashSets to separate the players into 4 groups:

- team1Playing
- team1Sitting
- team2Playing
- team2Sitting

We then run the game for M minutes and do player replacement every minute if needed.
When a player is replaced, we remove it from teamXPlaying set and put it into teamXSitting set.
We also remove the new player from teamXSitting set and add it into teamXPlaying set.

At the end of the game, we get the players currently playing in both team1Playing and team2Playing sets.

Example codes:

            for ( int t = 0; t < T; t++ )
            {
                int N = inp.NextInt ( );
                int M = inp.NextInt ( );
                int P = inp.NextInt ( );
                List<Player> allPlayers = new List<Player> ( );
                for ( int i = 0; i < N; i++ )
                {
                    Player p = new Player ( );
                    p.Name = inp.NextString ( );
                    p.ShotPct = inp.NextInt ( );
                    p.Height = inp.NextInt ( );
                    allPlayers.Add ( p );
                }

                allPlayers = allPlayers.OrderByDescending ( p => p.ShotPct ).ThenByDescending ( p => p.Height ).ToList ( );
                for ( int i = 0; i < N; i++ )
                {
                    allPlayers[i].Draft = i + 1;
                    if ( allPlayers[i].Draft % 2 == 1 )
                        allPlayers[i].Team = 1;
                    else
                        allPlayers[i].Team = 2;
                }

                HashSet<int> team1Playing = new HashSet<int> ( allPlayers.Where ( p => p.Team == 1 ).
                    OrderBy ( p => p.Draft ).Take ( P ).Select ( p => p.Draft ) );
                HashSet<int> team2Playing = new HashSet<int> ( allPlayers.Where ( p => p.Team == 2 ).
                    OrderBy ( p => p.Draft ).Take ( P ).Select ( p => p.Draft ) );
                HashSet<int> team1Sitting = new HashSet<int> ( allPlayers.Where ( p => p.Team == 1 ).
                    OrderBy ( p => p.Draft ).Skip ( P ).Select ( p => p.Draft ) );
                HashSet<int> team2Sitting = new HashSet<int> ( allPlayers.Where ( p => p.Team == 2 ).
                    OrderBy ( p => p.Draft ).Skip ( P ).Select ( p => p.Draft ) );


                for ( int i = 0; i < M; i++ )
                {
                    //update playtime
                    foreach ( int draft in team1Playing )
                    {
                        Player p = allPlayers[draft - 1];
                        p.PlayTime++;
                    }
                    foreach ( int draft in team2Playing )
                    {
                        Player p = allPlayers[draft - 1];
                        p.PlayTime++;
                    }
                    if ( team1Playing.Count + team1Sitting.Count > P )
                    {
                        ReplacePlayer ( allPlayers, team1Playing, team1Sitting );
                    }
                    if ( team2Playing.Count + team2Sitting.Count > P )
                    {
                        ReplacePlayer ( allPlayers, team2Playing, team2Sitting );
                    }
                }
                List<string> names=new List<string>();
                foreach ( int draft in team1Playing )
                    names.Add ( allPlayers[draft - 1].Name );
                foreach ( int draft in team2Playing )
                    names.Add ( allPlayers[draft - 1].Name );

                names.Sort ( );
                string ans=string.Join ( " ", names.ToArray ( ) );
                Console.WriteLine ( "Case #{0}: {1}", t + 1, ans );
            }


        private static void ReplacePlayer ( List<Player> allPlayers, HashSet<int> team1Playing, HashSet<int> team1Sitting )
        {
            //get who out
            List<Player> p1 = new List<Player> ( );
            foreach ( int draft in team1Playing )
                p1.Add ( allPlayers[draft - 1] );
            Player pout = p1.OrderByDescending ( p => p.PlayTime ).ThenByDescending ( p => p.Draft ).First ( );
            //get who in
            List<Player> p1s = new List<Player> ( );
            foreach ( int draft in team1Sitting )
                p1s.Add ( allPlayers[draft - 1] );
            Player pin = p1s.OrderBy ( p => p.PlayTime ).ThenBy ( p => p.Draft ).First ( );
            team1Playing.Remove ( pout.Draft );
            team1Playing.Add ( pin.Draft );
            team1Sitting.Remove ( pin.Draft );
            team1Sitting.Add ( pout.Draft );
        }

Facebook Hacker Cup 2014 Qualification Round, Square Detector

Problem statement:
https://www.facebook.com/hackercup/problem/318555664954399/

Solution:
We scan all cells started from the first row, from left to right for each row.
When the first black cell found, save the position as x0 and y0.
If there is indeed a valid black square, the position (x0,y0) will be the left-top corner of the black square.
During scanning we also count the black cells and the maximum x and y position of the black cell. We save these values as cnt_Black_Cells, maxX, and maxY.

After finished, we need to verify that the width of the candidate of the black square is black_width = (maxX - x0 + 1). It must be equal to (maxY - y0 +1), otherwise it is not a square.
The value of cnt_Black_Cells should also equal to black_width * black_width.

Then we scan the cells again, but now we only need to scan the following area:
 y: y0 to maxY 
 x: x0 to maxX
During this scan, we verify that every cell in this region is black.
If we found any white cell, then it is not a valid square.

Example codes:

            int T = inp.NextInt ( );
            //int T=1;

            for ( int t = 0; t < T; t++ )
            {
                int N = inp.NextInt ( );
                int x0=-1;
                int y0=-1;
                int xMax=-1;
                int yMax=-1;
                char[][] grid = new char[N][];
                for ( int i = 0; i < N; i++ )
                {
                    string s = inp.NextString ( );
                    grid[i] = s.ToCharArray ( );
                }
                int numBlacks=0;
                for ( int i = 0; i < N; i++ )
                {
                    for ( int j = 0; j < N; j++ )
                    {
                        if ( grid[i][j] == '#' )
                        {
                            if ( x0 < 0 )
                                x0 = j;
                            if ( y0 < 0 )
                                y0 = i;
                            xMax = Math.Max ( xMax, j );
                            yMax = Math.Max ( yMax, i );
                            numBlacks++;
                        }
                    }
                }
                int nb=0;
                for ( int i = y0; i <= yMax; i++ )
                {
                    for ( int j = x0; j <= xMax; j++ )
                    {
                        if ( grid[i][j] == '#' )
                            nb++;
                    }
                }
                int dy = yMax - y0 + 1;
                int dx = xMax - x0 + 1;
                string ans=( ( dx == dy ) && ( numBlacks == nb ) && ( dx * dx == nb ) ) ? "YES" : "NO";
                Console.WriteLine ( "Case #{0}: {1}", t + 1, ans );
    }