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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值