题意略。

思路:

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. php laravel5.6引入geetest 行为验证

    php laravel5.6引入geetest 行为验证 使用必要性 网站和APP,在所有可能被机器行为攻击的场景,例如但不限于注册.登录.短信接口.查询接口.营销活动.发帖评论等等,都可以部署使用「 ...

  2. [学习笔记] MySQL入门

    一.MySQL的安装与简单使用 ubuntu16.04下安装MySQL: sudo apt-get update sudo apt-get install mysql-server mysql-cli ...

  3. <表格>

    一.列表 信息资源的一种展示形式 二.列表的分类 1.有序列表 <ol> <li>列表项1</li> <li>列表项2</li> </ ...

  4. Mysql Atm取款机系统模拟案例

    #创建ATM数据库 CREATE DATABASE ATM; #创建用户CREATE USER `ATMMaster` IDENTIFIED BY '1234';GRANT ALL ON ATM.* ...

  5. 前后端分离 之vue-cli 搭建项目mac 系统讲解

    前端项目搭建必备技术 webpack nodejs 搭建 vue-cli 的安装 以上技术自行了解安装 一:创建前端项目 采用vue-cli 脚手架 1:终端执行如下命令 vue init webpa ...

  6. tomcat常见面试题目问答Top10

    Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,它 ...

  7. 【python-Django开发】Django 配置MySQL数据库讲解!!!

    官方文档请阅读:https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-db-api-drivers 配置MySQL数据库 1. 新建M ...

  8. html+css+dom补充

    补充1:页面布局 一般像京东主页左侧右侧都留有空白,用margin:0 auto居中,一般.w. <!DOCTYPE html> <html lang="en"& ...

  9. [ PyQt入门教程 ] PyQt5基本控件使用:消息弹出、用户输入、文件对话框

    本文主要介绍PyQt界面实现中常用的消息弹出对话框.提供用户输入的输入框.打开文件获取文件/目录路径的文件对话框.学习这三种控件前,先想一下它们使用的主要场景: 1.消息弹出对话框.程序遇到问题需要退 ...

  10. 分布式ID系列(4)——Redis集群实现的分布式ID适合做分布式ID吗

    首先是项目地址: https://github.com/maqiankun/distributed-id-redis-generator 关于Redis集群生成分布式ID,这里要先了解redis使用l ...