[USACO17FEB]Why Did the Cow Cross the Road I S
题目描述
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.
请计算最大的可行牛-鸡配对数。
样例
5 4
7
8
6
2
9
2 5
4 9
0 3
8 13
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的更多相关文章
- 洛谷 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 ...
- 洛谷 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 ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- 洛谷 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 ...
- [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 ...
- [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 ...
- 洛谷 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 ...
- [USACO17FEB]Why Did the Cow Cross the Road II S
题目描述 The long road through Farmer John's farm has crosswalks across it, conveniently numbered (). ...
- 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...
随机推荐
- 论C++的智能指针
一.简介 参考这篇博客,并且根据<C++ Primer>中相关知识,我总结了C++关于智能指针方面的内容. 为了解决内存泄漏的问题,便出现了智能指针.STL提供的智能指针有:aut ...
- 高校学生征信系统Postmortem结果
Postmortem结果 设想和目标 1 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件需要解决的问题是当前高校学生征信系统建设薄弱的问题,我们试图建立 ...
- C++数据结构中的基本算法排序
冒泡排序 基本思想:两两比较待排序的数,发现反序时交换,直到没有反序为止. public static void BubbleSort(int[] R) { for (int i = 0; i < ...
- MySQL 服务安装及命令使用
MySQL 服务安装及命令使用 课程来源说明 本节实验后续至第17节实验为本课程的进阶篇,都基于 MySQL 官方参考手册制作,并根据实验楼环境进行测试调整改编.在此感谢 MySQL 的开发者,官方文 ...
- Android属性动画 nineoldandroids
各种资源链接 nineoldandroids 任玉刚的五个图片滑动,点击menu http://blog.csdn.net/singwhatiwanna/article/details/1763998 ...
- 一个CSS简单入门网站
讲的知识简单明了,很实用: http://zh.learnlayout.com/
- Microsoft dynamic sdk中join应该注意的问题.
QueryExpression queryNextSeq = new QueryExpression { EntityName = "ep_prodoperationsequence&quo ...
- Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇——多页面VueSSR+热更新Server)
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇--多页面VueSSR+热更新Server) @(HTML/JS) 这是Vue多页面框架系列文章的第二篇,上一篇(纯前 ...
- VMware vCenter Server 6.5.0 U1
VMware vCenter Server 6.5.0 U1gName: VMware-VCSA-all-6.5.0-8024368.iso Release Date: 2018-03-20 Buil ...
- 关于PHP7
目前一直使用php7也看了许多文档视频等,整理一下相关细节(仅为记录-),对于PHP7性能,如下图所示. * 在wordpress3.0.1中 php7比php5.6性能提升约3倍左右 新特性 一.变 ...