zoukankan      html  css  js  c++  java
  • codeforces 341C Iahub and Permutations(组合数dp)

    C. Iahub and Permutations
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.

    The girl finds an important permutation for the research. The permutation contains n distinct integers a1a2, ..., an (1 ≤ ain). She replaces some of permutation elements with -1 value as a revenge.

    When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).

    Input

    The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.

    It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.

    Output

    Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).

    Sample test(s)
    input
    5
    -1 -1 4 3 -1
    
    output
    2
    
    Note

    For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.



    题目大意: 给你1~n个数字,要求错排,不过-1说明这个数字还不确定,但是给定了非-1数字,说明这个数字已经定了下来。开始很莽撞的直接用错排数计算。。。实际上不然,填数字的时候,有一些是可以乱填没有限制的,因为这些数字已经被开始填了,有一些则有限制,比如给两组数据。

    5
    -1 -1 4 3 -1
    限 限      限
    5
    -1 -1 2 5 -1
    限 非      非
    看第二组数据,错排是a[i]!=i,而2和5都被占用了,那么2,5都成了非限制的了,然而1是限制的。

    解题思路:开始看了传说中的frog蛙神的博客,表示看了一晚上都不解,若菜都是一把泪水啊!边看边问,终于理解了其中的要义,感谢frog1902的讲解。


    题目地址:C. Iahub and Permutations

    下面是蛙神写的思路:

    显然所有已经填好的位置可以不管,我们只看 a[i] = -1 的位置。

    x 表示目前有多少个位置使得a[i] = -1 且数字 i 已经被填在某个位置上,称为无限制位置。

    y 表示目前有多少个位置使得a[i] = -1 且数字 i 没有被填在某个位置上,称为有限制位置。


    那么,最一开始的时候,我们先把 y 个有限制位置抛弃不看。

    因为a[i] = -1 且 i 也没有出现在任何位置上,所以忽略不看不会有影响。


    现在我们先把 x 个无限制的位置填好。

    由于有 x 个数的 a[i] = -1 但是数字 i 已经被使用了。所以也一定有 x 个数的a[i] != -1但是数字 i 还没被用。

    就把这 x 个没使用的数字放在 x 个被填的位置上。方法数是 x! 。


    现在我们把 y 个无限制的位置一个接一个的加进来

    用 d[i] 表示已经有多少个无限制的位置被加进来,且不违反错排的规则。

    显然d[0] = x ! 。我们所要求的就是d[y].


    前面都比较好理解,主要就是下面这一部分,比较难理解了。

    为了更好的理解,我拿个例子辅助说明。

    如果此时我们把第 i 个有限制的位置加进来。分以下几种情况:

    1)我们从 x 个无限制的的位置中找一个 j ,令a[i] = a[j],a[j] = i。规约到d[i - 1]的方案数。

    5
    -1 -1 2 5 -1
    限 非      非
    从2,5中选一个,使得a[1]=2,a[2]=1,就变成了d[i-1].

    2)我们从i - 1个有限制的位置中找一个位置 j ,令a[i] = j,a[j] = i。规约到d[i - 2]的方案数。

    5
    -1 -1 3 4 -1
    限 限      限
    假如此时a[2]=5,a[5]=2,那么就变成了d[i-2].

    3)我们从i - 1个有限制的位置中找一个位置 j ,令a[i] = j,但是a[j] != i。规约到d[i - 1]的方案数。也就相当于由原来的 d[j] != j 限制变为了d[j] != i,其它限制不变。

    5
    -1 -1 3 4 -1
    限 限      限
    假如此时a[2]=5,a[5]=1,那么就变成了d[i-1].

    AC代码:

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    int mod=1e9+7;
    __int64 d[2002];
    int a[2002];
    
    __int64 cal(int p)
    {
        __int64 ans=1;
        int i;
        for(i=2;i<=p;i++)
            ans=(ans*i)%mod;
        return ans;
    }
    
    int main()
    {
        int n,x,y;
        while(~scanf("%d",&n))
        {
            int cnt=0,t=0;  //cnt记录可以填的个数,t记录填的数字被占用的个数
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                if(a[i]==-1)
                    cnt++;
            }
            for(int i=1;i<=n;i++)
            {
                if(a[i]>0)
                {
                    if(a[a[i]]==-1)
                        t++;
                }
            }
    
            x=t;  //x是无限制的个数
            y=cnt-t;
            d[0]=cal(x);   //d[0]=x!
            for(int i=1;i<=y;i++)
            {
                d[i]=((x+i-1)*d[i-1])%mod;
                if(i>1)
                    d[i]=(d[i]+(i-1)*d[i-2])%mod;
            }
            printf("%I64d
    ",d[y]);
        }
        return 0;
    }
    
    /*
    5
    -1 -1 4 3 -1
    5
    -1 -1 4 -1 -1
    5
    -1 -1 2 5 -1
    */
    



  • 相关阅读:
    poj 1733 Parity game
    poj 1456 Supermarket
    bzoj 1304 [CQOI 2009] 叶子的染色
    51Nod 1667 概率好题
    2015年阿里巴巴校招研发工程师在线笔试题汇总
    从字符串常量起说内存分配
    字符串笔面试题
    排序算法(4)-线性时间排序
    华为2015校园招聘机试
    笔画宽度变化(C++和matlab算法)
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3356075.html
Copyright © 2011-2022 走看看