hdu4268 multiset应用 贪心

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

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
/**
hdu 4268  multiset应用
题目大意:一牌,如果他的长和宽都分别不小于另一张,那么这张牌就可以覆盖另一张,问在A的牌中,有多少
          可以覆盖B的牌(一张牌只能覆盖一张,并且长宽已给出,并不一定长度大的的就是长)
解题思路:这道题的数据如果小一点,完全可以用二分图来做。对AB的牌进行排序长递增,然后枚举A的牌,把所有B
          的牌大于A的长的加入multiset里,利用lower_bound()找出宽最小的一个,然后删除该元素。复杂度O(n*logn)
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
const int maxn=100005;

int n;
multiset <int> s;

struct note
{
    int x,y;
    bool operator < (const note &other)const
    {
        return x<other.x;
    }
}a[maxn],b[maxn];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&b[i].x,&b[i].y);
        }
        sort(a,a+n);
        sort(b,b+n);
        s.clear();
        int k=0,ans=0;
        for(int i=0;i<n;i++)
        {
            while(a[i].x>=b[k].x&&k<n)
            {
                 s.insert(b[k].y);
                 k++;
            }
            if(s.empty())continue;
            multiset<int>::iterator it=s.lower_bound(a[i].y);
            if(it==s.end()||a[i].y<*it)it--;
            if(*it<=a[i].y)
            {
                ans++;
                s.erase(it);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-03 18:55:08

hdu4268 multiset应用 贪心的相关文章

【CF706D】Vasiliy&#39;s Multiset Trie+贪心

题目大意:需要维护一种数据结构,支持以下三种操作:插入一个数,删除一个数,查询该数据结构中的数异或给定数的最大值. 题解:如果没有删除操作就是一个标准的 Trie 上贪心求最大异或和问题.现在需要支持删除操作,因此,在树上每个节点维护一个额外的标记,表示有多少个数的某一位经过当前节点.插入操作依然只需修改树上一条链,而删除一个数时,同样需要将这条链上的标记值减 1 即可.这时便可以根据经过的每个点标记值是否为 0 进行贪心操作. 代码如下 #include <bits/stdc++.h> us

HDU 4268 Alice and Bob(贪心+multiset)

HDU 4268 题意:Alice与Bob在玩卡片游戏,他们每人有n张卡片,若Alice的一张卡片长与宽都不小于Bob的一张卡片,则Bob的卡片就会被盖住,一张卡片只可以使用一次,且不可旋转求Alice最多可以盖住多少张Bob的卡片. 思路:记录两人卡片情况,并按照长度将两人卡片分别降序排序.遍历两人的卡片,将长度小于Alice的卡片长度的Bob卡片的宽度插入multiset中,在multiset中找到小于等于Alice卡片宽度的第一个数,将这个数给消去且答案+1.//贪心法自行发挥即可. co

HDU 4268 Alice and Bob(贪心+Multiset的应用)

 题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形可以覆盖另一个矩形的条件的是,本身长度大于等于另一个矩形,且宽度大于等于另一个矩形,矩形不可旋转,问你Alice最多能覆盖Bob的几个矩形? 思路:贪心,先按照h将Alice和Bob的矩形排序,对于Alice的每个矩形,如果Bob的矩形的h小于Alice的h,将Bob的w插入到集合中. 然后,在集合中找到不大于Alice矩形d的最大的Bob的d,那么这样做肯定是最优的. #include<cstdio> #include<

CodeForces 706D Vasiliy&#39;s Multiset (字典树查询+贪心)

题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大值为多少 析:这是一个字典树的应用,不过确实没看出来....主要思想是这样,先用10进制数,转成二进制数,记下每个结点的0,1的个数,这样增加和删除,就是对01的删除, 剩下的就是查询,那么尽量让0和1XOR是最大的,所以,对于给定数,我们要去尽量他的XOR数,如果找到就加上,找不到,就找下一个.这

hdu 4268 Alice and Bob(贪心+multiset+二分)

Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3511    Accepted Submission(s): 1129 Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In thi

hdu 4268 multiset+贪心

Alice和Bob有n个长方形,有长度和宽度,一个矩形可以覆盖另一个矩形的条件的是,本身长度大于等于另一个矩形,且宽度大于等于另一个矩形,矩形不可旋转,问你Alice最多能覆盖Bob的几个矩形? 1 /* 2 HDU 4268 3 贪心+STL 4 */ 5 6 #include<stdio.h> 7 #include<math.h> 8 #include<iostream> 9 #include<set> 10 #include<algorithm&

贪心+容器 hdu4268

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 hei

ZOJ 3963 Heap Partition(multiset + stl自带二分 + 贪心)题解

题意:给你n个数字s1~sn,要你把它们组成一棵棵二叉树,对这棵二叉树来说,所有节点来自S,并且父节点si<=子节点sj,并且i<j,问你树最少几棵二叉数.树 思路:贪心.我们往multiset加还能加子节点的节点,二分查找一个个大于等于当前插入节点的节点,然后插入,若找不到则重新建一棵树. 没想到set自带lower_bound(),第一次迭代器遍历TLE就想着手动写二分...然后发现自带二分... 代码: #include<iostream> #include<stdio

cf1061D 贪心+multiset 好题!

cf上的思维题真好! 本题是在模拟的基础上贪心即可:将n段时间按照左端点(右端点为第二关键字)从小到大排序,然后遍历每一个时间段. 对于每一个时间段[li,ri],先找到multiset中最靠近li但在li左侧的r, 如果没有这样的r,即[li,ri]是当前最靠左的,那就需要新加一台电视机,然后把ri加入multiset 如果找到这样的r,那就进行一次判断,如果从r到li等待时间中浪费的钱大于等于新加一台电视的钱,那就新加一台电视,把ri加入multiset 否则就接着r往下看,那就把r从mul