# Oil Wel

### Problem Statement :

```Mr. Road Runner bought a piece of land in the middle of a desert for a nominal amount. It turns out that the piece of land is now worth millions of dollars as it has an oil reserve under it. Mr. Road Runner contacts the ACME corp to set up the oil wells on his land. Setting up oil wells is a costly affair and the charges of setting up oil wells are as follows.

The rectangular plot bought by Mr. Road Runner is divided into r * c blocks. Only some blocks are suitable for setting up the oil well and these blocks have been marked. ACME charges nothing for building the first oil well. For every subsequent oil well built, the cost would be the maximum ACME distance between the new oil well and the existing oil wells.

If (x,y) is the position of the block where a new oil well is setup and (x1, y1) is the position of the block of an existing oil well, the ACME distance is given by

max(|x-x1|, |y-y1|)
the maximum ACME distance is the maximum among all the ACME distance between existing oil wells and new wells.

If the distance of any two adjacent blocks (horizontal or vertical) is considered 1 unit, what is the minimum cost (E) in units it takes to set up oil wells across all the marked blocks?

Input Format

The first line of the input contains two space separated integers r *c*.
r lines follow each containing c space separated integers.
1 indicates that the block is suitable for setting up an oil well, whereas 0 isn't.

r c
M11 M12 ... M1c
M21 M22 ... M2c
...
Mr1 Mr2 ... Mrc
Constraints

1 <= r, c <= 50
Output Format

Print the minimum value E as the answer.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <iostream>

using namespace std;

#define M 53
#define INF 30300000

int n,m,a[M][M],s[M][M][M][M];

cin>>n>>m;
for (int i=0; i<n; ++i)
for (int j=0; j<m; ++j)
cin>>a[i][j];
}

int mod(int x){
return x<0 ? -x:x;
}

int fine(int x, int y, int l, int r, int u, int d){
return max(max(abs(l-x),abs(x-r)),max(abs(y-d),abs(y-u)));
}

void din(void){
for (int i=0; i<n; ++i)
for (int l=0; l+i<n; ++l)
for (int j=0; j<m; ++j)
for (int u=0; u+j<m; ++u){
int r=l+i;
int d=u+j;
if (l==r && u==d){
s[l][r][u][d]=0;
continue;
}

int h=INF;

if (l<r){
int kl=0,kr=0;
for (int j=u; j<=d; ++j){
if (a[l][j])
kl+=fine(l,j,l+1,r,u,d);
if (a[r][j])
kr+=fine(r,j,l,r-1,u,d);
}
h=min(h,s[l+1][r][u][d]+kl);
h=min(h,s[l][r-1][u][d]+kr);
}

if (u<d){
int ku=0,kd=0;
for (int j=l; j<=r; ++j){
if (a[j][u])
ku+=fine(j,u,l,r,u+1,d);
if (a[j][d])
kd+=fine(j,d,l,r,u,d-1);
}
h=min(h,s[l][r][u+1][d]+ku);
h=min(h,s[l][r][u][d-1]+kd);
}

s[l][r][u][d]=h;
//cout<<l<<' '<<r<<' '<<u<<' '<<d<<"->"<<h<<"\n";
}

cout<<s[0][n-1][0][m-1]<<"\n";
}

int main(){
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
din();
return 0;
}

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static int dist(int x1,int y1,int x2,int y2) {
int x = x1 - x2, y = y1 - y2;
if (x < 0) {
x = -x;
}
if (y < 0) {
y = -y;
}
return (x > y)?x:y;
}
public static int cost(int x,int y,int x1,int y1,int x2,int y2) {
int d1 = dist(x,y,x1,y1), d2 = dist(x,y,x2,y2);
return (d1 > d2)?d1:d2;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt(), col = scanner.nextInt();
int [][] a = new int [row][col];
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
a[i][j] = scanner.nextInt();

}
}
int[][][][] result = new int [row][col][row][col];
for (int x1 = row - 1; x1 >= 0; --x1) {
for (int y1 = col - 1; y1 >= 0; --y1) {
for (int x2 = x1; x2 < row; ++x2) {
for (int y2 = (x1 == x2)?(y1 + 1):y1; y2 < col; ++y2) {
result[x1][y1][x2][y2] = 1000000000;
if (x1 < x2) {
int temp = result[x1 + 1][y1][x2][y2];
for (int y = y1; y <= y2; ++y) {
if (a[x1][y] == 1) {
temp += cost(x1, y, x1 + 1, y1, x2, y2);
}
}

if (result[x1][y1][x2][y2] > temp) {

result[x1][y1][x2][y2] = temp;
}

temp = result[x1][y1][x2 - 1][y2];
for (int y = y1; y <= y2; ++y) {
if (a[x2][y] == 1) {
temp += cost(x2, y, x1 ,y1, x2 - 1, y2);

}
}
if (result[x1][y1][x2][y2] > temp) {

result[x1][y1][x2][y2] = temp;
}
}
if (y1 < y2) {
int temp = result[x1][y1 + 1][x2][y2];
for (int x = x1; x <= x2; ++x) {
if (a[x][y1] == 1) {
temp += cost(x, y1, x1, y1 + 1, x2, y2);
}
}
if (result[x1][y1][x2][y2] > temp) {

result[x1][y1][x2][y2] = temp;
}
temp = result[x1][y1][x2][y2 - 1];
for (int x = x1; x <= x2; ++x) {
if (a[x][y2] == 1) {
temp += cost(x, y2, x1, y1, x2, y2 - 1);
}
}
if (result[x1][y1][x2][y2] > temp) {

result[x1][y1][x2][y2] = temp;
}

}

}
}
}
}
System.out.println(result[0][0][row - 1][col - 1]);

}
}

In C :

#include <stdio.h>

int a[55][55][55][55],b[55][55],t,i,j,k,l,m,n,r,c,dy,dx;
int z[55][55][55],v[55][55][55],u;

int main()
{

scanf("%d %d", &r, &c);

for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);

for(i=0;i<c;i++)
for(j=0;j<r;j++)
{
z[j][j][i] = b[j][i];
for(k=j+1;k<r;k++) z[j][k][i] = z[j][k-1][i] + b[k][i];
}

for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
v[i][j][j] = b[i][j];
for(k=j+1;k<c;k++) v[i][j][k] = v[i][j][k-1] + b[i][k];
}

/*
for(i=0;i<r;i++)
for(k=0;k<r;k++)
for(j=0;j<c;j++)
for(l=j;l<c;l++)
*/

u = -1;

for(dy=0;dy<r;dy++)
for(dx=0;dx<c;dx++)
for(i=0;i+dy<r;i++)
for(j=0;j+dx<c;j++)
/*
for(dy=0;dy<2;dy++)
for(dx=0;dx<2;dx++)
for(i=0;i+dy<2;i++)
for(j=0;j+dx<2;j++)
*/
{
k = i+dy;
l = j+dx;

m = 2000000000;

if(k==i && l==j) m = 0;

if(k-i >= l-j && k-i > 0)
{
t = a[i+1][j][k][l];
if(v[k][j][l])
{
t +=  v[i][j][l]*(k-i);
if(t<m) m = t;
}

t = a[i][j][k-1][l];
if(v[i][j][l])
{
t +=  v[k][j][l]*(k-i);
if(t<m) m = t;
}
}

//    printf("prve m %d\n",m);

if(k-i <= l-j && l-j > 0)
{
t = a[i][j+1][k][l];
if(z[i][k][l])
{
t +=  z[i][k][j]*(l-j);
if(t<m) m = t;
}

t = a[i][j][k][l-1];
if(z[i][k][j])
{
t +=  z[i][k][l]*(l-j);
if(t<m) m = t;
}
}

a[i][j][k][l] = m;

if(m!=2000000000 && m > u) u = m;

// if(m)
//      printf("%d %d %d %d -> %d\n",i,j,k,l,m);
}
/*
for(i=0;i<r;i++)
for(j=0;j<c;j++)
for(k=j;k<c;k++)
printf("v %d %d %d -> %d\n", i,j,k, v[i][j][k]);
*/

/*
for(j=0;j<c;j++)
for(i=0;i<r;i++)
for(l=i;l<r;l++)
printf("z %d %d %d -> %d\n", i,l,j, z[i][l][j]);
*/

//k = a[0][0][r-1][c-1];

if(u<0) u = 0;

printf("%d\n",u);

return 0;
}

In Python3 :

r, c = list(map(int, input().strip().split()))
n = max(r, c)
g = [[0]*n for i in range(n)]
for i in range(r):
bs = list(map(int, input().strip().split()))
for j in range(c):
g[i][j] = bs[j]

x = [[0]*(n+1) for i in range(n+1)]
for i in range(n):
for j in range(n):
x[i+1][j+1] = x[i+1][j] + x[i][j+1] - x[i][j] + g[i][j]

fs  = g
fz  = [[0]*n for i in range(n)]
ans = [[0]*n for i in range(n)]
anz = [[0]*n for i in range(n)]
for d in range(1,n):
for i in range(n-d):
I = i + d + 1
for j in range(n-d):
J = j + d + 1
total = fz[i][j] = x[I][J] - x[i][J] - x[I][j] + x[i][j]
anz[i][j] = min(
ans[i  ][j  ] + d*(total - fs[i  ][j  ]),
ans[i  ][j+1] + d*(total - fs[i  ][j+1]),
ans[i+1][j  ] + d*(total - fs[i+1][j  ]),
ans[i+1][j+1] + d*(total - fs[i+1][j+1]),
)
ans, anz = anz, ans
fs,  fz =  fz,  fs

print (ans[0][0])```
```

## Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

## Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

## Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty