BZOJ 3384: [Usaco2004 Nov]Apple Catching 接蘋果( dp )

來源:互聯網
上載者:User

標籤:

dp

dp( x , k ) = max( dp( x - 1 , k - 1 ) + *** , dp( x - 1 , k ) + *** ) *** = 0 or 1 ,根據情況

(BZOJ 1750雙倍經驗) 

------------------------------------------------------------------------

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i )#define clr( x , c ) memset( x , c , sizeof( x ) )#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i ) using namespace std; const int maxn = 1000 + 5;const int maxm = 30 + 5; int d[ maxn ][ maxm ];int pos[ maxn ];int n , t; int dp( int x , int k ) {if( x < 1 || k < 0 ) return 0;int &ans = d[ x ][ k ];if( ans != -1 ) return ans;ans = 0;int p = 1 & k;// 0 -> 1 , 1 -> 2ans = max( dp( x - 1 , k ) + ( p == pos[ x ] ), dp( x - 1 , k - 1 ) + ( p != pos[ x ] ) );return ans;} int main() {freopen( "test.in" , "r" , stdin );clr( d , -1 );cin >> n >> t;Rep( i , n )     scanf( "%d" , pos + i ) , pos[ i ]--;d[ 1 ][ 0 ] = ! pos[ 1 ];int ans = 0;rep( i , t + 1 ) ans = max( dp( n , i ) , ans );cout << ans << "\n";return 0;}

  

------------------------------------------------------------------------

3384: [Usaco2004 Nov]Apple Catching 接蘋果Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 23  Solved: 18
[Submit][Status][Discuss]Description    很少有人知道奶牛愛吃蘋果.農夫約翰的農場上有兩棵蘋果樹(編號為1和2),每一棵樹上都長滿了蘋果.奶牛貝茜無法摘下樹上的蘋果,所以她只能等待蘋果從樹上落下.但是,由於蘋果掉到地上會摔爛,貝茜必須在半空中接住蘋果(沒有人愛吃摔爛的蘋果).貝茜吃東西很快,所以她接到蘋果後僅用幾秒鐘就能吃完.每一分鐘,兩棵蘋果樹其中的一棵會掉落一個蘋果.貝茜已經過了足夠的訓練,只要站在樹下就一定能接住這棵樹上掉落的蘋果.同時,貝茜能夠在兩棵樹之間快速移動(移動時間遠少於1分鐘),因此當蘋果掉落時,她必定站在兩棵樹其中的一棵下面.此外,奶牛不願意不停地往返於兩棵樹之間,因此會錯過一些蘋果, 蘋果每分鐘掉落一個,共T(1≤T≤1000)分鐘,貝茜最多願意移動W(I≤w≤30)次.現給出每分鐘掉落蘋果的樹的編號,要求判定貝茜能夠接住的最多蘋果數.開始時貝茜在1號樹下.Input    第1行:由空格隔開的兩個整數T和W.    第2到T+1行:1或2(每分鐘掉落蘋果的樹的編號).Output     在貝茜移動次數不超過W的前提下她能接到的最多蘋果數Sample Input7 2
2
1
1
2
2
1
1Sample Output6HINT

  7分鐘內共掉落7個蘋果一一第1個從第2棵樹上掉落,接下來的2個蘋果從第1棵樹上掉落,再接下來的2個從第2棵樹上掉落,最後2個從第1棵樹上掉落.   貝茜不移動直到接到從第1棵樹上掉落的兩個蘋果,然後移動到第2棵樹下,直到接到從第2棵


樹上掉落的兩個蘋果,最後移動到第1棵樹下,接住最後兩個從第1棵樹上掉落的蘋果.這樣貝茜共接住6個蘋果.


Source

Gold

 

BZOJ 3384: [Usaco2004 Nov]Apple Catching 接蘋果( dp )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.