hdu 4268 Alice and Bob(贪心)

来源:互联网 发布:fc2免费视频域名 编辑:程序博客网 时间:2024/05/17 04:44

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4268

/*   贪心,先对高度排序(升序),接着宽度也排序(升序),接下来对于Alice的每个卡找到Bob所有   符合条件的卡片宽度最小的那个(因为高度已经符合了)。*/#include<iostream>#include <stdio.h>#include<string.h>#include<algorithm>#include <set>using namespace std;const int N=100010;struct node{    int x,y,t;    friend bool operator<(const node &a,const node &b){        if(a.x ==b.x && a.y == b.y)            return a.t>b.t;        return a.x< b.x || (a.x == b.x&& a.y < b.y);    }};multiset<int> S;node a[N<<1];const int Inf=1<<31;int T,n;int main() {    scanf("%d",&T);    while(T--) {        scanf("%d",&n);        for(int i=0;i<n<<1;i++)            scanf("%d%d",&a[i].x,&a[i].y);        for(int i=0;i<n;i++)            a[i].t=0;        for(int i=n;i<n<<1;i++)            a[i].t=1;        sort(a,a+2*n);        //for(int i = 0;i <n<<1;i++)        //  printf("%d %d %d\n",a[i].x,a[i].y,a[i].t);        S.clear();        int ans=0;        for(int i=0;i<2*n;i++) {            if(a[i].t==1)S.insert(a[i].y);            else {                if(!S.empty()){                    if(*S.begin()<=a[i].y){                        __typeof(S.begin())it=S.upper_bound(a[i].y);                        it--;                        ans++;                        S.erase(it);                    }                }            }        }        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击