Stars_构造+尺取法

Stars

Time Limit: 15000 ms Memory Limit: 65536 KiB

Problem Description

There are N (1 ≤ N ≤ 400) stars in the sky. And each of them has a unique coordinate (x, y) (1 ≤ x, y ≤ N). Please calculate the minimum area of the rectangle (the edges of the rectangle must be parallel to the X, Y axes) that can cover at least K (1 ≤ K ≤ N) stars. The stars on the borders of the rectangle should not be counted, and the length of each rectangle’s edge should be an integer.

Input

Input may contain several test cases. The first line is a positive integer T (T ≤ 10), indicating the number of test cases below.

For each test cases, the first line contains two integers N, K, indicating the total number of the stars and the number of stars the rectangle should cover at least.

Each of the following N lines contains two integers x, y, indicating the coordinate of the stars.

Output

For each test case, output the answer on a single line.

Sample Input

2
1 1
1 1
2 2
1 1
1 2

Sample Output

1
2

Hint

Source

“浪潮杯”山东省第六届ACM大学生程序设计竞赛
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>


using namespace std;
#define ll long long
#define all(x) (x).begin(),x.end()
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
#define rep(i,a,n) for(int i=a;i<n;i++)
#define se second
const int inf=0x3f3f3f3f;
const int N=400+10;
int a[N][N];
int v[N],n,k;

int cal(int *l,int *r){
    int st,ed;
    st=ed=0;
    for(int i=0;i<=n;i++)
        v[i]=r[i]-l[i];
    while(ed<=n&&v[ed]<k)
        ed++;
    if(ed>n)return N*N;
    int ret=ed;
    while(ed<=n){
        if(v[ed]-v[st]>=k){
            ret=min(ret,ed-st);
            st++;
        }else ed++;
    }
    return ret;
}



int main(){
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
    freopen("in.txt","r",stdin);

    int t=rd();
    while(t--){
        n=rd(),k=rd();
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++){
            int x=rd(),y=rd();
            a[x][y]=1;
        }
        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        a[i][j]+=a[i][j-1];

        for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        a[i][j]+=a[i-1][j];

        int ans=n*n;
        for(int i=0;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        ans=min(ans,(j-i)*cal(a[i],a[j]));

        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80157649