记录奥林比克/课程录制 洛谷P2255 [USACO14JAN]

题面在最下方。

本题贪心可解,我也不是很懂动规解法(双线程DP?)

先把各个课程(比赛)按结束时间从小到大排序,记录两个摄像机的结束时间。

然后枚举课程,如果某个课程的开始时间早于任何一台摄像机的结束时间,则该课程不能被录制,如果某个课程只能用某台摄像机录制,则安排该台摄像机录制,如果某个课程能被两台摄像机录制,那么根据贪心应该选择结束时间大的那一个摄像机。
时间复杂度:  排序sort O(N log N)  枚举 O(n)。

附上AC代码

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 template<class T> inline void read(T &_a){
 6     int _ch=getchar();_a=0;
 7     while(_ch<'0' || _ch>'9')_ch=getchar();
 8     while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();}
 9 }
10 
11 const int maxn=151;
12 struct lesson
13 {
14     int s,t;
15     inline bool operator < (const lesson x) const {return t<x.t;}
16 }node[maxn];
17 int n,ans,s1,s2;
18 
19 int main()
20 {
21     freopen("record.in","r",stdin);
22     freopen("record.out","w",stdout);
23     read(n);
24     if(n<=2) {printf("%d",n); return 0;}
25     for (register int i=1;i<=n;++i) read(node[i].s),read(node[i].t);
26     sort(node+1,node+n+1);
27     for (register int i=1;i<=n;++i)
28     {
29         if(node[i].s<s1&&node[i].s<s2) continue;
30         ++ans;
31         if(node[i].s>=s1&&node[i].s<s2) s1=node[i].t;
32         else if (node[i].s<s1&&node[i].s>=s2) s2=node[i].t;
33         else if (s1<s2) s2=node[i].t;
34         else s1=node[i].t;
35     }
36     printf("%d",ans);
37     return 0;
38 }
View Code

动规说明:

法二:DP

双线程DP

先sort

然后对于f[i,j],表示现在同时录制i和j,接下来(包括i和j)最多能录制多少(i<=j)

f[k,j]+1 { i<k<j,且a[k]>=b[i] }

那么f[i,j]=max{f[j,k]+1 { j<k 且 a[k]>=b[i]}

中文版

题目描述:

要开运动会了,神犇学校的n个班级要选班服,班服共有100种样式,编号1~100。现在每个班都挑出了一些样式待选,每个班最多有100个待选的样式。要求每个班最终选定一种样式作为班服,且该班的样式不能与其他班级的相同,求所有可能方案的总数,由于方案总数可能很大,所以要求输出mod 1000000007后的答案。

输入描述:

共有T组数据。

对于每组数据,第一行为一个整数n,表示有n个班级。

2~n+1行,每行有最多100个数字,表示第i-1班待选班服的编号。

输出描述:

对于每组数据,输出方案总数 mod 1000000007后的答案。

样例输入:

2

3

5 100 1

2

5 100

2

3 5

8 100

样例输出:

4

4

数据范围:

对于30%的数据,1<=T<=3, 1<=n<=3,每班待选样式不超过10种。

对于50%的数据,1<=T<=5, 1<=n<=5,每班待选样式不超过50种。

对于100%的数据,1<=T<=10, 1<=n<=10,每班待选样式不超过100种。

英文版(洛谷)

题目描述

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.

输入输出样例

输入样例#1:
6
0 3
6 7
3 10
1 5
2 8
1 9
输出样例#1:
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

原文地址:https://www.cnblogs.com/jaywang/p/7701818.html