[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 ...
随机推荐
- win7开启wifi
在启用本地共享连接时,出现的错误! 我已经建了一个无线临时网络,来启用共享用来上网的!Internet连接共享访问被启用时,出现了一个错误(null)?而且这错误也会在系统日志里留下记录,都是些莫名其 ...
- collections deque队列及其他队列
from collections import deque dq = deque(range(10),maxlen=10) dq.rotate(3)#队列旋转操作接受一个参数N,让N>0时,队列 ...
- EVA 4400存储硬盘故障数据恢复方案和数据恢复过程
EVA系列存储是一款以虚拟化存储为实现目的的HP中高端存储设备,平时数据会不断的迁移,加上任务通常较为繁重,所以磁盘的负载相对是较重的,也是很容易出现故障的.EVA是依靠大量磁盘的冗余空间,以及故障后 ...
- es6+react.js组件入门初探
React是一个用于构建用户见面的javascript库. React主要用于构建UI,许多人认为React是MVC中的V(视图) React起源于Facebook的内部项目,用来架设Instagra ...
- maven 每次update后影响接口实现类的问题
遇到maven每次update后,就会更改eclipse中java Compiler中的jdk compliance版本 <plugin> <groupId>org.apach ...
- SpringCloud的Hystrix(五) Hystrix机制
参考链接:http://www.jianshu.com/p/e07661b9bae8 一.前言 大型复杂的分布式系统中,高可用相关的技术架构非常重要.高可用架构非常重要的一个环节,就是如何将分布式系统 ...
- maven入门(10)maven的仓库
[0]README 1)本文部分文字转自 "maven实战",旨在 review "maven(6)仓库" 的相关知识: [1]何为 Maven仓库 1) ...
- Android 6.0 以后webview不加载图片的问题
/** * Webview在安卓5.0之前默认允许其加载混合网络协议内容 * 在安卓5.0之后,默认不允许加载http与https混合内容,需要设置webview允许其加载混合网络协议内容 */if ...
- logback中批量插入数据库的参考代码
protected void insertProperties(Map<String, String> mergedMap, Connection connection, long eve ...
- centos虚拟机nat模式,可以上内网,不能上外网
http://sky425509.iteye.com/blog/1996085 我这边的问题是,好久没用虚拟机了,重启之后,变成了dhcp模式,整个网卡配置变了. 重新配置了静态ip,网关,dns后才 ...