[USACO2009 OPEN] 滑雪课 Ski Lessons

[USACO2009 OPEN]滑雪课Ski Lessons

题目描述

Farmer John wants to take Bessie skiing in Colorado. Sadly, Bessie is not really a very good skier.

Bessie has learned that the ski resort is offering S (0 <= S <= 100) ski classes throughout the day. Lesson i starts at time M_i (1 <= M_i <= 10,000) and lasts for time L_i (1 <= L_i <= 10,000). After lesson i, Bessie's ski ability becomes A_i (1 <= A_i <= 100). Note: this ability is an absolute, not an incremental change.

Bessie has purchased a map which shows all N (1 <= N <= 10,000) ski slopes along with the time D_i (1 <= D_i <= 10,000) required to ski down slope i and the skill level C_i (1 <= C_i <= 100) required to get down the slope safely. Bessie's skill level must be greater than or equal to the skill level of the slope in order for her to ski down it.

Bessie can devote her time to skiing, taking lessons, or sipping hot cocoa but must leave the ski resort by time T (1 <= T <= 10,000), and that means she must complete the descent of her last slope without exceeding that time limit.

Find the maximum number of runs Bessie can complete within the time limit. She starts the day at skill level 1.

Extra feedback will be provided on the first 50 submissions.

Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪。很不幸,Bessie滑雪技术并不精湛。 Bessie了解到,在滑雪场里,每天会提供S(0<=S<=100)门滑雪课。第i节课始于M_i(1<=M_i<=10000),上的时间为L_i(1<=L_i<=10000)。

上完第i节课后,Bessie的滑雪能力会变成A_i(1<=A_i<=100). 注意:这个能力是绝对的,不是能力的增长值。

Bessie买了一张地图,地图上显示了N(1 <= N <= 10,000)个可供滑雪的斜坡,从第i个斜坡的顶端滑至底部所需的时长D_i(1<=D_i<=10000),以及每个斜坡所需要的滑雪能力C_i(1<=C_i<=100),以保证滑雪的安全性。Bessie的能力必须大于等于这个等级,以使得她能够安全滑下。

Bessie可以用她的时间来滑雪,上课,或者美美地喝上一杯可可汁,但是她必须在T(1<=T<=10000)时刻离开滑雪场。这意味着她必须在T时刻之前完成最后一次滑雪。 求Bessie在实现内最多可以完成多少次滑雪。这一天开始的时候,她的滑雪能力为1.

输入输出格式

输入格式:

* Line 1: Three space-separated integers: T, S, and N

* Lines 2..S+1: Line i+1 describes ski lesson i with three

space-separated integers: M_i, L_i, and A_i

* Lines S+2..S+N+1: Line S+i+1 describes ski slope i with two

space-separated integers: C_i and D_i.

输出格式:

A single integer on a line by itself, the maximum number of runs that Bessie may ski within the time limit.

输入输出样例

输入样例#1:  复制
10 1 2 
3 2 5 
4 1 
1 3 
输出样例#1:  复制
6 

说明

Ski the second slope once, take the lesson, and ski the first slope 5 times before time is up: a total of 6 slopes.

题解

看到题目就觉得这是动规但一直没想到如何状态转移……看了别人的题解之后才有一些想法

f[i][j]:前i单位时间能力值为j可以滑的最多次数

lessons[i][j]:结束时间为i,获得能力为j的时长最短的课程的开始时间

ski[i]:能力值为i可以滑的时间最短的坡的时长

d[i]表示前i时长最多可以滑的坡数

几个状态转移方程:

喝可可:f[i][j]=max(f[i][j],f[i-1][j])

滑雪:f[i][j]=max(f[i][j],f[i-ski[j]][j]+1)

上课:f[i][j]=max(f[i][j],d[lessons[i-1][j]])

代码

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 int lessons[10001][101],ski[111],f[10001][101],d[10001];
 6 //lessons[i][j]表示结束时间为i,能力为j的课程的最晚开始时间 
 7 //ski[i]表示能力值为i可以滑的时间最短的坡的时长 
 8 //f[i][j]表示前i时长能力值为j最多可以滑的坡数 
 9 //d[i]表示前i时长最多可以滑的坡数 
10 int t,s,n;
11 int main()
12 {
13     scanf("%d%d%d",&t,&s,&n);
14     for(int i=1;i<=s;i++)//初始化lessons[][] 
15     {
16         int m,l,a;
17         scanf("%d%d%d",&m,&l,&a);
18         lessons[l+m-1][a]=max(lessons[l+m-1][a],m);
19     }
20     for(int i=1;i<=n;i++)//初始化ski[] 
21     {
22         int c,d;
23         scanf("%d%d",&c,&d);
24         for(int j=c;j<=100;j++)
25             if(!ski[j]||ski[j]>d)
26                 ski[j]=d;
27     }
28     for(int i=0;i<=t;i++)
29         for(int j=0;j<=100;j++)
30             f[i][j]=-1000000;
31     f[0][1]=0;
32     for(int i=1;i<=t;i++)
33     {
34         for(int j=1;j<=5;j++)
35         {
36             f[i][j]=max(f[i][j],f[i-1][j]);//喝可可 
37             if(ski[j]&&i>=ski[j])//滑雪 
38                 f[i][j]=max(f[i][j],f[i-ski[j]][j]+1);
39             if(lessons[i-1][j])//上课 
40                 f[i][j]=max(f[i][j],d[lessons[i-1][j]]);
41             d[i]=max(d[i],f[i][j]);
42         }
43     }
44     printf("%d\n",d[t]);
45     return 0;
46 }

注意两个问题:

1、初始化:f[0][1]=0(初始化能力为1),其余都为负无穷!

2、状态转移方程没有f[i][j]=max(f[i][j],f[i][j-1)!

第一次写的时候因为这两个问题WA了……但自己也没想出来为什么……如果有神犇理解的话敬请指教w  

最后吧分享一句关于动规挺有感触的一句话……

除了要对基本概念和方法正确理解外,必须具体问题具体分析处理,以丰富的想象力去建立模型,用创造性的技巧去求解

(新人第一次发帖,多多指教)

猜你喜欢

转载自www.cnblogs.com/LiHaozhe/p/9475473.html
ski
今日推荐