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 :



title-img


                            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;
	for(int mask = 1 ; mask < 16 ; mask++) {
		int sgn = __builtin_popcount(mask);
		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 Reader in;
  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 {
    in = new Reader();
    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;
    for (int mask = 1; mask < 1 << 4; mask++) {
      
      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;
  }

  static class Reader {
    final private int BUFFER_SIZE = 1 << 16;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Reader() {
      din = new DataInputStream(System.in);
      buffer = new byte[BUFFER_SIZE];
      bufferPointer = bytesRead = 0;
    }

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

    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;
      byte c = read();
      while (c <= ' ')
        c = read();
      boolean neg = (c == '-');
      if (neg)
        c = read();
      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;
      byte c = read();
      while (c <= ' ')
        c = read();
      boolean neg = (c == '-');
      if (neg)
        c = read();
      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;
      byte c = read();
      while (c <= ' ')
        c = read();
      boolean neg = (c == '-');
      if (neg)
        c = read();
      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 {
      bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
      if (bytesRead == -1)
        buffer[0] = -1;
    }

    private byte read() throws IOException {
      if (bufferPointer == bytesRead)
        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)
                        








View More Similar Problems

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →

Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

View Solution →

Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy

View Solution →