题目描述

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)

    冲刺总结随笔 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.项目预期进展及现实进展 项目预期 ...

  2. 201621123062《java程序设计》第12周作业总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 思维导图: 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车. 2. ...

  3. 20162330 实验二 《Java面向对象程序设计》 实验报告

    2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验二 <Java面向对象程序设计> 实验报告 课程名称:<程序设计与数据结构> 学生班级:162 ...

  4. 轻量级django 一

    from django.http import HttpResponse from django.conf.urls import url from django.conf import settin ...

  5. django 连接mysql

    环境 Linux 修改工程目录下的settings.py 文件 #!!!!!!!!切勿出现中文 即便//注释也不行 DATABASES = { 'default': { 'ENGINE': 'djan ...

  6. Error contacting service. It is probably not running.

    平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6   hadoop 1.2.1 运行zookeeperd后显示启动成功: JMX enabled ...

  7. Python 迭代器之列表解析与生成器

     [TOC] 1. 列表解析 1.1 列表解析基础 列表解析把任意一个表达式应用到一个迭代对象中的元素 Python内置ord函数会返回一个字符的ASCII整数编码(chr函数是它的逆过程, 它将A ...

  8. JAVA_SE基础——56.包的创建

    接下来我来给大家讲下--包 , 先看一段代码 class Demo1{ public static void main(String[] args) { System.out.println(&quo ...

  9. JAVA_SE基础——30.构造代码块

    黑马程序员入学blog...构造代码块作用:给所有的对象进行统一的初始化. 问题:要求每个小孩出生都会哭,这份代码有两个构造函数,如果需要每个小孩出生都要哭的话,那么就需要在不同的构造函数中都调用cr ...

  10. 非PE病毒介绍

    1.宏病毒 1.1 介绍 本文中的宏特制office系列办公软件中的宏,Microsoft Office中对宏的定义为"宏就是能够组织在一起的,可以作为一个独立命令来执行的一系列Word 命 ...