# 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;

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 = 1;
for(int i = 1 ; i <= N ; i++) {
binom[i] = 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;
Xmax = X;
Ymin = Y;
Ymax = Y;
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 = make_pair(0, Xmin);
dude = make_pair(0, Xmax);
dude = make_pair(1, Ymin);
dude = 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;
invfact = new long;
fact = 1;
invfact = 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];
for (int i = 0; i < N; i++) {
p[i] = new int;
p[i] = in.nextInt();
p[i] = in.nextInt();
if (p[i] < sx) sx = p[i];
if (p[i] > ex) ex = p[i];
if (p[i] < sy) sy = p[i];
if (p[i] > ey) ey = p[i];
}
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;
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 = -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;
long long ifac;

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=fac=ifac=ifac=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)```
```

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func