题目描述

Farmer John's cows are trying to learn to cross the road effectively. Remembering the old "why did the chicken cross the road?" joke, they figure the chickens must be experts on crossing the road, and go off in search of chickens to help them.

农夫约翰的牛们正在尝试去学会高效地穿越马路。熟知经典的笑话“为什么鸡要过马路?”,他们想到鸡一定是过马路的专家,便动身寻找能够帮助它们的鸡。

As it turns out, chickens are very busy creatures and have limited time to help the cows. There are  chickens on the farm (), conveniently numbered , and each chicken  is only willing to help a cow at precisely time . The cows, never in a hurry, have more flexibility in their schedules. There are  cows on the farm (), conveniently numbered , where cow  is able to cross the road between time  and time . Figuring the "buddy system" is the best way to proceed, each cow  would ideally like to find a chicken  to help her cross the road; in order for their schedules to be compatible,  and  must satisfy .

牛们发现,鸡是一种特别繁忙的动物,并且只有一定的时间来帮助它们。农场上共有 只鸡(),十分便利地被编号为, 而且,每只鸡 只有恰好在时间 时才会愿意帮助牛们。而从不慌张的牛们,有更加灵活的时间安排。农场上共有 只牛(),也十分便利地被编号为,牛 在时间 到 之间可以穿过马路。想到“小伙伴系统”是最好的行进方式,每只牛 会理想地愿意找到一只鸡 来帮助她穿过马路;为了是它们的时间表不冲突, 和 必须满足

If each cow can be paired with at most one chicken and each chicken with at most one cow, please help compute the maximum number of cow-chicken pairs that can be constructed.

如果每只牛可以与至少一只鸡结伴,并且每只鸡与至少一只牛,请帮助计算可构造的牛-鸡配对数量的最大值。

输入输出格式

输入格式:

The first line of input contains  and . The next  lines contain , and the next  lines contain  and  () for . The 's, 's, and 's are all non-negative integers (not necessarily distinct) of size at most 1,000,000,000

第一行的输入包括 和。下面的 行包括,再接下来的 行包括 和(),。所有 与 都是非负整数(可能相等),并皆小于等于

输出格式:

Please compute the maximum possible number of cow-chicken pairs.

请计算最大的可行牛-鸡配对数。

样例

输入样例#1:

5 4
7
8
6
2
9
2 5
4 9
0 3
8 13
输出样例#1:

3
把牛往鸡上配对。
用数据结构储存鸡的t[i]。
将牛的y作第一关键字,x做第二关键字排序
之所以这样排,可用贪心理解。
在x相同的情况下,尽可能满足y小的。
在x小y大时,尽可能在后面配。
每一次匹配时,找到大于x的最小t[i],判断是否小于y
数据结构要方便查找,删除,所以用平衡树,此处用STL(不可以用set,要用multiset)
把鸡往牛上配对很麻烦,我的代码只有30分,如果有其他方法,可以留言
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
typedef pair<int,int> pa;
multiset<int> s;
pa cow[];
int c,n,ans;
set<int>::iterator it;
int main()
{int i,j,x,y;
//freopen("3.in","r",stdin);
//freopen("x.out","w",stdout);
cin>>c>>n;
for (i=;i<=c;i++)
{
scanf("%d",&x);
s.insert(x);
}
for (i=;i<=n;i++)
{
scanf("%d%d",&x,&y);
cow[i]=pa(y,x);
}
sort(cow+,cow+n+);
for (i=;i<=n;i++)
{
it=s.lower_bound(cow[i].second);
if (it!=s.end() && *it<=cow[i].first)
{
ans++;
s.erase(it);
}
}
cout<<ans;
}

[USACO17FEB]Why Did the Cow Cross the Road I S的更多相关文章

  1. 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

    P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...

  2. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  3. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  4. 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

    //神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...

  5. [Luogu3659][USACO17FEB]Why Did the Cow Cross the Road I G

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  6. [USACO17FEB]Why Did the Cow Cross the Road III S

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  7. 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

    题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...

  8. [USACO17FEB]Why Did the Cow Cross the Road II S

    题目描述 The long road through Farmer John's farm has  crosswalks across it, conveniently numbered  (). ...

  9. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

随机推荐

  1. GNU/Hurd笔记整理

    patch 0 关于文件锁支持的解决方案,大部分是由Neal Walfield在2001年完成的.这些补丁由Marcus Brinkmann发表,随后被Michael Banck于2002年修改了部分 ...

  2. Exception in thread "main" expected '<document start>', but found BlockMappingStart in 'reader', line 23, column 2: nimbus.host: "master"

    平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6   hadoop 1.2.1 启动storm的时候,遇到这个问题,百度之后,看到大家的解决方案 ...

  3. 从PRISM开始学WPF(六)MVVM(二)Command?

    从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...

  4. node框架koa

    node的两大常见web服务器框架有express和koa,之前已经介绍过express了现在来介绍下koa吧~ koa也是express团队的出品,意在利用es7新出的async来告别"回 ...

  5. Java NIO之选择器

    1.简介 前面的文章说了缓冲区,说了通道,本文就来说说 NIO 中另一个重要的实现,即选择器 Selector.在更早的文章中,我简述了几种 IO 模型.如果大家看过之前的文章,并动手写过代码的话.再 ...

  6. hadoop2.6.0实践:004 启动伪分布式hadoop的进程

    [hadoop@LexiaofeiMaster hadoop-2.6.0]$ start-dfs.shStarting namenodes on [localhost]localhost: start ...

  7. 基于python的统计公报关键数据爬取

    # -*- coding: utf-8 -*- """ Created on Wed Nov 8 14:23:14 2017 @author: 123 "&qu ...

  8. 用于水和水蒸汽物性计算的Python模块——iapws

    无论是火电还是核电,将能量转化为电能的方式主要还是烧开水,即加热水产生高压蒸汽驱动汽轮机做功再发电.在进行热力循环分析.流动传热计算时,需获得水和水蒸汽的物性参数.网上主流的水蒸汽物性计算程序是上海成 ...

  9. python基础——继承与派生、组合

    python基础--继承与派生 1 什么是继承: 继承是一种创建新的类的方式,在python中,新建的类可以继承自一个或者多个父类,原始类成为基类或超累,新建的类成为派生类或子类 1.1 继承分为:单 ...

  10. Django(博客系统):文章内容使用django-ckeditor、文章简介使用django-tinymce

    文章内容使用django-ckeditor 1)安装django-ckeditor pip install django-ckeditorpip install Pillow 2)在settings. ...