↓ 跳过正文
  1. Posts/

hdu 1176 免费馅饼(二维dp)

·1252 字·3 分钟

免费馅饼
#

**Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29065 Accepted Submission(s): 9921 **

Problem Description

都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:

为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)

Input

输入数据有多组。每组数据的第一行为以正整数n(0

Output

每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。 提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。

Sample Input

6

5 1

4 1

6 1

7 2

7 2

8 3

0

Sample Output

4

Author

lwg

二维dp

状态转移方程也很容易想到

dp[i][j]表示在时间i,位置j的时候能得到的馅饼的个数。

dp[i][j]是由dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1]得到。

注意边界和初始化。

因为一开始是在位置5

所以 dp[1][4]=a[1][4];

  dp[1][5]=a[1][5];

  dp[1][6]=a[1][6];

但是取出最大的dp[i][j]的时候出了问题导致我一直WA,而且还没有想得很明白。

AC代码:

代码实现
 1
 2
 3    /* ***********************************************
 4    Author :111qqz
 5    Created Time :2016年02月22日 星期一 23时18分21秒
 6    File Name :code/hdu/1176.cpp
 7    ************************************************ */
 8
 9    #include <iostream>
10    #include <algorithm>
11    #include <cstring>
12    #include <cstdio>
13    #include <cmath>
14
15    using namespace std;
16
17    int n,t,x,maxtime;
18    long long ans;
19    const int N=1E5+7;
20    int a[N][15],dp[N][15];
21
22    int MAX(int a,int b,int c)
23    {
24        int res = -1;
25        if ( a>res )
26            res = a;
27        if ( b>res )
28            res = b;
29        if ( c>res )
30            res = c;
31        return res;
32    }
33
34    int main()
35    {
36        while ( scanf("%d",&n)!=EOF&&n )
37        {
38            ans = -1 ;
39            maxtime = -1;
40            memset(a,0,sizeof(a));
41            memset(dp,0,sizeof(dp));
42            for ( int i = 1 ; i <= n ; i++ )
43            {
44                scanf("%d %d",&x,&t);
45                a[t][x]++;
46                if ( t>maxtime )
47                    maxtime = t;
48            }
49            dp[1][4] = a[1][4];
50            dp[1][5] = a[1][5];
51            dp[1][6] = a[1][6];
52            for ( int i = 2 ; i <= maxtime ; i++ )
53                for ( int j = 0 ; j <= 10 ; j++ )
54                {
55                    if ( j==0 )
56                        dp[i][j] = max(dp[i-1][j],dp[i-1][j+1]) + a[i][j];
57                        else if ( j==10 )
58                                dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + a[i][j];
59                                else  dp[i][j] = MAX(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+a[i][j];
60                   // if ( dp[i][j]>ans )
61                  //      ans = dp[i][j];
62
63                }
64                for ( int i = 0 ; i <= 10 ; i++)
65                    if ( dp[maxtime][i]>ans )
66                       ans = dp[maxtime][i];
67            printf("%I64d\n",ans);
68
69        }
70        return 0;
71    }

WA代码:

代码实现
 1
 2
 3    #include <iostream>
 4    #include <algorithm>
 5    #include <cstring>
 6    #include <cstdio>
 7    #include <cmath>
 8
 9    using namespace std;
10
11    int n,t,x,maxtime;
12    long long ans;
13    const int N=1E5+7;
14    int a[N][15],dp[N][15];
15
16    int MAX(int a,int b,int c)
17    {
18        int res = -1;
19        if ( a>res )
20            res = a;
21        if ( b>res )
22            res = b;
23        if ( c>res )
24            res = c;
25        return res;
26    }
27
28    int main()
29    {
30        while ( scanf("%d",&n)!=EOF&&n )
31        {
32            ans = -1 ;
33            maxtime = -1;
34            memset(a,0,sizeof(a));
35            memset(dp,0,sizeof(dp));
36            for ( int i = 1 ; i <= n ; i++ )
37            {
38                scanf("%d %d",&x,&t);
39                a[t][x]++;
40                if ( t>maxtime )
41                    maxtime = t;
42            }
43            dp[1][4] = a[1][4];
44            dp[1][5] = a[1][5];
45            dp[1][6] = a[1][6];
46            for ( int i = 2 ; i <= maxtime ; i++ )
47                for ( int j = 0 ; j <= 10 ; j++ )
48                {
49                    if ( j==0 )
50                        dp[i][j] = max(dp[i-1][j],dp[i-1][j+1]) + a[i][j];
51                        else if ( j==10 )
52                                dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + a[i][j];
53                                else  dp[i][j] = MAX(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+a[i][j];
54                    if ( dp[i][j]>ans )
55                        ans = dp[i][j];
56
57                }
58              //  for ( int i = 0 ; i <= 10 ; i++)
59              //      if ( dp[maxtime][i]>ans )
60              //         ans = dp[maxtime][i];
61            printf("%I64d\n",ans);
62
63        }
64        return 0;
65    }

相关文章

hdu 1114 - Piggy-Bank (完全背包)

·774 字·2 分钟
F - Piggy-Bank **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

hdu 1087 - Super Jumping! Jumping! Jumping! (最长上升子序列)

·536 字·2 分钟
E - Super Jumping! Jumping! Jumping! **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given chessmen list.

hdu1069 Monkey and Banana (最长下降子序列)

·921 字·2 分钟
C - Monkey and Banana **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

hdu 1171 Big Event in HDU (母函数,01背包)

·1075 字·3 分钟
**Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26534 Accepted Submission(s): 9332 ** Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002. The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).

hdu 1203 I NEED A OFFER! (01背包)

·977 字·2 分钟
I NEED A OFFER! # **Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18287 Accepted Submission(s): 7320 ** Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了。要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的。Speakless没有多少钱,总共只攒了n万美元。他将在m个学校中选择若干的(当然要在他的经济承受范围内)。每个学校都有不同的申请费用a(万美元),并且Speakless估计了他得到这个学校offer的可能性b。不同学校之间是否得到offer不会互相影响。“I NEED A OFFER”,他大叫一声。帮帮这个可怜的人吧,帮助他计算一下,他可以收到至少一份offer的最大概率。(如果Speakless选择了多个学校,得到任意一个学校的offer都可以)。