hdoj4268Alice and Bob【贪心+二分】

来源:互联网 发布:基于时间的sql注入 编辑:程序博客网 时间:2024/05/16 10:02

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
221 23 42 34 532 35 76 84 12 53 4
 

Sample Output
12
 

Source
2012 ACM/ICPC Asia Regional Changchun Online
 

刚开始做这题时就想的是对两个人的卡分别按照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;} 


0 0
原创粉丝点击