首页 > ACM题库 > HDU-杭电 > hdu 2311 Cut the Cake[解题报告]C++
2014
01-05

hdu 2311 Cut the Cake[解题报告]C++

Cut the Cake

问题描述 :

Today is the birthday of Mr. Bon Vivant, who is known as one of the greatest p�issiers in the world. Those who are invited to his birthday party are gourmets from around the world. They are eager to see and eat his extremely creative cakes. Now a large box-shaped cake is being carried into the party. It is not beautifully decorated and looks rather simple, but it must be delicious beyond anyone’s imagination. Let us cut it into pieces with a knife and serve them to the guests attending the party.

The cake looks rectangular, viewing from above (Figure C-1). As exemplified in Figure C-2, the cake will iteratively be cut into pieces, where on each cut exactly a single piece is cut into two smaller pieces. Each cut surface must be orthogonal to the bottom face and must be orthogonal or parallel to a side face. So, every piece shall be rectangular looking from above and every side face vertical.

Figure C-1: The top view of the cake

Figure C-2: Cutting the cake into pieces

Piece sizes in Figure C-2 vary significantly and it may look unfair, but you don’t have to worry. Those guests who would like to eat as many sorts of cakes as possible often prefer smaller pieces. Of course, some prefer larger ones.
Your mission of this problem is to write a computer program that simulates the cutting process of the cake and reports the size of each piece.

输入:

The input is a sequence of datasets, each of which is of the following format.

n w d
p1 s1

pn sn
 

The first line starts with an integer n that is between 0 and 100 inclusive. It is the number of cuts to be performed. The following w and d in the same line are integers between 1 and 100 inclusive. They denote the width and depth of the cake, respectively. Assume in the sequel that the cake is placed so that w and d are the lengths in the east-west and north-south directions, respectively.

Each of the following n lines specifies a single cut, cutting one and only one piece into two. pi is an integer between 1 and i inclusive and is the identification number of the piece that is the target of the i-th cut. Note that, just before the i-th cut, there exist exactly i pieces. Each piece in this stage has a unique identification number that is one of 1, 2, …, i and is defined as follows:

* The earlier a piece was born, the smaller its identification number is.
* Of the two pieces born at a time by the same cut, the piece with the smaller area (looking from above) has the smaller identification number. If their areas are the same, you may define as you like the order between them, since your choice in this case has no influence on the final answer.

Note that identification numbers are adjusted after each cut.
si is an integer between 1 and 1000 inclusive and specifies the starting point of the i-th cut. From the northwest corner of the piece whose identification number is pi, you can reach the starting point by traveling si in the clockwise direction around the piece. You may assume that the starting point determined in this way cannot be any one of the four corners of the piece. The i-th cut surface is orthogonal to the side face on which the starting point exists.

The end of the input is indicated by a line with three zeros.

输出:

The input is a sequence of datasets, each of which is of the following format.

n w d
p1 s1

pn sn
 

The first line starts with an integer n that is between 0 and 100 inclusive. It is the number of cuts to be performed. The following w and d in the same line are integers between 1 and 100 inclusive. They denote the width and depth of the cake, respectively. Assume in the sequel that the cake is placed so that w and d are the lengths in the east-west and north-south directions, respectively.

Each of the following n lines specifies a single cut, cutting one and only one piece into two. pi is an integer between 1 and i inclusive and is the identification number of the piece that is the target of the i-th cut. Note that, just before the i-th cut, there exist exactly i pieces. Each piece in this stage has a unique identification number that is one of 1, 2, …, i and is defined as follows:

* The earlier a piece was born, the smaller its identification number is.
* Of the two pieces born at a time by the same cut, the piece with the smaller area (looking from above) has the smaller identification number. If their areas are the same, you may define as you like the order between them, since your choice in this case has no influence on the final answer.

Note that identification numbers are adjusted after each cut.
si is an integer between 1 and 1000 inclusive and specifies the starting point of the i-th cut. From the northwest corner of the piece whose identification number is pi, you can reach the starting point by traveling si in the clockwise direction around the piece. You may assume that the starting point determined in this way cannot be any one of the four corners of the piece. The i-th cut surface is orthogonal to the side face on which the starting point exists.

The end of the input is indicated by a line with three zeros.

样例输入:

3 5 6
1 18
2 19
1 2
3 4 1
1 1
2 1
3 1
0 2 5
0 0 0

样例输出:

4 4 6 16
1 1 1 1
10

2012 Multi-University Training Contest 3

1009题

没看到后来加的说明,以为都是矩形区域,直接AC了…………

#include <cstdio>
#include <cstring>
const int MAXN = 1005;
int left[3][MAXN][MAXN], right[3][MAXN][MAXN], top[3][MAXN][MAXN];
char a[MAXN][MAXN];

inline int min(const int &x, const int &y)
{
    return x < y ? x : y;
}

inline int max(const int &x, const int &y)
{
    return x > y ? x : y;
}

int main()
{
    int t;
    int n, m;
    scanf("%d", &t);
    memset(a, 0, sizeof(a));
    for(int cas=1;cas<=t;++cas)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
        {
            scanf("%s", a[i]+1);
        }
        memset(left, 0, sizeof(left));
        memset(right, 0, sizeof(right));
        memset(top, 0, sizeof(top));
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;++j)
            {
                if(a[i][j] == 'B')
                {
                    top[0][i][j] = 0;
                    top[1][i][j] = top[1][i-1][j] + 1;
                    left[0][i][j] = 0;
                    left[1][i][j] = left[1][i][j-1] + 1;
                }
                else
                {
                    top[0][i][j] = top[0][i-1][j] + 1;
                    top[1][i][j] = 0;
                    left[0][i][j] = left[0][i][j-1] + 1;
                    left[1][i][j] = 0;
                }
                if(a[i][j] == a[i-1][j])
                {
                    top[2][i][j] = 1;
                }
                else
                {
                    top[2][i][j] = top[2][i-1][j] + 1;
                }
                if(a[i][j] == a[i][j-1])
                {
                    left[2][i][j] = 1;
                }
                else
                {
                    left[2][i][j] = left[2][i][j-1] + 1;
                }
            }
            for(int j=m;j>=1;--j)
            {
                if(a[i][j] == 'B')
                {
                    right[0][i][j] = 0;
                    right[1][i][j] = right[1][i][j+1] + 1;
                }
                else
                {
                    right[0][i][j] = right[0][i][j+1] + 1;
                    right[1][i][j] = 0;
                }
                if(a[i][j] == a[i][j+1])
                {
                    right[2][i][j] = 1;
                }
                else
                {
                    right[2][i][j] = right[2][i][j+1] + 1;
                }
            }
        }
        int ans = 4;
        int maxLeft[3], maxRight[3];
        for(int j=1;j<=m;++j)
        {
            for(int i=1;i<=n;++i)
            {
                for(int k=0;k<3;++k)
                {
                    if(top[k][i][j] == 1)
                    {
                        maxLeft[k] = left[k][i][j];
                        maxRight[k] = right[k][i][j];
                    }
                    else
                    {
                        maxLeft[k] = min(maxLeft[k], left[k][i][j]);
                        maxRight[k] = min(maxRight[k], right[k][i][j]);
                    }
                    ans = max(ans, (maxRight[k] + maxLeft[k] - 1) * 2 + top[k][i][j] * 2);
                }
            }
        }
        printf("Case #%d: %d\n", cas, ans);
    }
    return 0;
}

解题转自:http://blog.csdn.net/cyberzhg/article/details/7815384


  1. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!