HDU 6044 Limited Permutation 想法

版权声明:有错误欢迎大家指出。转载请注明出处~ https://blog.csdn.net/black_miracle/article/details/76474344

Limited Permutation

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1714    Accepted Submission(s): 463


Problem Description
As to a permutation  p1,p2,,pn from  1 to  n, it is uncomplicated for each  1in to calculate  (li,ri) meeting the condition that  min(pL,pL+1,,pR)=pi if and only if  liLiRri for each  1LRn.

Given the positive integers  n (li,ri)  (1in), you are asked to calculate the number of possible permutations  p1,p2,,pn from  1 to  n, meeting the above condition.

The answer may be very large, so you only need to give the value of answer modulo  109+7.
 

Input
The input contains multiple test cases.

For each test case:

The first line contains one positive integer  n, satisfying  1n106.

The second line contains  n positive integers  l1,l2,,ln, satisfying  1lii for each  1in.

The third line contains  n positive integers  r1,r2,,rn, satisfying  irin for each  1in.

It's guaranteed that the sum of  n in all test cases is not larger than  3106.

Warm Tips for C/C++: input data is so large (about 38 MiB) that we recommend to use  fread() for buffering friendly.
size_t fread(void *buffer, size_t size, size_t count, FILE *stream); // reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by buffer; the total number of elements successfully read is returned.
 

Output
For each test case, output " Case #xy" in one line (without quotes), where  x indicates the case number starting from  1 and  y denotes the answer of corresponding case.
 

Sample Input
 
  
3 1 1 3 1 3 3 5 1 2 2 4 5 5 2 5 5 5
 

Sample Output
 
  
Case #1: 2 Case #2: 3

题意:给你n个l r 代表[li,ri]的最小值是pi且[li,ri+1]或[li-1,ri]的最小值不是pi  问能构造出多少个这样的序列


题解:对于当前区间  如果我们找不到有[l,r]为当前区间  那么方案数就是0

假设有[l,l+x],[l+x+1,r]两个区间

如果[l,l+x]是[l,r]这个区间的最小值  那么会并吞掉[l+x+1,r]

反之也成立

所以组合数搞一搞就ok。。。


#include <cstdio>
#include <algorithm>
#include <utility>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1000000007;
const int N=1000010;
namespace fastIO{
    #define BUF_SIZE 100000
    bool IOerror=0;
    inline char nc(){
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend){
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1){
                IOerror=1;
                return -1;
            }
        }return *p1++;
    }
    inline bool blank(char ch){
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    inline bool read(int &x){
        char ch;
        while(blank(ch=nc()));
        if(IOerror)return 0;
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
        return 1;
    }
    #undef BUF_SIZE
};
namespace Comb{
    int f[N],rf[N];
    LL inv(LL a,LL m){return(a==1?1:inv(m%a,m)*(m-m/a)%m);}
    LL C(int n,int m){
        if(m<0||m>n)return 0;
        return (LL)f[n]*rf[m]%mod*rf[n-m]%mod;
    }
    void init(){
        f[0]=1;for(int i=1;i<=1000000;i++)f[i]=(LL)f[i-1]*i%mod;
        rf[1000000]=inv(f[1000000],mod);
        for(int i=1000000;i;i--)rf[i-1]=(LL)rf[i]*i%mod;
    }
}
int l[N],r[N],n;
map<P,int> M;
LL dfs(int l,int r){
    // printf("%d %d\n",l,r);
    if(l>r)return 1;
    int x=M[P(l,r)]; if(!x)return 0;
    return Comb::C(r-l,r-x)*dfs(l,x-1)%mod*dfs(x+1,r)%mod;
}
int main(){
    Comb::init();
    for(int cas=1;fastIO::read(n);cas++){
        M.clear();
        for(int i=1;i<=n;i++)fastIO::read(l[i]);
        for(int i=1;i<=n;i++)fastIO::read(r[i]);
        for(int i=1;i<=n;i++)M[P(l[i],r[i])]=i;
        LL ans=dfs(1,n);
        printf("Case #%d: %lld\n",cas,ans);
    }return 0;
}


猜你喜欢

转载自blog.csdn.net/black_miracle/article/details/76474344
今日推荐