题目描述

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. alpha-咸鱼冲刺day4

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 QAQ具体工作量没啥进展.但是前后端终于可以数据交互了!.. 四,问题困难 日常啥都不会,百度真心玩一年. 还得自学nodejs ...

  2. Alpha冲刺No.6

    站立式会议 继续页面设计 在安卓内构件数据库相应类 解决摄像头.照片的使用的异常问题 二.实际项目进展 页面设计完成百分80 类架构完成 在虚拟机中,能够完成摄像头的调用和程序的使用 三.燃尽图 四. ...

  3. 解决python中flask_sqlalchemy包安装失败的问题

    在进行flask_sqlalchemy包的下载安装时出现以下问题: 由图片可看出是编码转换出了问题,找到pip\compat_init_.py文件,打开它并查看第73行,将代码做如下更改并保存: 问题 ...

  4. 团队作业7——第二次项目冲刺(Beta版本12.04)

    1.当天站立式会议照片 本次会议内容:1:每个人汇报自己完成的工作.2:组长分配各自要完成的任务. 2.每个人的工作 黄进勇:项目整合,后台代码. 李勇:前台界面优化. 何忠鹏:数据库模块. 郑希彬: ...

  5. python 人工智能资源推荐

    原创 2017-06-05 玄魂工作室 玄魂工作室 我翻了翻我自己曾经看过的书,还是放弃了推荐.原因很简单,我对这个领域并不是很熟悉,我来推荐资源有点误人子弟.so,简单推点其他人建议给我的内容,希望 ...

  6. jQuery兼容浏览器IE8方法

    在维护公司网站的时候,发现在IE8下jquery会报对象不支持此属性或方法.缺少对象的错误:  在其他浏览器就可以正常运行,当前使用的jquery版本是3.1.1,查资料发现jquery从2.0开始不 ...

  7. 快速搭建fabric-v1.1.0的chaincode开发环境

    本文参考了fabric官方文档:http://hyperledger-fabric.readthedocs.io/en/latest/peer-chaincode-devmode.html?highl ...

  8. jquery实现对div的拖拽功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 新概念英语(1-125)Tea for two

    Does Susan have tea by herself?A:Can't you come in and have tea now,Peter? Not yet.B:I must water th ...

  10. C#微信公众号开发——access_token的获取

    access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_toke ...