HDU 6044 Limited Permutation(递归)

18 篇文章 0 订阅

Description
一个1~n的排列p[1],p[2],…,p[n],现在给出p[i]在该排列中作为最小值的最大存在区间[L[i],R[i]],问满足这些限制条件的排列p有多少种
Input
多组用例,每组用例首先输入一整数n表示排列长度,之后n个整数L[i]和n个整数R[i]表示排列每个值作为最小值的最大存在区间,以文件尾结束输入(1<=n<=1e6)
Output
对于每组用例,输出满足这些限制条件的排列p的方案数
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
Solution
显然1的区间应该是[1,n],找到1的位置pos之后,区间被分成了两部分[1,pos-1]和[pos+1,n],每部分中有一个最小值,找到最小值后又可以把区间分成两部分,所以每次分出的两部分基于同样的子问题,而且注意到最小值确定后,分成的两部分中数是相对大小,所以只要从n-1个数中选取pos-1个数给左边部分即可,方案数C(n-1,pos-1),剩下的问题在于如果快速找到区间最小值,也即对一个区间[L,R],如何快速找到i使得[L[i],R[i]]=[L,R],注意到把每次把区间分成两部分的过程看过根节点和左右子树的话我们会得到一颗笛卡尔树,且该棵树满足其前序遍历序列的L值递增,R值递减,那么我们在递归之前把区间按左端点为第一关键字升序排,右端点为第二关键字降序排,前序遍历这棵笛卡尔树递归求解,每次只需要拿出还没用的第一个区间即为最小值,如果这个区间与当前求解的区间不同说明无解,注意输入很大要用fread
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
namespace fastIO 
{
    #define BUF_SIZE 100000
    //fread -> read
    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 void read(int &x) 
    {
        char ch;
        while(blank(ch=nc()));
        if(IOerror)return;
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
    }
    #undef BUF_SIZE
};
using namespace fastIO;
using namespace std;
typedef long long ll;
const int maxn=1000001,mod=1e9+7;
int n;
struct node
{
    int l,r,id;
    bool operator<(const node&b)const
    {
        if(l!=b.l)return l<b.l;
        return r>b.r;
    }
}a[maxn];
int fact[maxn],inv[maxn];
void init()
{
    fact[0]=1;
    for(int i=1;i<2*maxn;i++)fact[i]=(ll)i*fact[i-1]%mod;//求i! 
    inv[0]=inv[1]=1;
    for(int i=2;i<maxn;i++)inv[i]=mod-(int)(mod/i*(ll)inv[mod%i]%mod);//线性求1~n逆元 
    for(int i=1;i<maxn;i++)inv[i]=(ll)inv[i-1]*inv[i]%mod;//预处理i!的逆元 
}
int C(int n,int m)
{
    return (ll)fact[n]*inv[m]%mod*inv[n-m]%mod;
}
int flag,cnt;
int Solve(int L,int R)
{
    if(!flag)return 0;
    if(L>R)return 1;
    cnt++;
    if(L==R)
    {
        if(a[cnt].l!=L||a[cnt].r!=R)return flag=0;
        return 1;
    }
    if(a[cnt].l!=L||a[cnt].r!=R)return flag=0;
    int pos=a[cnt].id;
    if(pos<L||pos>R)return flag=0;
    return (ll)Solve(L,pos-1)*Solve(pos+1,R)%mod*C(R-L,pos-L)%mod;
}
int main()
{
    init();
    int res=1;
    while(read(n),!IOerror)
    {
        flag=1,cnt=0;
        for(int i=1;i<=n;i++)read(a[i].l),a[i].id=i;
        for(int i=1;i<=n;i++)read(a[i].r);
        sort(a+1,a+n+1);
        printf("Case #%d: %d\n",res++,Solve(1,n));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值