UVA1330 LA3029 POJ1964 HDU1505 City Game【最大子段和+DP】

City Game
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 6449 Accepted: 2590

Description

Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.
Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you’re building stands is 3$.
Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.

Input

The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are:
R – reserved unit
F – free unit
In the end of each area description there is a separating line.

Output

For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.

Sample Input

2
5 6
R F F F F F
F F F F F F
R R R F F F
F F F F F F
F F F F F F

5 5
R R R R R
R R R R R
R R R R R
R R R R R
R R R R R

Sample Output

45
0

Source

Southeastern Europe 2004

问题链接UVA1330 LA3029 POJ1964 HDU1505 City Game
问题简述:(略)
问题分析
    这个问题需要考虑时间复杂度,否则会TLE。
    使用二维滑动窗口法可以AC,参见参考链接。
    追加了2个题解。前缀和+滑动窗口法中,对列求前缀和在POJ中会TLE,对行求前缀和则可以AC,数据的原因,O(n^3)时间复杂度是不可接受的。前缀和+DP也有同样问题。
程序说明:(略)
参考链接【滑动窗口】uva 1330 City Game
题记:(略)

AC的C++语言程序如下:

/* UVA1330 LA3029 POJ1964 HDU1505 City Game */

#include <iostream>
#include <cstdio>

using namespace std;

const int N = 1000;
int a[N][N], up[N][N], l[N][N], r[N][N];

int main()
{
    int k, m, n;
    scanf("%d", &k);
    while(k--) {
        scanf("%d%d", &m, &n);
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++) {
                char s[2];
                scanf("%s", s);
                if(s[0] == 'F') a[i][j] = 0;
                else a[i][j] = 1;
            }

        int ans = 0;
        for(int i = 0; i < m; i++) {
            int left = -1, right = n;
            for(int j = 0; j < n; j++) {
                if(a[i][j] == 1) {
                    up[i][j] = l[i][j] = 0;
                    left = j;
                } else {
                    up[i][j] = i == 0 ? 1 : up[i - 1][j] + 1;
                    l[i][j] = i == 0 ? left + 1 : max(l[i - 1][j], left + 1);
                }
            }
            for(int j = n - 1; j >= 0; j--)
                if(a[i][j] == 1) {
                    r[i][j] = m;
                    right= j;
                } else {
                    r[i][j] = i == 0 ? right - 1 : min(r[i - 1][j], right - 1);
                    ans = max(ans, up[i][j] * (r[i][j] - l[i][j] + 1));
                }
        }

        printf("%d\n", ans * 3);
    }

    return 0;
}

AC的C++语言程序(前缀和+滑动窗口法,HDU中TLE)如下:

/* UVA1330 LA3029 POJ1964 HDU1505 City Game */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1000;
int sum[N + 1][N + 1];
const int M1000 = - (N + 1) * (N + 1);

int main()
{
    int k, m, n, a;
    scanf("%d", &k);
    while(k--) {
        memset(sum, 0, sizeof(sum));

        scanf("%d%d", &m, &n);
        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                char s[2];
                scanf("%s", s);
                if(s[0] == 'F') a = 1;
                else a = M1000;
                sum[i][j] = sum[i - 1][j] + a;      // 计算前缀和
            }
        }

        // 滑动窗口法:按第i-j行,对所有的列计算最大子段和
        int maxSum = 0, subSum;
        for(int i = 1; i <= m; i++)
            for(int j = i; j <= m; j++) {
                subSum = 0;
                for(int k = 1; k <= n; k++) {
                    subSum += sum[j][k] - sum[i - 1][k];
                    if(subSum > maxSum) maxSum = subSum;
                    if(subSum < 0) subSum = 0;
                }
            }

        printf("%d\n", maxSum * 3);
    }

    return 0;
}

AC的C++语言程序(前缀和+DP,HDU中TLE)如下:

/* UVA1330 LA3029 POJ1964 HDU1505 City Game */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1000;
int sum[N + 1][N + 1], dp[N + 1];
const int M1000 = - (N + 1) * (N + 1);

int main()
{
    int k, m, n, a;
    scanf("%d", &k);
    while(k--) {
        memset(sum, 0, sizeof(sum));

        scanf("%d%d", &m, &n);
        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                char s[2];
                scanf("%s", s);
                if(s[0] == 'F') a = 1;
                else a = M1000;
                sum[i][j] = sum[i - 1][j] + a;      // 计算前缀和
            }
        }

        // 按第i-j行,对所有的列计算最大子段和
        int ans = 0;
        for(int i = 1; i <= m; i++)
            for(int j = i; j <= m; j++) {
                memset(dp, 0, sizeof(dp));
                for(int k = 1; k <= n; k++) {
                    if(dp[k - 1] > 0)
                        dp[k] = dp[k - 1] + (sum[j][k] - sum[i - 1][k]);
                    else
                        dp[k] = sum[j][k] - sum[i - 1][k];
                    ans = max(ans, dp[k]);
                }
            }

        printf("%d\n", ans * 3);
    }

    return 0;
}

TLE的C++语言程序如下:

/* UVA1330 LA3029 POJ1964 HDU1505 City Game */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1000;
int sum[N + 1][N + 1];
const int M1000 = - (N + 1) * (N + 1);

int main()
{
    int k, m, n, a;
    scanf("%d", &k);
    while(k--) {
        memset(sum, 0, sizeof(sum));

        scanf("%d%d", &m, &n);
        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++) {
                char s[2];
                scanf("%s", s);
                if(s[0] == 'F') a = 1;
                else a = M1000;
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a;
            }

        int ans = 0;
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                for (int p = i; p <= m; p++)
                    for (int q = j; q <= n; q++) {
                        int t = sum[p][q] - sum[p][j - 1] - sum[i - 1][q] + sum[i - 1][j - 1];
                        ans = max(ans, t);
                    }

        printf("%d\n", ans * 3);
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105401132
今日推荐