贪心+容器 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 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

题义大概就是A有一些卡片,B有一些卡片,A的卡片能覆盖B卡片最多多少张(只有长和宽同时大于等于才行)

#include <bits/stdc++.h>
using namespace std;
#define Maxn 100010

struct Node{
    int x,y,id;
};
Node G[Maxn*2];

bool cmp(Node n1,Node n2){
    if(n1.x == n2.x && n2.y == n2.y) return n1.id > n2.id;
    //这里注意排序方式
    if(n1.x != n2.x) return n1.x > n2.x;
    return n1.y > n2.y;
}

int main()
{
    int N;
    cin >> N;
    while(N--){
        int n,t;
        cin >> n;
        for(int i = 0; i <(n<<1); i++){
            scanf("%d%d",&G[i].x,&G[i].y);
            if(i < n){
                G[i].id = 1;
            }else{
                G[i].id = 0;
            }
        }
        sort(G,G+(n<<1),cmp);
        multiset<int>s;
        int cnt = 0;
        for(int i = 0; i < (n<<1); i++){
            // 这里用了贪心的方法,因为已经排序了,按照从x从小到大的顺序
            // 如果是A,放入容器,如果是B,就从A中找到比B大的中的最小的(贪心)
            if(G[i].id){
                s.insert(G[i].y);
            }else{
                multiset<int>::iterator it = s.lower_bound(G[i].y);
                if(it == s.end() || *it < G[i].y){
                    // 这步判断很重要
                    continue;
                }else{
                    cnt++;
                    s.erase(it);
                    // 找到就删掉,以后都不再用了
                }
            }
        }
        printf("%d\n",cnt);
    }
}

  

时间: 2024-08-30 11:34:55

贪心+容器 hdu4268的相关文章

LeetCode----盛最多水的容器「贪心」

题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点?(i,?ai) . 在坐标内画 n 条垂直线,垂直线 i?的两个端点分别为?(i,?ai) 和 (i, 0). 找出其中的两条线,使得它们与?x?轴共同构成的容器可以容纳最多的水. 图片引用自LeetCode 示例 输入: [1,8,6,2,5,4,8,3,7] 输出: 49 思路 可以直接看成求最大矩形面积,左边下标记为 l,右边下标记为 r 则面积 S = Min(a[l], a[r]) * (r-l) 先尽

HDU4268 Alice and Bob 【贪心】

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

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 Bo

【BZOJ1110】[POI2007]砝码Odw 贪心

[BZOJ1110][POI2007]砝码Odw Description 在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作.公司有一些固定容量的容器可以装这些砝码.他们想装尽量多的砝码以便搬运,并且丢弃剩下的砝码.每个容器可以装的砝码数量有限制,但是他们能够装的总重量不能超过每个容器的限制.一个容器也可以不装任何东西.任何两个砝码都有一个特征,他们的中总有一个的重量是另外一个的整数倍,当然他们也可能相等. Input 第一行包含两个数n和m.表示容器的数量以

【NOI2015】荷马史诗[Huffman树+贪心]

#130. [NOI2015]荷马史诗 统计 描述 提交 自定义测试 追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛>和<伊利亚特>组成的鸿篇巨制<荷马史诗>实在是太长了,Allison 想通过一种编码方式使得它变得短一些. 一部<荷马史诗>中有 nn 种不同的单词,从 11 到 nn 进行编号.其中第 ii 种单词出现的总

URAL 2026 Dean and Schedule 贪心、双端队列(deque)、队列(queue)

C - Dean and Schedule Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2026 Description A new academic year approaches, and the dean must make a schedule of classes for first-year students. Ther

学习日志---贪心算法

贪心算法: 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解.贪心算法不是对所有问题都能得到整体最优解,但对范围相当广泛的许多问题他能产生整体最优解或者是整体最优解的近似解.       简单来说就是从一点开始,以每一步最优的方式去寻求最优解,但不保证是全局最优,只保证是每步最优. 例子: 有十一个快件,每个快件有送货的起始时间,要求用最少的车,运送完所有的快件.快件1:1:00-4:00快件2

【贪心专题】HDU 1050 D - Moving Tables (移桌子)

链接:click here~ 题意:在一个长走廊里搬桌子,走廊的两侧都是房间,把桌子从一个房间搬到另外一个房间,走廊的宽度只能允许一个桌子通过,每次搬桌子需要10分钟(每一次允许再不交叉的走廊中同时搬桌子),问最少多长时间搬完! [解题思路] 这题也是想了好久,关键点:由于房间是奇偶分开的,所以先把他们换成连续的自然数,即n=(n-1)/2.这样对门的就变成一个编号了,这点是关键,感觉好厉害(因为房间是对称分布的,需要注意一下奇偶的情况.)利用房间号分割走廊,贪心实现求走廊的最大重叠数 这里使用

HihoCoder 1053 : 居民迁移 二分+贪心+双指针

居民迁移 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描述 公元2411年,人类开始在地球以外的行星建立居住点.在第1326号殖民星上,N个居住点分布在一条直线上.为了方便描述,我们设第i个居住点的位置是Xi,其中居住着Yi位居民.随着冬季的到来,一些人口较多的居住点的生态循环系统已经开始超负荷运转.为了顺利度过严冬,殖民星上的居民一致同意通过转移到人口较少的居住点来减轻人口众多的居住点的负荷. 遗憾的是,1326殖民星的环境非常恶劣.在冬季到来前,每个居民点的居民最远