10

Click here to load reader

ICL 2010 Advantage Round Solutions

Embed Size (px)

DESCRIPTION

ICL 2010 was conducted during Apogee 2010. The coding contest involved solving output only problems. Use of any programming language was allowed. This is the solution set of the advantage round.

Citation preview

Page 1: ICL 2010 Advantage Round Solutions

Advantage Round

Solutions

BITS ACM

ICL 2010

Mayank Abhishek APOGEE 2010

Page 2: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 1

Addition of Big Numbers

Use Windows/Linux calculator or solve manually. Coding in C/C++ is the last resort.

If you use Linux then this Shell Script solves all input files in one go:

for i in `seq 0 9`; do cat $i.in | sed 's/ /+/' | bc | tail -`head -1 $i.in` >$i.out ; done

Page 3: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 2

Powered Fractions

Sort on the basis of

. Any sort algorithm will do.

Since most input files contained only 2 fractions you could also solve them manually.

For finding the GCD to reduce the fractions you could use,

function gcd(a, b) if a = 0 return b while b ≠ 0 if a > b a := a − b else b := b − a return a

Page 4: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 3

Playing With Fibonacci Numbers

Do you really need the solution for this? The only catch was to not use recursion which could

blow up the runtime.

Page 5: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 4

Playing With Tiles

The following recursive formula solves the problem,

F(1) = 1

F(2) = 2

F(N) = F(N-1) + F(N-2)

How to arrive on this formula?

For each tiling arrangement of size N-1 you can add a vertical tile to get an arrangement of N

tiles. For each arrangement of size N-2 you can add 2 horizontal tiles to get an arrangement of N

tiles.

Solution for N=1 and N=2 are trivial.

We do not count adding two vertical tiles in front of all arrangements of size N-2. Why?

Again in this problem, do not use recursion. Iterate wisely. Take care of the limits of the data

type you used for the variables as the numbers generated will be huge. Always keep F(N) as

F(N) mod 10000.

Page 6: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 5

Three Way Partitions

Create an array SUM[N] where SUM[i] stores the sum of all the numbers upto i in the list.

Now you can calculate the sum of numbers between positions k and m (both inclusive) in one

step it will be,

SUM[k]-SUM[m-1]

Now we have to place two splitters. Try all combinations i.e NC2. Take MIN over the MAX from

each combination.

Page 7: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 6

More Players In The Game

Create an array SUM[N] where,

SUM[i] = SUM[i-1] + VALUE[i], if VALUE[i] is non-zero

SUM[i] = 0, otherwise

Take the MAX over the array SUM[N]

Page 8: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 7

Chocolate Cookies

The array WAYS[M][N] will contain the number of ways of reaching i, j from 1,1 for each i, j.

WAYS[1][1] = 1,

WAYS[i][j] = WAYS[i-1][j]+WAYS[i][j-1], if i, j is not blocked

WAYS[i][j] = 0, otherwise

Handle the cases for first row and first column. Also, be careful to keep only the last 4 digits as

values grow very large.

Page 9: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 8

The Winner Takes It All

For each pair of buildings (both inclusive) calculate two things,

The minimum height of buildings between them.

The sum of widths of all buildings between them.

Multiply these two to get the largest rectangular area between the two buildings which includes

both.

Take the MAX over all areas so found.

This can be done in O(N2), which was expected.

An O(NlogN) solution is also possible, but was not required.

Page 10: ICL 2010 Advantage Round Solutions

I C L 2 0 1 0 A D V A N T A G E R O U N D S O L U T I O N S

Page 9

Poof Go Clouds & Pyramids - Pyramids

Poof Go Clouds can be solved by using a simple DFS (Depth First Search)

[http://en.wikipedia.org/wiki/Depth-first_search] or BFS (Breadth First Search)

[http://en.wikipedia.org/wiki/Breadth-first_search].

Pyramids – Pyramids was Longest increasing subsequence

[http://en.wikipedia.org/wiki/Longest_increasing_subsequence].