[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.

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

样例

输入样例#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分,如果有其他方法,可以留言
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<set>
 6 using namespace std;
 7 typedef pair<int,int> pa;
 8 multiset<int> s;
 9 pa cow[300001];
10 int c,n,ans;
11 set<int>::iterator it;
12 int main()
13 {int i,j,x,y;
14 //freopen("3.in","r",stdin);
15 //freopen("x.out","w",stdout);
16     cin>>c>>n;
17      for (i=1;i<=c;i++)
18      {
19         scanf("%d",&x);
20         s.insert(x);
21      }
22      for (i=1;i<=n;i++)
23      {
24         scanf("%d%d",&x,&y);
25         cow[i]=pa(y,x);
26      }
27     sort(cow+1,cow+n+1);
28     for (i=1;i<=n;i++)
29     {
30          it=s.lower_bound(cow[i].second);
31           if (it!=s.end() && *it<=cow[i].first)
32           {
33                 ans++;
34                s.erase(it);
35           }
36     }
37 cout<<ans;
38 }
posted @ 2017-07-02 10:34  Z-Y-Y-S  阅读(402)  评论(0编辑  收藏  举报