hdoj4268Alice and Bob【贪心+二分】

Alice and Bob

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4106    Accepted Submission(s): 1275


Problem Description
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N different rectangular cards respectively. Alice wants to use his cards to cover Bob's. The card A can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. As the best programmer, you are asked to compute the maximal number of Bob's cards that Alice can cover.
Please pay attention that each card can be used only once and the cards cannot be rotated.
 

Input
The first line of the input is a number T (T <= 40) which means the number of test cases. 
For each case, the first line is a number N which means the number of cards that Alice and Bob have respectively. Each of the following N (N <= 100,000) lines contains two integers h (h <= 1,000,000,000) and w (w <= 1,000,000,000) which means the height and width of Alice's card, then the following N lines means that of Bob's.
 

Output
For each test case, output an answer using one line which contains just one number.
 

Sample Input
  
  
2 2 1 2 3 4 2 3 4 5 3 2 3 5 7 6 8 4 1 2 5 3 4
 

Sample Output
  
  
1 2
 

Source
 

刚开始做这题时就想的是对两个人的卡分别按照h从小到大排序h相同就按照w从小到大排然后对每一个Bob的二分查找能覆盖它的在x接近的情况下y最接近的。

然后测试样列过了就提交结果一直wa就是不知道那错了最后无奈百度了一下别人写的最终终于想到了bug

我写的wa代码

这组数据过不去

1

2

4 22

5 7

3 6

3 21

正确答案是2

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100010;
struct Node{
    int x,y;
}A[maxn],B[maxn];
bool vis[maxn]; 
bool cmp(Node a,Node b){
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    int t,i,j,k,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i=0;i<n;++i){
            scanf("%d%d",&A[i].x,&A[i].y);
        }
        for(i=0;i<n;++i){
            scanf("%d%d",&B[i].x,&B[i].y);
        }memset(vis,false,sizeof(vis));
        sort(A,A+n,cmp);
        sort(B,B+n,cmp);
        int ans=0,now=0,x;
        for(i=0;i<n;++i){
            int l=now,r=n-1,mid;
            while(l<=r){
                mid=(l+r)>>1;
                if(A[mid].x>=B[i].x)
                    r=mid-1;
                else {
                    l=mid+1;
                    x=mid;
                }
            }
            if(A[x].x>B[i].x){
                continue;
            }now=x;
            for(j=x;j<n;++j){
                if(!vis[j]&&A[j].x>=B[i].x&&A[j].y>=B[i].y){
                    vis[j]=1;
                    ans++;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

ac代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
const int maxn=100010;
struct Node{
    int x,y;
}A[maxn],B[maxn];
bool cmp(Node a,Node b){
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    int t,i,j,k,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i=0;i<n;++i){
            scanf("%d%d",&A[i].x,&A[i].y);
        }
        for(i=0;i<n;++i){
            scanf("%d%d",&B[i].x,&B[i].y);
        }
        int ans=0;
        sort(A,A+n,cmp);
        sort(B,B+n,cmp);
        multiset<int>M;
        for(int a=0,b=0;a<n;++a){
            for(;b<n&&B[b].x<=A[a].x;++b)
                M.insert(B[b].y);
            if(!M.empty()){
                multiset<int>::iterator it;
                it=M.upper_bound(A[a].y);
                if(it!=M.begin()){
                    it--;
                    ans++;
                    M.erase(it);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值