poj 3176
Cow Bowling
Description
The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
Input
Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Output
Line 1: The largest sum achievable using the traversal rules
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
Hint
Explanation of the sample:
7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5The highest score is achievable by traversing the cows as shown above.
代码:
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[360][360],d[360][360];
int main()
{
int n,i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
scanf("%d",&a[i][j]);
d[0][0]=a[0][0];
for(i=1;i<n;i++)
{
d[i][0]=d[i-1][0]+a[i][0];
d[i][i]=d[i-1][i-1]+a[i][i];
for(j=1;j<i;j++)
{
d[i][j]=max(d[i-1][j-1],d[i-1][j]);
d[i][j]+=a[i][j];
}
}
int c=0;
for(i=0;i<n;i++)
c=max(c,d[n-1][i]);
printf("%d\n",c);
}
poj 2385
Apple Catching
Description
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.
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.
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
* Line 1: The maximum number of apples Bessie can 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 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.
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.
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dp[1005][35];
int a[1005];
int main()
{
int i,j;
int t,w;
scanf("%d%d",&t,&w);
for(i=1;i<=t;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
if(a[1]==1)
{
dp[1][0]=1;
dp[1][1]=0;
}
else { dp[1][0]=0;dp[1][1]=1;}
for(i=2;i<=t;i++)
for(j=0;j<=w;j++)
{
if(i-1>=j)
{
if(j==0)
dp[i][j]=dp[i-1][j];
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]);
if(j%2+1==a[i])
dp[i][j]++;
}
}
int c=0;
for(i=0;i<=w;i++)
c=max(dp[t][i],c);
printf("%d\n",c);
}