题意略。

思路:

I.对于整个区间a1,....,an,必然有一个区间[1,n]与之对应,因为a1,...,an是1,...,n的一个排列,所以在[1,n]中定然有一个最小的数字1,

如果最大的区间[l,r]长度比[1,n]小,那么我们可以知道在[l,r]之外的数字是依然大于1的,这使得1这个数字没有合法的地方可放。

II.起于1左端的区间不可能终于1的右端。

III.数字1左端的部分类似于整体,因为左端也类似地有一个最小的数字。

IV.要想知道整体的方案数有多少,假设可以由f(1,n)算出,那么f(1,n) = f(1,mid - 1) * f(mid + 1,r) * C(n - 1,mid - 1)。

V.这个题递归的顺序可以预先算出来。

#include<bits/stdc++.h>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
const LL maxn = 1e6 + ; inline bool scan_d(int& ret){
char c;int sgn;
if(c = getchar(),c == EOF) return ;
while(c != '-' && (c < '' || c > '')) c = getchar();
sgn = (c == '-') ? - : ;
ret = (c == '-') ? : (c - '');
while(c = getchar(),c >= '' && c <= '') ret = ret * + (c - '');
ret *= sgn;
return ;
} struct node{
int l,r,id;
node(int a = ,int b = ){
l = a,r = b;
}
bool operator<(const node& nd) const{
if(l != nd.l) return l < nd.l;
return r > nd.r;
}
}; int cnt;
LL fac[maxn],inv[maxn];
node store[maxn];
bool flag; LL exgcd(LL a,LL b,LL& x,LL& y){
if(a == && b == ) return -;
if(b == ){
x = ,y = ;
return a;
}
LL d = exgcd(b,a % b,y,x);
y -= a / b * x;
return d;
}
LL rev(LL a,LL n){
LL x,y;
LL d = exgcd(a,n,x,y);
if(d == ) return (x % n + n) % n;
else return -;
}
void init(){
fac[] = ;
for(LL i = ;i < maxn;++i) fac[i] = fac[i - ] * i % mod; inv[maxn - ] = rev(fac[maxn - ],mod);
for(LL i = maxn - ;i >= ;--i){
inv[i] = inv[i + ] * (i + ) % mod;
//printf("inv[%d] == %lld\n",i,inv[i]);
}
}
LL C(int n,int m){
return ((fac[n] * inv[m]) % mod) * inv[n - m] % mod;
}
LL dfs(int l,int r){
//printf("now l == %d r == %d\n",l,r);
if(flag) return ;
if(l > r) return ; if(store[cnt].l != l || store[cnt].r != r){
flag = true;
return ;
}
node cur = store[cnt++];
int mid = cur.id;
LL lft = ,rht = ,c = ;
lft = dfs(l,mid - );
rht = dfs(mid + ,r);
c = C(r - l,mid - l);
//printf("c == %lld\n",c);
//printf("---> %lld\n",(lft * rht % mod) * c % mod);
return (lft * rht % mod) * c % mod;
} int main(){
int cas = ,n;
init();
while(scan_d(n)){
flag = false;
cnt = ;
for(int i = ;i <= n;++i) scan_d(store[i].l);
for(int i = ;i <= n;++i) scan_d(store[i].r),store[i].id = i;
sort(store + ,store + n + );
LL ans = dfs(,n);
printf("Case #%d: %lld\n",cas++,ans);
}
return ;
}

HDU 6044的更多相关文章

  1. HDU 6044 - Limited Permutation | 2017 Multi-University Training Contest 1

    研究一下建树 : /* HDU 6044 - Limited Permutation [ 读入优化,笛卡尔树 ] | 2017 Multi-University Training Contest 1 ...

  2. HDU 6044 Limited Permutation(搜索+读入优化)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6044 [题目大意] 给出两个序列li,ri,现在要求构造排列p,使得对于区间[li,ri]来说, ...

  3. HDU 6044 Limited Permutation 读入挂+组合数学

    Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...

  4. hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】

    题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. HTML--网页练习--(360导航首页的一部分)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. mysql8.0的连接写法

    由于mysql8.0的新特新,所以Driver要写成“com.mysql.cj.jdbc.Driver” url:"jdbc:mysql://host_address:3306/db_nam ...

  3. linux初学者-数据库管理MariaDB篇

    linux初学者-数据库管理MariaDB篇 MariaDB是一种数据库管理系统,是MySQL的一个分支,但是比MySQL更加优秀,可以说是MySQL的替代品.MariaDB使用的是SQL语句.下文将 ...

  4. MySQL不停地自动重启怎么办

    近期,测试环境出现了一次MySQL数据库不断自动重启的问题,导致的原因是强行kill -9 杀掉数据库进程导致,报错信息如下: --24T01::.769512Z [Note] Executing ' ...

  5. git push 出现non-fast-forward的错误

    1.git push origin liu_0909:daily_liu_0909 出现non-fast-forward的错误,证明您的本地库跟远程库的提交记录不一致,即 你的本地库版本需要更新2.g ...

  6. golang 任意类型之间相互转换

    在处理一些参数的时候,可能需要将参数转换为各种类型,这里实现一个通用的转换函数,实现各种类型之间的相互转换. 当然,如果源数据格式和目标数据类型不一致,是会返回错误的.例如将字符串“一二三”转换为数值 ...

  7. DotSpatial安装、类库引用方法

    解决VS工具栏添加DotSpatial后,控件不全问题. 注意注意注意:不要使用Nuget安装DotSpatial!!! 我在Nuget上把所有DotSpatial的版本都安装了一遍,都缺少控件,然后 ...

  8. spring boot中的声明式事务管理及编程式事务管理

    这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...

  9. .NET Core 3.0深入源码理解HttpClientFactory之实战

      写在前面 前面两篇文章透过源码角度,理解了HttpClientFactory的内部实现,当我们在项目中使用时,总会涉及以下几个问题: HttpClient超时处理以及重试机制 HttpClient ...

  10. MySQL-5.7.21非图形化下载、安装、连接问题记录

    1.安装包下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-winx64.zip 官网:https://www.mysql.co ...