题意略。

思路:

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. iOS 图解弹幕功能的实现

    先来看一张效果图(LICEcap录制的有点卡, 凑合看) 理一下大概流程: 接下来实现: 弹幕视图从底部弹上来, 依次动画向上滚动, 出屏幕就移除加入重用队列, 下次使用. 定义相关属性: 1. 根据 ...

  2. linux初学者-磁盘配额篇

    linux初学者-磁盘配额篇 在使用磁盘时,只要有权限,不管什么用户都可以在其中写入数据,但是如果是多人在一个磁盘中写入数据,就会出现争议,如果一个用户将磁盘空间用完,那么其他人无法再使用.如下图所示 ...

  3. flink入门实战总结

    随着大数据技术在各行各业的广泛应用,要求能对海量数据进行实时处理的需求越来越多,同时数据处理的业务逻辑也越来越复杂,传统的批处理方式和早期的流式处理框架也越来越难以在延迟性.吞吐量.容错能力以及使用便 ...

  4. jQuery中的事件与动画 笔记整理

    一. jQuery中的事件 jQuery事件是对javaScript事件的封装. 1.基础事件 在javaScript中,常用的基础事件有鼠标事件.键盘事件.window事件.表单事件.事件的绑定和处 ...

  5. MicroPython TPYBoard v201 简易家庭气象站的实现过程

    转载请注明文章来源,更多教程可自助参考docs.tpyboard.com,QQ技术交流群:157816561,公众号:MicroPython玩家汇 前言 上一篇教程中我们实现了一个简单网页的显示.本篇 ...

  6. mybatis动态插入数据库

    <insert id="dynamicAddUser"> insert into t_user <!-- trim 对所有的表中列名 进行动态处理 --> ...

  7. 【Mac】解压文件夹中文乱码

    Mac 用的英文系统,发现下载含中文的文件后,解压为乱码,如图所示: 解决方法: 下载一个解压软件:"The Unarchiver" 安装后设置下,如图: 之后设置压缩文件的默认打 ...

  8. codeforces 339A.Helpful Maths B.Xenia and Ringroad 两水题

    A.题意就是把字符串里面的数字按增序排列,直接上代码. #include <string.h> #include <stdio.h> #include <algorith ...

  9. solr使用心得

    /**   * @author  zhipeng  * @date 创建时间:2015-10-10 下午12:15:35   * @parameter     * @return    */ publ ...

  10. C#开发可播放摄像头及任意格式视频的播放器

    前言 本文主要讲述,在WPF中,借助Vlc.DotNet调用VLC类库,实现视频播功能,下面我们先来做开发前的准备工作. 准备工作 首先,我们创建一个项目WpfVLC,然后,进入Neget搜索Vlc. ...