poj 2385 Apple Catching

Apple Catching
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7108   Accepted: 3469

Descriptionios

It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.

Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).

Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.

Input數組

* Line 1: Two space separated integers: T and W

* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.

Outputapp

* Line 1: The maximum number of apples Bessie can catch without walking more than W times.

Sample Inputless

7 2
2
1
1
2
2
1
1

Sample Outputui

6

Hintspa

INPUT DETAILS:

Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.

OUTPUT DETAILS:

Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
========================================================================
題目大意:不多有人知道母牛也愛吃蘋果,John農場裏有一隻母牛Bessie,和兩棵蘋果樹。Bessie不吃摔爛的蘋果因此只能在空中接住蘋果來吃。
兩棵蘋果樹每分鐘掉落一個蘋果,一共掉落T分鐘,T個蘋果,兩棵蘋果樹不遠,Bessie一開始在樹1下,若是另外一棵樹掉蘋果,Bessie能趕過去接住。
可是Bessie體力有限,在兩棵之間的奔波有次數限制W。提供T、W和每分鐘掉蘋果的樹的編號,問Bessie最多接到多少蘋果。

解題思路:就一組數據,猜也是DP了。
既然是DP就要打狀態表,橫軸是跑動的次數,縱軸掉落是第多少個蘋果,狀態值是到掉落第n個蘋果時跑動 i 次能接到的最多蘋果數。
更新行時,跑動次數爲0的狀態值能夠按照哪棵樹掉蘋果直接填寫,跑動次數爲 j 的狀態值 dp[ i ][ j ] = max( dp[ i-1 ][ j-1 ] , dp[ i-1 ][ j ] )+1。
 
 1 //dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + 1。
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 using namespace std;
 7 int dp[1005][35];
 8 int main()
 9 {
10     int a[1005];
11     int t, w;
12     cin >> t >> w;
13     int i;
14     memset(dp, 0, sizeof(dp));
15     memset(a, 0, sizeof(a));
16     for (i = 1; i <= t; i++)
17     {
18         cin >> a[i];
19     }
20     int j;
21     for (i = 1; i <= t; i++)
22     {
23         if (a[i] == 1)
24         {
25             dp[i][0] = dp[i-1][0] + 1;
26         }
27         else dp[i][0] = dp[i - 1][0];
28         for (j = 1; j <= w; j++)
29         {
30             if ((j & 1) != (a[i] & 1))        //若是掉蘋果的樹編號與跑動次數奇偶性不一樣,則更新.好比蘋果掉在1,跑了1次,沒接到蘋果
31                 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + 1;
32             else                           //若是掉蘋果的樹編號與跑動次數奇偶性相同,則保持上一行的狀態值  
33                 dp[i][j] = dp[i - 1][j];
34         }
35     }
36     int ans = 0;
37     for (int i = 0; i <= w; i++)        //遍歷dp數組最後一行找最優狀態值  
38     {
39         if (dp[t][i] > ans)
40         {
41             ans = dp[t][i];
42         }
43     }
44     printf("%d\n", ans);
45     return 0;
46 
47 }

偶數&1爲0,奇數&1爲1code

2&1即01&10=00,奇數的最後一位是1,偶數的最後一位是0blog

相關文章
相關標籤/搜索