首页 > ACM题库 > HDU-杭电 > hdu 2921 Adding up Triangles[解题报告]C++
2014
02-23

hdu 2921 Adding up Triangles[解题报告]C++

Adding up Triangles

问题描述 :

Take a look at the triangle on the left of the figure below. It is made of 9 (unit) triangles arranged in three rows (N = 3 ). Needless to say, a unit triangle is a triangle with N = 1 .

If you study the figure for few seconds, you’ll realize that you can find 13 different triangles (which we’ll call sub-triangles.) Of these 13 sub-triangles we have: Nine unit triangle; three with N = 2 , and one with N = 3 . The following table lists the number of sub-triangles in arrangements with N < 5 .
Let’s define the value of a unit triangle to be the integer value written in that triangle. In general, the value of a triangle is the sum of values in all its unit triangles. The triangle on the right is the same as the other one but with the sub-triangle having the largest value being highlighted. Write a program to determine the sub-triangle with the largest value.

输入:

Your program will be tested on one or more test cases. Each test case is specified in a single line made of integers (separated by spaces.) The first integer is the number of rows in the test case, and the remaining integers are the values of the unit triangles specified in a top-down, left-to-right order. (the first test case in the example below is the same as the one in the figure.) The last line of the input file contains the number 0 (which is not part of the test cases.)The maximum number of rows is 400. The absolute value of a unit triangle is less than 1000.

输出:

Your program will be tested on one or more test cases. Each test case is specified in a single line made of integers (separated by spaces.) The first integer is the number of rows in the test case, and the remaining integers are the values of the unit triangles specified in a top-down, left-to-right order. (the first test case in the example below is the same as the one in the figure.) The last line of the input file contains the number 0 (which is not part of the test cases.)The maximum number of rows is 400. The absolute value of a unit triangle is less than 1000.

样例输入:

3 6 -24 0 12 -10 12 40 -4 6
4 1 1 -1 1 1 -1 1 -1 1 1 -1 1 -1 1 -1 1
0

样例输出:

1. 54
2. 4

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<cmath>
using namespace std;

int f[410][820], val[410][810];

int main()
{
    int n, kase = 0;
    while (cin >> n)
    {
        if (n == 0) break;
        for (int i = 1; i <= n; i++)
        {
            f[i][0] = 0;
            for (int j = 1; j <= 2 * i - 1; j++)
            {
                scanf("%d", &val[i][j]);
                f[i][j] = f[i][j - 1] + val[i][j];
            }
        }
        int ans = -0x3f3f3f3f;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= 2 * i - 1; j += 2)
            {
                int tmp = 0;
                for (int k = i; k <= n; k++)
                {
                    if (j + 2 * (k  - i) > 2 * k - 1) break;
                    tmp += f[k][j + 2 * (k - i)] - f[k][j - 1];
                    if (tmp > ans) ans = tmp;
                    //if (tmp == 0) cout << i << " " << j << " " << k << endl;
                }
            }
        for (int i = n; i >= 1; i--)
        {
            for (int j = 2; j <= 2 * i - 1; j += 2)
            {
                int tmp = 0;
                for (int k = i; k >= 1; k--)
                {
                    if (j - 2  * (i - k) < 1) break;
                    if (j > 2 * k - 1) break;
                    tmp += f[k][j] - f[k][j - 2 * (i - k) - 1];
                    if (tmp > ans) ans = tmp;
                    //if (tmp == 0) cout << i << " " << j << " " << k << endl;
                }
            }
        }
        kase++;
        cout << kase << ". " << ans << endl;
    }
}

  1. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。