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:
7Then 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.
3 8
8 1 0
2 7 4 4
4 5 2 6 5
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.Output
Line 1: The largest sum achievable using the traversal rulesSample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5Sample Output
30Hint
Explanation of the sample:
7The highest score is achievable by traversing the cows as shown above.
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
非常明显的dp+贪心
对于n行的三角形,其一共有 n*(n+1)/2 个数
dp[left(i)] = max{ dp[left(i) , dp[i] + a[left(i)] }
dp[left(i+1)+1] = max{ dp[left(i)+1] , dp[i] + a[left(i)+1] }
其中,left(i)表示i下一层左边的数
AC代码:GitHub
1 /* 2 By:OhYee 3 Github:OhYee 4 HomePage:http://www.oyohyee.com 5 Email:oyohyee@oyohyee.com 6 Blog:http://www.cnblogs.com/ohyee/ 7 8 かしこいかわいい? 9 エリーチカ! 10 要写出来Хорошо的代码哦~ 11 */ 12 13 #include <cstdio> 14 #include <algorithm> 15 #include <cstring> 16 #include <cmath> 17 #include <string> 18 #include <iostream> 19 #include <vector> 20 #include <list> 21 #include <queue> 22 #include <stack> 23 #include <map> 24 using namespace std; 25 26 //DEBUG MODE 27 #define debug 0 28 29 //循环 30 #define REP(n) for(int o=0;o<n;o++) 31 32 #define t(n) (((n) * ((n)+1))/2) 33 34 const int maxn = 1005; 35 int n; 36 int a[t(maxn)]; 37 int dp[t(maxn)]; 38 39 int left(int n) { 40 int i; 41 for(i = 0;t(i) < n;i++); 42 return n + i; 43 } 44 45 bool Do() { 46 if(scanf("%d",&n) == EOF) 47 return false; 48 for(int i = 1;i <= t(n);i++) 49 scanf("%d",&a[i]); 50 51 memset(dp,0,sizeof(dp)); 52 for(int i = 0;i <= t(n - 1);i++) { 53 dp[left(i)] = max(dp[left(i)],dp[i] + a[left(i)]); 54 dp[left(i) + 1] = max(dp[left(i) + 1],dp[i] + a[left(i) + 1]); 55 } 56 57 int Max = -1; 58 for(int i = t(n - 1) + 1;i <= t(n);i++) 59 Max = max(Max,dp[i]); 60 61 printf("%d\n",Max); 62 return true; 63 } 64 65 int main() { 66 while(Do()); 67 return 0; 68 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com