hihocoder1257(构造)(2015北京ACM/ICPC)

题意:

给你n条蛇,a[i]的长度为i,要求组成一个矩形。奇数蛇可折叠奇数次,偶数蛇折叠偶数次,然后按蛇的次序输出

(即一条蛇的输出只能是一个方向的)

2 3

1 2

1 3 2 3

1 1 2 1 2 2

2 5

1 4

1 5 2 5

1 1 2 1 2 2

1 2 1 3 2 3 2 4

3 5

3 4

1 4 1 5

2 4 2 5 3 5

2 2 2 3 3 3 3 2

3 1 2 1 1 1 1 2 1 3

思路:

构造的话一般都是找规律,通过前面的推出后面的:

首先我们可以发现矩形的长宽是取决于n

1: 1 1                          2:1 2

3: 2 3                          4:2 5

5: 3 4                          6:3 7

然后是找矩形的关系

我们可以发现偶数矩形可以由它的前一个组成,即在后面加上

3: 1 3 3              4:1 3 3 4 4

   2 2 3                2 2 3 4 4

然后看奇数矩形,通过长宽不停的从前找规律可以发现f[n]与f[n-3]有一定的关系

1 3 3 4 4                    1 3 3 4 4 5 7

2 2 3 4 4         -->        2 2 3 4 4 5 7

                             6 6 6 5 5 5 7

                             6 6 6 7 7 7 7


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

using namespace std;


void fin(int cur)
{
    if(cur == 1)
    {
        printf("1 1\n");
        return ;
    }
    if(cur == 2)
    {
        printf("1 1\n");
        printf("1 2 1 3\n");
        return;
    }
    if(cur == 3)
    {
        printf("2 1\n");
        printf("1 1 1 2\n");
        printf("1 3 2 3 2 2\n");
        return;
    }
    int tx = (cur+1)/2;
    int ty = (cur%2)? tx*2-1:tx*2+1;

    if(cur % 2 == 0)
    {
        fin(cur-1);
        for(int i = 1; i <= cur/2; i ++)
            printf("%d %d ",i,ty-1);
        for(int i = cur/2; i >= 1; i--)
            printf("%d %d ",i,ty);
        printf("\n");
        return ;
    }
    else
    {
        fin(cur-3);
        for(int i = 1; i <= (cur-2)/2; i++)
            printf("%d %d ",i,ty-1);
        for(int i = 1; i <= (cur-2)/2+1; i++)
            printf("%d %d ",tx-1,ty-i);
        printf("\n");

        for(int i = 1; i <= (cur-1)/2; i++)
            printf("%d %d ",tx-1,i);
        for(int i = 0; i <= (cur-1)/2-1; i++)
            printf("%d %d ",tx,(cur-1)/2-i);
        printf("\n");

        for(int i = 0; i < cur/2+1; i++)
            printf("%d %d ",tx,ty-cur/2+i);
        for(int i = 0; i < cur/2; i++)
            printf("%d %d ",tx-i-1,ty);
        printf("\n");
        return ;
    }
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        printf("%d %d\n",(n+1)/2,(n%2)? (n+1)/2*2-1:(n+1)/2*2+1);
        fin(n);
    }
    return 0;
}

  

posted @ 2015-11-22 15:54  Przz  阅读(260)  评论(0编辑  收藏  举报