Light OJ 1004,lightoj1004
1004 - Monkey Banana Problem
|
PDF (English) |
Statistics |
Forum |
| Time Limit: 2 second(s) |
Memory Limit: 32 MB |
You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.
Output
For each case, print the case number and maximum number of bananas eaten by the monkey.
| Sample Input |
Output for Sample Input |
|
2
4
7
6 4
2 5 10
9 8 12 2
2 12 7
8 2
10
2
1
2 3
1 |
Case 1: 63
Case 2: 5 |
三角形塔題目的加強版,這次是上下兩個顛倒的三角形疊加一起了。
結果我下標沒注意,要調試了很長時間,反覆強調下標的精確性了。
方法也是從底往上:只有一條數組的時候,必然是從中選擇最大的一個數,有兩條數組的時候,那麼就是從上一條走到下一條,當前數組的一個格子的數選擇可以走到下一個數組的數,這裡是兩個選擇,的最大值加起來。如此從底往上推導,就能出結果了。
不要問我演算法有什麼用了:
就自娛自樂吧。
小公司是根本用不上的,或者說他們根本沒有懂演算法的人在裡面,也就更加不會有懂演算法的人出來當面試官,招懂演算法的人了。
演算法是屠龍刀,問題是先要找到龍。
#include <stdio.h>#include <vector>#include <string.h>#include <algorithm>#include <iostream>#include <string>#include <limits.h>#include <stack>#include <queue>#include <set>#include <map>using namespace std;const int MAX_N = 100;const int MAX_DN = 200;long long arr[MAX_DN][MAX_N], tbl[MAX_N];long long getMostBanana(long long A[][MAX_N], long long dp[], int n){int len = (n<<1)-1;memset(dp, 0, sizeof(long long)*n);dp[0] = A[len-1][0];for (int i = len-2, d = 2; i >= n-1; i--, d++)//注意不是i>=n是i>=n-1{dp[d-1] = dp[d-2] + A[i][d-1];for (int j = d-2; j > 0; j--){dp[j] = max(dp[j], dp[j-1]) + A[i][j];}dp[0] = dp[0] + A[i][0];}for (int i = n-2, d = n-1; i >= 0; i--, d--)//不能是i=n-1,要i=n-2{//一點定的接合不對,就會答案錯誤。for (int j = 0; j < d; j++){dp[j] = max(dp[j], dp[j+1]) + A[i][j];}}return dp[0];}int main(){int T, n, t = 1;scanf("%d", &T);while (T--){printf("Case %d: ", t++);scanf("%d", &n);for (int i = 1; i <= n; i++){for (int j = 0; j < i; j++)scanf("%lld", &arr[i-1][j]);}for (int i = n-1, d = n; i >= 1; i--, d++){for (int j = 0; j < i; j++)scanf("%lld", &arr[d][j]);}printf("%lld\n", getMostBanana(arr, tbl, n));}return 0;}
杭電OJ1004誰可以幫我看下呀,改了很多遍還是不對
貌似b[]重複定義了。。去掉裡面的,把外面的 下標改成1009
還有 a數組開小了
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,k=0,j,max;
char a[1005][17];
int b[1009]={0};
while(scanf("%d",&n)&&n!=0)
{
int k=0;
for(i=0;i<n;i++)
scanf("%s",a[i]),b[i]=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(a[j],a[i])==0)
b[i]++;
max=b[0];
for(i=1;i<n;i++)
if(b[i]>max)
{
max=b[i];
k=i;
}
printf("%s\n",a[k]);
}
return 0;
}
杭電OJ1004誰可以幫我看下呀,改了很多遍還是不對
數組a太小了,改成1000就可以了