hdu 6044 Limited Permutation

题意:给你n个限制,让你求长度为n的排列列有多少种。每个限制描述为,从li 到 ri这段区间,ai为最小值。

分析:对于一个描述为[1,n] 的限制,它所对应的位置一定是1到n的最小值也就是1,那么这个位置就把区间分成了两份。假设这个位置左边有k个,然后从剩下的n-1个数中选出k个放在左边,都是符合题意的。然后把两个小区间做同样的处理。就得到了答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mo 1000000007
typedef long long ll;
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;

const int N = 3e6 + 5;
struct p{
    int l, r, id;   
}a[N];
ll mul[N];
ll ans, tot;
int pos;

ll quickM(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1)ans = ans * a % mo;
        b >>= 1;
        a = a * a % mo;
    }
    return ans;
}

int cmp(p a, p b){
    return a.l < b.l || a.l == b.l && a.r > b.r;
}

ll C(ll n, ll m){
    return mul[n] * quickM(mul[m], mo - 2) % mo * quickM(mul[n - m], mo - 2) % mo;  
}

void dfs(int l, int r){
    int pos = tot;
    if(r < l) return;
    if(a[tot].l != l || a[tot].r != r) ans = 0;
    if(ans == 0) return;
    if(l == r) {
        tot++;
        return;
    }
    ans = ans * C(r - l, a[tot].id - l) % mo;
    tot++;
    dfs(l, a[pos].id - 1);
    dfs(a[pos].id + 1, r);
}

int main(){
    int n, kase = 0;
    mul[0] = 1;
    for(ll i = 1; i <= 1e6; i++) mul[i] = mul[i - 1] * i % mo; 
    while(read(n), !fastIO::IOerror){
        ans = tot = 1;
        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, cmp);
        dfs(1, n);
        printf("Case #%d: %lld\n", ++kase, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值