题目来源:http://poj.org/problem?id=2181

Jumping Cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5065 Accepted: 3082

Description

Farmer John's cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump.

The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the cows in their quest to jump. These potions must be administered exactly in the order they were created, though some may be skipped.

Each potion has a 'strength' (1 <= strength <= 500) that enhances the cows' jumping ability. Taking a potion during an odd time step increases the cows' jump; taking a potion during an even time step decreases the jump. Before taking any potions the cows' jumping ability is, of course, 0.

No potion can be taken twice, and once the cow has begun taking potions, one potion must be taken during each time step, starting at time 1. One or more potions may be skipped in each turn.

Determine which potions to take to get the highest jump.

Input

* Line 1: A single integer, P

* Lines 2..P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.

Output

* Line 1: A single integer that is the maximum possible jump.

Sample Input

8
7
2
1
8
4
3
5
6

Sample Output

17
这两天做搜索题做得很无语,中午没事了想刷道水题,练练手,上网搜了一下pku的强联通, 有人说2181是强连通,,一看没做过,果断读题。。
读了两遍,没发现和强联通有什么关系,, 我看着像DP, 很是怀疑那哥们是不是写错了,,翻了下discuss,果断是DP, 很无语。。。
题目大意:有p个跳板,每个跳板有一个弹跳度a[i], 如果在奇数时间走上这个跳板,则总弹跳力度增加a[i],如果在偶数时间走上这个跳板,则总弹跳力度
减少a[i],问最后能积累的最大弹跳力度!!
思路:首先贪心可解此题, 7 2 1 8 4 3 5 6  可分成 7 (2 1) 8 (4 ,3) (5 6);  在奇数时间取上升序列中最大的,在偶数时间取下降序列中最小的。
不过最后有一点需要注意,当整个序列进行完时,最后肯定是加上一个数,7 (2 1) 8 (4 3) (5 6) 1 , 这个例子的结果和上面的那个是一样的!
如果用dp的话: dp[i][1]表示走到第i个跳板时是奇数, dp[i][2]表示走到第i个跳板时是偶数。
则状态转移方程为:dp[i][1]=max(dp[i-1][1]-a[i-1]+a[i],dp[i-1][2]+a[i]);
dp[i][2]=max(dp[i-1][1]-a[i],dp[i-1][2]+a[i-1]-a[i]);
然后遍历所有dp[i][1],dp[i][2],取其中最大的!! 此题代码不再给出!
 
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3905

Sleeping

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 836    Accepted Submission(s): 332


Problem Description
ZZZ is an enthusiastic ACMer and he spends lots of time on training. He always stays up late for training. He needs enough time to sleep, and hates skipping classes. So he always sleeps in the class. With the final exams coming, he has to spare some time to listen to the teacher. Today, he hears that the teacher will have a revision class. The class is N (1 <= N <= 1000) minutes long. If ZZZ listens to the teacher in the i-th minute, he can get Ai points (1<=Ai<=1000). If he starts listening, he will listen to the teacher at least L (1 <= L <= N) minutes consecutively. It`s the most important that he must have at least M (1 <= M <= N) minutes for sleeping (the M minutes needn`t be consecutive). Suppose ZZZ knows the points he can get in every minute. Now help ZZZ to compute the maximal points he can get.
 

Input
The input contains several cases. The first line of each case contains three integers N, M, L mentioned in the description. The second line follows N integers separated by spaces. The i-th integer Ai means there are Ai points in the i-th minute.
 

Output
For each test case, output an integer, indicating the maximal points ZZZ can get.
 

Sample Input
10 3 3 1 2 3 4 5 6 7 8 9 10
 

Sample Output
49
此题也是dp,也是无意中做的题,,发现我和Dp有无解之缘啊。。。
题目大意:一节课有n分钟,ZZZ每听一分钟课都能得到一定的分数,一旦她开始听课就必须要至少连续听L分钟,但是她每节课都要有m分钟的睡眠时间(这m分钟不连续),
问在不影响她睡觉的情况下, 她一节课最多能得多少分。。。
这个题比上个要难一些,首先dp[i][j]表示前i分钟有j分钟是睡觉时间, 如果第i分钟睡觉dp[i][j]=dp[i-1][j-1];
第i分钟学习的话:  dp[i][j]=max(dp[k][j]+sum[i]-sum[k])    1<=k<=i-L;  sum[i]表示前i分钟的搜友权值之和。
这里如果枚举k的话会超时, 要另开一个数组进行记录!!
code:
View Code
 1 # include<stdio.h>
2 # include<string.h>
3 int sum[1005];
4 int dp[1005][1005],dp_tmp[1005][1005];
5 int max(int a,int b)
6 {
7 return a>b?a:b;
8 }
9 int main()
10 {
11 int i,j,n,m,L;
12 while(scanf("%d%d%d",&n,&m,&L)!=EOF)
13 {
14 sum[0]=0;
15 for(i=1;i<=n;i++)
16 {
17 scanf("%d",sum+i);
18 sum[i]+=sum[i-1];
19 }
20 memset(dp,0,sizeof(dp));
21 memset(dp_tmp,0,sizeof(dp_tmp));
22 for(i=1;i<=n;i++)
23 {
24 for(j=0;j<=m && j<=i;j++)
25 {
26 dp[i][j]=dp[i-1][j-1];///第i分钟睡觉能获得的最大值
27 if(i-L>=j) dp_tmp[i][j]=max(dp_tmp[i-1][j]+sum[i]-sum[i-1],dp[i-L][j]+sum[i]-sum[i-L]);
28 if(i-L>=0)
29 dp[i][j]=max(dp[i][j],dp_tmp[i][j]);
30 }
31 }
32 printf("%d\n",dp[n][m]);
33 }
34 return 0;
35 }
 
 
 
 
posted on 2011-08-16 20:38  奋斗青春  阅读(889)  评论(0编辑  收藏  举报