题目描述
Being a fan of all cold-weather sports (especially those involving cows),
Farmer John wants to record as much of the upcoming winter Moolympics as
possible.
The television schedule for the Moolympics consists of N different programs
(1 <= N <= 150), each with a designated starting time and ending time. FJ
has a dual-tuner recorder that can record two programs simultaneously.
Please help him determine the maximum number of programs he can record in
total. 冬奥会的电视时刻表包含N (1 <= N <= 150)个节目,每个节目都有开始和结束时间。农民约翰有两台录像机,请计算他最多可以录制多少个节目。
输入输出格式
输入格式:
-
Line 1: The integer N.
- Lines 2..1+N: Each line contains the start and end time of a single
program (integers in the range 0..1,000,000,000).
输出格式:
- Line 1: The maximum number of programs FJ can record.
输入输出样例
6 0 3 6 7 3 10 1 5 2 8 1 9
4
说明
INPUT DETAILS:
The Moolympics broadcast consists of 6 programs. The first runs from time
0 to time 3, and so on.
OUTPUT DETAILS:
FJ can record at most 4 programs. For example, he can record programs 1
and 3 back-to-back on the first tuner, and programs 2 and 4 on the second
tuner.
Source: USACO 2014 January Contest, Silver
分析
如果只有一台的话直接贪心即可,两台呢,贪心也可以,多加一个变量,记录另一台摄像机到什么时间了即可
额能会有几种情况:
- 两台摄像机都在拍摄节目,这个节目也就拍不成了
- 有一台摄像机空闲,那就选这台拍
- 两台都空闲,那么找两者结束之前拍摄较晚的继续拍下去,另外一台留给后面的拍,这样选择余地更多(浪费也少)
注意结束时间还是可以用的,他在结束时间的前一分钟已经拍完了,手动测试样例发现的,难怪我调试不对orz。
代码
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 5 struct Que{ 6 int l,r; 7 bool operator < (const Que &a) const 8 { 9 return r < a.r; 10 } 11 }q[210]; 12 13 int main() 14 { 15 int n,ans = 0, last1 = -1,last2 = -1; 16 scanf("%d",&n); 17 for (int i=1; i<=n; ++i) 18 scanf("%d%d",&q[i].l,&q[i].r); 19 sort(q+1,q+n+1); 20 for (int i=1; i<=n; ++i) 21 { 22 if (q[i].l<last1&&q[i].l<last2) continue ; 23 ans++; 24 if (q[i].l>=last1&&q[i].l<last2) last1 = q[i].r; 25 else if (q[i].l<last1&&q[i].r>=last2) last2 = q[i].r; 26 else if (last1<last2) last2 = q[i].r; 27 else last1 = q[i].r; 28 } 29 printf("%d",ans); 30 return 0; 31 }