Lining Up

Posted zangzang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lining Up相关的知识,希望对你有一定的参考价值。

问题 H: Lining Up

时间限制: 1 Sec  内存限制: 128 MB
提交: 45  解决: 30
[提交][状态][讨论版][命题人:admin]

题目描述

There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is Ai.
Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 109+7. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0.

Constraints
1≤N≤105
0≤Ai≤N?1

输入

The input is given from Standard Input in the following format:
N
A1 A2 … AN

输出

Print the number of the possible orders in which they were standing, modulo 109+7.

样例输入

5
2 4 4 0 2

样例输出

4

提示

There are four possible orders, as follows:
·2,1,4,5,3
·2,5,4,1,3
·3,1,4,5,2
·3,5,4,1,2

#include <iostream>
#include <cstdio>

using namespace std;

int a[100005]= {0};
int n;
int pd1()
{
    for(int i=1; i<=n-1; i+=2)
    {
        if(a[i]!=2)
        {
            return 0;
        }
    }
    return 1;
}
int pd2()
{
    if(a[0]!=1)
        return 0;
    int t = n/2;
    for(int i=1; i<=t; i++)
    {
        if(a[i*2]!=2)
        {
            return 0;
        }
    }
    return 1;
}
int main()
{

    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        int x;
        scanf("%d",&x);
        a[x]++;
    }
    if(n%2==0)
    {
        int flag = pd1();
        if(flag==0)
            printf("0");

        else
        {
            long long int t = 1;
            int p = n/2;
            for(int i=0; i<p; i++)
            {
                t*=2;
                t%=1000000007;
            }
            printf("%lld",t);
        }
    }
    else
    {
        int flag = pd2();
        if(flag==0)
            printf("0");
        else
        {
            long long int t = 1;
            int p = n/2;
            for(int i=0; i<p; i++)
            {
                t*=2;
                t%=1000000007;
            }
            printf("%lld",t);
        }
    }
}

规律题 奇数偶数分情况讨论

样例

7
6 4 0 2 4 0 2   输出:0;

 

 

8
7 5 1 1 7 3 5 3    输出:16;

以上是关于Lining Up的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1118 Lining Up

UVA270-Lining Up

POJ 1118 Lining Up

Lining Up(在一条直线上的最大点数目,暴力)

题解 AT2271 Lining Up

带有片段的 Android Up 按钮未显示完整片段