Sei sulla pagina 1di 7

NITK, Information Technology

Course: Advance Algorithms IT700

Divide and Conquer


Problem 1:
Ron Weasley has a magical wooden log of length 1. He wants to cut it into as many little logs as he

can. But he wants to adhere to the following rule: At any moment, the length of the longest log
which he possesses may not be larger than the length of shortest one, times some constant
factor. Every time, he is only allowed to cut exactly one log into two shorter ones.
Input
One floating-point number, 1 k 1.999, meaning the stated constant factor. The number will
have at most 3 digits after the decimal point.
Output
First, you should output one number n, the maximal achievable number of logs for the given value
of the constant factor. Then, you should output any proof that this number of logs is in fact
achievable: n-1 descriptions of cutting, using the following notation. At each step, you print two
numbers: first, the index of the log that you want to cut into two parts; second, the length of the
newly created log (cut off from the original one). It is assumed that the starting log has index 0.
Each newly created log will be given the lowest possible free integer index (so, at the ith step this
will be i). Each time, the size of size of the original log will be decreased by the size of the newly
created log.
Example
Input:
1.5
Output:
4
0 0.4
0 0.3
1 0.2

NITK, Information Technology

Course: Advance Algorithms IT700

Problem 2:
The last product of the R2 Company in the 2D games' field is a new revolutionary algorithm of
searching for the shortest path in a 2n maze.
Imagine a maze that looks like a 2n rectangle, divided into unit squares. Each unit square is
either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the
maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two
free maze cells, you need to determine the minimum time required to go from one cell to the other.
Unfortunately, the developed algorithm works well for only one request for finding the shortest path,
in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to
optimize the algorithm to find the shortest path. Write a program that will effectively respond to
multiple requests to find the shortest path in a 2n maze.

Input
The first line contains two integers, n and m (1n2105;

1m2105) the width of the

maze and the number of queries, correspondingly. Next two lines contain the maze. Each line
contains n characters, each character equals either '.' (empty cell), or 'X' (obstacle).
Each of the next m lines contains two integers vi and ui (1vi,ui2n) the description of the ith request. Numbers vi, ui mean that you need to print the value of the shortest path from the cell of
the maze number vi to the cell number ui. We assume that the cells of the first line of the maze are
numbered from 1 to n, from left to right, and the cells of the second line are numbered
from n+1 to 2n from left to right. It is guaranteed that both given cells are empty.

Output
Print m lines. In the i-th line print the answer to the i-th request either the size of the shortest path
or -1, if we can't reach the second cell from the first one.

NITK, Information Technology

Course: Advance Algorithms IT700

Sample test(s)

Input
4 7
.X..
...X
5 1
1 3
7 7
1 4
6 1
4 7
5 7

output
1
4
0
5
2
2
2

input
10 3
X...X..X..
..X...X..X
11 7
7 18
18 10

output
9
-1
3

NITK, Information Technology

Course: Advance Algorithms IT700

Problem 3:
Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify
to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a
friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make
sure Iahub will be the one qualified, he tells Iahub the following task.
You're given an (1-based) array a with n elements. Let's define function f(i,j) (1i,jn) as (i-

j)2+g(i,j)2. Function g is calculated by the following pseudo-code:

int g(int i, int j) {


int sum = 0;
for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1)
sum = sum + a[k];
return sum;
}
Find a value minij

f(i,j).

Probably by now Iahub already figured out the solution to this problem. Can you?

Input
The first line of input contains a single integer n (2n100000). Next line
contains n integers a[1], a[2], ..., a[n] (-104a[i]104).

Output
Output a single integer the value of minij

f(i,j).

Sample test(s)

input
4
1 0 0 -1

output
1

input
2
1 -1

output
2

NITK, Information Technology

Course: Advance Algorithms IT700

Problem 4:
There is an nm rectangular grid, each cell of the grid contains a single integer: zero or one. Let's
call the cell on the i-th row and the j-th column as (i,j).
Let's define a "rectangle" as four integers a,b,c,d (1acn;
denotes a set of cells of the grid {(x,y)

1bdm). Rectangle

: axc,byd}. Let's define a "good rectangle" as

a rectangle that includes only the cells with zeros.


You should answer the following q queries: calculate the number of good rectangles all of which
cells are in the given rectangle.

Input
There are three integers in the first line: n, m and q (1n,m40,1q3105). Each of the
next n lines contains m characters the grid. Consider grid rows are numbered from top to bottom,
and grid columns are numbered from left to right. Both columns and rows are numbered starting
from 1.
Each of the next q lines contains a query four integers that describe the current
rectangle, a, b, c, d (1acn;

1bdm).

Output
For each query output an answer a single integer in a separate line.
Sample test(s)

Input
5 5 5
00101
00000
00001
01000
00001
1 2 2 4
4 5 4 5
1 2 5 2
2 2 4 5
4 2 5 3

NITK, Information Technology

Course: Advance Algorithms IT700

output
10
1
7
34
5

input
4 7 5
0000100
0000010
0011000
0000000
1 7 2 7
3 1 3 1
2 3 4 5
1 2 2 7
2 2 4 7

output
3
1
16
27
52

Note:
For the first example, there is a 55 rectangular grid, and the first, the second, and the third
queries are represented in the following image.

NITK, Information Technology

Course: Advance Algorithms IT700

For the first query, there are 10 good rectangles, five 11, two 21, two 12, and
one 13.

For the second query, there is only one 11 good rectangle.

For the third query, there are 7 good rectangles, four 11, two 21, and one 31.

Potrebbero piacerti anche