# Covering the stains

### Problem Statement :

```There is a huge blanket on your bed but unfortunately it has N stains. You cover them using a single, rectangular silk cloth. The silk is expensive, which is why the rectangular piece needs to have the least area as possible. You love this blanket and decide to minimize the area covering the stains. You buy some cleaning liquid to remove the stains but sadly it isn't enough to clean all of them. You can just remove exactly K stains. The rest of the stains need to be covered using a single, rectangular fragment of silk cloth.

Let X denote the area of the smallest possible silk cloth that may cover all the stains originally. You need to find the number of different ways in which you may remove K stains so that the remaining N-K stains can be covered with silk of area strictly less than X (We are looking for any configuration that will reduce the cost).

Assume that each stain is a point and that the rectangle is aligned parallel to the axes.

Input Format

The first line contains two integers N (1<=N<=1000) and K (0<=K<=N). Next follow N lines, one for each stain. Each line contains two integers in the form 'X Y', (0<=X,Y<100000), the coordinates of each stain into the blanket. Each pair of coordinates is unique.

Output Format

Output a single integer. The remainder of the division by 1000000007 of the answer.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int MOD = 1000000007, MAXN = 1005;

int binom[MAXN][MAXN];

int N, K;
int X[MAXN], Y[MAXN];
int Xmin, Xmax, Ymin, Ymax;
pair<int, int> dude[4];

inline int calc(vector<pair<int, int> > vec) {
int num = 0;
for(int i = 0 ; i < N ; i++) {
for(int j = 0 ; j < (int)vec.size() ; j++) {
if (vec[j].first == 0 && X[i] == vec[j].second) {
num++;
break;
}	else if (vec[j].first == 1 && Y[i] == vec[j].second) {
num++;
break;
}
}
}
if (num > K) {
return 0;
}	else {
return binom[N - num][K - num];
}
}

int main() {
scanf("%d %d", &N, &K);
binom[0][0] = 1;
for(int i = 1 ; i <= N ; i++) {
binom[i][0] = 1;
for(int j = 1 ; j <= i ; j++) {
binom[i][j] = (binom[i - 1][j] + binom[i - 1][j - 1]) % MOD;
}
}

for(int i = 0 ; i < N ; i++) {
scanf("%d %d", &X[i], &Y[i]);
}
Xmin = X[0];
Xmax = X[0];
Ymin = Y[0];
Ymax = Y[1];
for(int i = 1 ; i < N ; i++) {
Xmin = min(Xmin, X[i]);
Xmax = max(Xmax, X[i]);
Ymin = min(Ymin, Y[i]);
Ymax = max(Ymax, Y[i]);
}
dude[0] = make_pair(0, Xmin);
dude[1] = make_pair(0, Xmax);
dude[2] = make_pair(1, Ymin);
dude[3] = make_pair(1, Ymax);
int ans = 0;
vector<pair<int,int> > v;
for(int i = 0 ; i < 4 ; i++) {
if ((mask >> i) & 1) {
v.push_back(dude[i]);
}
}
if (sgn & 1) {
ans = (ans + calc(v)) % MOD;
}	else {
ans = (ans - calc(v)) % MOD;
}
}
ans = (ans + MOD) % MOD;
ans = (ans + MOD) % MOD;
printf("%d\n", ans);
return 0;
}

In Java :

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class Solution {
private static PrintWriter out;
public static long mod = 1000000007;
public static long mod_exp(long b, long e) {
long r = 1;
while (e > 0) {
if ((e & 1) == 1) r = (r * b) % mod;
b = (b * b) % mod;
e >>= 1;
}
return r;
}

public static long[] fact, invfact;

public static void main(String[] args) throws IOException {
out = new PrintWriter(System.out, true);

fact = new long[1001];
invfact = new long[1001];
fact[0] = 1;
invfact[0] = 1;
for (int i = 1; i <= 1000; i++) {
fact[i] = (i * fact[i - 1]) % mod;
invfact[i] = mod_exp(fact[i], mod - 2);
}

int sx = 1 << 29, sy = 1 << 29, ex = -1, ey = -1;
int N = in.nextInt(), K = in.nextInt();
int[][] p = new int[N][2];
for (int i = 0; i < N; i++) {
p[i] = new int[2];
p[i][0] = in.nextInt();
p[i][1] = in.nextInt();
if (p[i][0] < sx) sx = p[i][0];
if (p[i][0] > ex) ex = p[i][0];
if (p[i][1] < sy) sy = p[i][1];
if (p[i][1] > ey) ey = p[i][1];
}
int[] b = new int[] {sx, ex, sy, ey};
int[] id = new int[] {0, 0, 1, 1};
long total = 0;

int count = 0;
for (int i = 0; i < N; i++) {
boolean ok = false;
for (int j = 0; j < 4; j++) {
if (set(mask, j) && p[i][id[j]] == b[j])
ok = true;
}
if (ok) count++;
}
long sgn = Integer.bitCount(mask) % 2 == 1 ? 1 : (mod - 1);
total = (total + sgn * binom(N - count, K - count)) % mod;
}

out.println(total);
out.close();
System.exit(0);
}

public static boolean set(int m, int i) {
return ((m >> i) & 1) == 1;
}

public static long binom(int N, int K) {
if (K < 0 || K > N) return 0;
return fact[N] * invfact[K] % mod * invfact[N - K] % mod;
}

final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;

din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
}

public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
}

public String readLine() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}

public int nextInt() throws IOException {
int ret = 0;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

public long nextLong() throws IOException {
long ret = 0;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

public double nextDouble() throws IOException {
double ret = 0, div = 1;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}

private void fillBuffer() throws IOException {
buffer[0] = -1;
}

private byte read() throws IOException {
fillBuffer();
return buffer[bufferPointer++];
}

public void close() throws IOException {
if (din == null)
return;
din.close();
}
}

}

In C :

#include <stdio.h>
#include <stdlib.h>
#define MOD 1000000007
long long modPow(long long a,int x);
long long modInverse(long long a);
long long C(int n,int k);
long long fac[1001];
long long ifac[1001];

int main(){
int N,K,maxx=-1,maxy=-1,minx=-1,miny=-1,u=0;
int d=0,l=0,r=0,ul=0,ur=0,dl=0,dr=0,i,t;
int *x,*y;
long long ans=0;
fac[0]=fac[1]=ifac[0]=ifac[1]=1;
for(i=2;i<1001;i++){
fac[i]=i*fac[i-1]%MOD;
ifac[i]=modInverse(fac[i]);
}
scanf("%d%d",&N,&K);
x=(int*)malloc(N*sizeof(int));
y=(int*)malloc(N*sizeof(int));
for(i=0;i<N;i++){
scanf("%d%d",x+i,y+i);
if(minx==-1 || x[i]<minx)
minx=x[i];
if(miny==-1 || y[i]<miny)
miny=y[i];
if(maxx==-1 || x[i]>maxx)
maxx=x[i];
if(maxy==-1 || y[i]>maxy)
maxy=y[i];
}
for(i=0;i<N;i++){
if(x[i]==minx)
l++;
if(x[i]==maxx)
r++;
if(y[i]==miny)
d++;
if(y[i]==maxy)
u++;
if(x[i]==minx && y[i]==miny)
dl++;
if(x[i]==minx && y[i]==maxy)
ul++;
if(x[i]==maxx && y[i]==miny)
dr++;
if(x[i]==maxx && y[i]==maxy)
ur++;
}
if(N==1){
if(K==0)
printf("0");
else
printf("1");
}
else if(minx==maxx || miny==maxy){
if(K>=1)
ans=(ans+C(N-1,K-1)*2%MOD)%MOD;
if(K>=2)
ans=(ans-C(N-2,K-2)+MOD)%MOD;
printf("%lld",ans);
}
else{
if(K>=u)
ans=(ans+C(N-u,K-u))%MOD;
if(K>=d)
ans=(ans+C(N-d,K-d))%MOD;
if(K>=l)
ans=(ans+C(N-l,K-l))%MOD;
if(K>=r)
ans=(ans+C(N-r,K-r))%MOD;
t=u+d;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
t=u+l-ul;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
t=u+r-ur;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
t=d+l-dl;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
t=d+r-dr;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
t=l+r;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
t=u+d+l-ul-dl;
if(K>=t)
ans=(ans+C(N-t,K-t))%MOD;
t=u+d+r-ur-dr;
if(K>=t)
ans=(ans+C(N-t,K-t))%MOD;
t=u+r+l-ul-ur;
if(K>=t)
ans=(ans+C(N-t,K-t))%MOD;
t=r+d+l-dl-dr;
if(K>=t)
ans=(ans+C(N-t,K-t))%MOD;
t=u+d+l+r-ul-ur-dl-dr;
if(K>=t)
ans=(ans-C(N-t,K-t)+MOD)%MOD;
printf("%lld",ans);
}
return 0;
}
long long modPow(long long a,int x){
long long res = 1;
while(x>0){
if(x%2)
res=res*a%MOD;
a=a*a%MOD;
x>>=1;
}
return res;
}
long long modInverse(long long a){
return modPow(a,MOD-2);
}
long long C(int n,int k){
return fac[n]*ifac[k]%MOD*ifac[n-k]%MOD;
}

In Python3 :

def stain_combos(n, k):
p = 1000000007
if k < 0:
return 0
out = 1
for i in range(n-k+1, n+1):
out = (out * i) % p
denom = 1
for i in range(1, k+1):
denom = (denom * i) % p
denom = pow(denom, p-2, p)
return (out*denom)%p

def solve(n, k, stains):
print(size_decrease(stains, k))

from itertools import combinations
from functools import reduce

def size_decrease(stains, k):
min_x = min([p.x for p in stains])
max_x = max([p.x for p in stains])
min_y = min([p.y for p in stains])
max_y = max([p.y for p in stains])
top = {p for p in stains if p.x==min_x}
bot = {p for p in stains if p.x==max_x}
left = {p for p in stains if p.y==min_y}
right = {p for p in stains if p.y==max_y}

out = 0
for i in range(1, 5):
for sides in combinations([top, bot, left, right], i):
removed = reduce(lambda x,y: x.union(y), sides)
length = len(removed)
out += ((-1)**(i+1)) * stain_combos(len(stains)-length, k-length)
return out%1000000007

from collections import namedtuple

point = namedtuple('point', ['x','y'])

n, k = [int(x) for x in input().split(" ")]

stains = []

for _ in range(n):
stains.append(point(*[int(number) for number in input().split(" ")]))

solve(n, k, stains)```
```

## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

## Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

## Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For