#P3661. [USACO17FEB] Why Did the Cow Cross the Road I S

[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 CC chickens on the farm (1C20,0001 \leq C \leq 20,000), conveniently numbered 1C1 \ldots C, and each chicken ii is only willing to help a cow at precisely time TiT_i. The cows, never in a hurry, have more flexibility in their schedules. There are NN cows on the farm (1N20,0001 \leq N \leq 20,000), conveniently numbered 1N1 \ldots N, where cow jj is able to cross the road between time AjA_j and time BjB_j. Figuring the "buddy system" is the best way to proceed, each cow jj would ideally like to find a chicken ii to help her cross the road; in order for their schedules to be compatible, ii and jj must satisfy AjTiBjA_j \leq T_i \leq B_j.

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 CC and NN. The next CC lines contain T1TCT_1 \ldots T_C, and the next NN lines contain AjA_j and BjB_j (AjBjA_j \leq B_j) for j=1Nj = 1 \ldots N. The AA's, BB's, and TT'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.

题目大意

题目描述

Farmer John 的奶牛们正在学习如何有效地过马路。回想起古老的“鸡为什么要过马路?”笑话,他们认为鸡一定是过马路的专家,于是去寻找鸡来帮助它们。

事实上,鸡是非常忙碌的生物,它们只有有限的时间来帮助奶牛。农场上有 CC 只鸡(1C20,0001 \leq C \leq 20,000),方便地用编号 1C1 \ldots C 标识,每只鸡 ii 只愿意在确切的时间 TiT_i 帮助一头奶牛。奶牛们从不着急,它们的日程安排更加灵活。农场上有 NN 头奶牛(1N20,0001 \leq N \leq 20,000),方便地用编号 1N1 \ldots N 标识,其中奶牛 jj 能够在时间 AjA_j 到时间 BjB_j 之间过马路。考虑到“伙伴系统”是最好的方式,每头奶牛 jj 理想情况下希望找到一只鸡 ii 来帮助她过马路;为了使它们的日程安排兼容,iijj 必须满足 AjTiBjA_j \leq T_i \leq B_j

如果每头奶牛最多只能与一只鸡配对,每只鸡也最多只能与一头奶牛配对,请计算可以构建的最大奶牛-鸡配对数。

输入格式

输入的第一行包含 CCNN。接下来的 CC 行包含 T1TCT_1 \ldots T_C,接下来的 NN 行包含 AjA_jBjB_jAjBjA_j \leq B_j),其中 j=1Nj = 1 \ldots NAABBTT 都是不超过 1,000,000,000 的非负整数(不一定互不相同)。

输出格式

请计算可能的奶牛-鸡配对的最大数量。

5 4
7
8
6
2
9
2 5
4 9
0 3
8 13
3