Description
It isA little known fact that cows love apples. Farmer John has apple trees (which is conveniently numbered1and2)inchHis field, each of the full of apples. Bessie cannot reach the apples when they is on the tree, so she must wait forthem to fall. However, she mustCatchtheminchThe air since the apples bruise when they hits the ground (and no one wants to eat bruised apples). Bessie isA quick eater, so an apple she doesCatch isEateninchjust a few seconds. Each minute, one of the trees drops Apple. Bessie, having much practice, canCatchAn appleifShe isStanding under a tree fromWhich one falls. While Bessie can walk between the trees quickly (inchMuch less than a minute), she can stand under only one tree at any time. Moreover, cows DoNotGetA lot of exercise, so she isNot willing to walk back and forth between the trees endlessly (and thus misses some apples ). Apples Fall (one each minute) forT1<= T <=1, the) minutes. Bessie isWilling to walk back and forth at the most W (1<= W <= -) times. Given which tree would drop an apple each minute, determine the maximum number of apples which Bessie canCatch. Bessie starts at tree1.
Input
12.. t+112: The tree that would drop an apple each minute.
Output
1 catch without walking more than W times.
Sample Input
7 2 2 1 1 2 2 1 1
Sample Output
6
Hint
INPUT details:seven apples fall-One fromTree2, then bothinchA row fromTree1, then bothinchA row fromTree2, then bothinchA row fromTree1. Bessie isWilling to walk fromOne tree to the other twice. OUTPUT Details:bessie canCatchSix apples by staying under tree1Until the first and dropped, then moving to tree2 forThe next, then returning back to the tree1 forThe final.
Source
Usaco 2004 November
Set Dp[i][j] indicates the maximum value of the apple when walking J-step when I find the first Apple.
to initialize first, see the Code
Dp[i][j]=max (dp[i-1][j],dp[i-1][j-1]), indicating the maximum value of walking or not walking. Then judge whether you can dp[i][j]++. Finally find the maximum value
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6#include <cmath>7 using namespacestd;8 #defineW 369 #defineN 1006Ten intDp[n][w]; One intn,w; A intA[n]; - intMain () - { the while(SCANF ("%d%d", &n,&w) = =2){ - //int sum=0; - for(intI=1; i<=n;i++){ -scanf"%d",&a[i]); + } -Memset (DP,0,sizeof(DP)); + if(a[1]==1){ Adp[1][0]=1; atdp[1][1]=0; - } - if(a[1]==2){ -dp[1][0]=0; -dp[1][1]=1; - } in - for(intI=2; i<=n;i++){ to for(intj=0; j<=w;j++){ + if(j==0){ -dp[i][j]=dp[i-1][j]+ (j%2+1==a[i]); the Continue; * } $Dp[i][j]=max (dp[i-1][j],dp[i-1][j-1]);Panax Notoginseng if(j%2+1==A[i]) { -dp[i][j]++; the } + } A } the intans=dp[n][0]; + for(intI=1; i<=w;i++){ -ans=Max (ans,dp[n][i]); $ } $printf"%d\n", ans); - - } the return 0; -}
View Code
There is another way:
Set Dp[i][j] means that when I find the apple, I walk up to the max of J-Step apples
You can run the
Before i-1 minutes up to walk J times
Up to J-1 times before I-1 min.
These two states are moving over.
Note that the second type of transfer J can choose to walk or not to go. Because it's going to be at most J times.
With a tree-shaped DP that I've done before.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6#include <cmath>7 using namespacestd;8 #defineW 369 #defineN 1006Ten intDp[n][w]; One intn,w; A intA[n]; - intMain () - { the while(SCANF ("%d%d", &n,&w) = =2){ - //int sum=0; - for(intI=1; i<=n;i++){ -scanf"%d",&a[i]); + } -Memset (DP,0,sizeof(DP)); + if(a[1]==1) dp[1][0]=1; Adp[1][1]=1; at for(intI=2; i<=n;i++){ - for(intj=0; j<=w;j++){ - if(j==0){ -dp[i][j]=dp[i-1][j]+ (j%2+1==a[i]); - Continue; - } in -Dp[i][j]=max (dp[i][j],dp[i-1][j]+ (j%2+1==a[i])); toDp[i][j]=max (dp[i][j],dp[i-1][j-1]+ (j%2==a[i])); +Dp[i][j]=max (dp[i][j],dp[i-1][j-1]+ (j%2+1==a[i])); - } the } *printf"%d\n", Dp[n][w]); $ }Panax Notoginseng return 0; -}
View Code
POJ 2385 Apple Catching (DP)