【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=6044

【题目大意】

  给出两个序列li,ri,现在要求构造排列p,使得对于区间[li,ri]来说,
  pi是区间中最小的值,且[li,ri]是满足pi是区间最小值的最大区间

【题解】

  我们发现对于区间[L,R],我们可以按照Dfs序找到支配这个区间的pi,
  对于找到的每个pi,我们将剩余的数字划分到左右区间继续进行dfs,
  如果在某个区间我们无法寻求到解,那么整个dfs的过程就被判定为无解,
  除去最小值作为根节点之后,左右子树的节点分配就是一个组合数了。

【代码】

#include <cstdio>
#include <algorithm>
#include <utility>
#include <map>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1000000007;
const int N=1000010;
namespace fastIO{
#define BUF_SIZE 100000
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 bool read(int &x){
char ch;
while(blank(ch=nc()));
if(IOerror)return 0;
for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
return 1;
}
#undef BUF_SIZE
};
namespace Comb{
int f[N],rf[N];
LL inv(LL a,LL m){return(a==1?1:inv(m%a,m)*(m-m/a)%m);}
LL C(int n,int m){
if(m<0||m>n)return 0;
return (LL)f[n]*rf[m]%mod*rf[n-m]%mod;
}
void init(){
f[0]=1;for(int i=1;i<=1000000;i++)f[i]=(LL)f[i-1]*i%mod;
rf[1000000]=inv(f[1000000],mod);
for(int i=1000000;i;i--)rf[i-1]=(LL)rf[i]*i%mod;
}
}
int l[N],r[N],n;
map<P,int> M;
LL dfs(int l,int r){
// printf("%d %d\n",l,r);
if(l>r)return 1;
int x=M[P(l,r)]; if(!x)return 0;
return Comb::C(r-l,r-x)*dfs(l,x-1)%mod*dfs(x+1,r)%mod;
}
int main(){
Comb::init();
for(int cas=1;fastIO::read(n);cas++){
M.clear();
for(int i=1;i<=n;i++)fastIO::read(l[i]);
for(int i=1;i<=n;i++)fastIO::read(r[i]);
for(int i=1;i<=n;i++)M[P(l[i],r[i])]=i;
LL ans=dfs(1,n);
printf("Case #%d: %lld\n",cas,ans);
}return 0;
}

HDU 6044 Limited Permutation(搜索+读入优化)的更多相关文章

  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 读入挂+组合数学

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

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

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

  4. fread读入优化,寻找速度极限

    序: 在之前的测试中,我们比较了四种读入方式,发现使用读入优化是最快的选择,但是我们知道fread()是比它更快的方法.这一次,我们对比四种读入优化,探寻C++读取速度的极限. 分别是getchar( ...

  5. OI黑科技:读入优化

    利用getchar()函数加速读入. Q:读入优化是什么? A :更加快速地读入一些较大的数字. Q:scanf不是已经够快了吗? A:Naive,scanf还是不!够!快! Q:那怎么办呢? A:我 ...

  6. ACM:读入优化

    两个简单的读入优化 int getin(){ ;; while(!isdigit(tmp=getchar()) && tmp!='-'); ,tmp=getchar(); )+(ans ...

  7. c++读入优化

    对于输入数据非常大的一些可(变)爱(态)题目,scanf就会大大拖慢程序的运行速度,cin就更不用说了,所以我们要用一种高大上的东西——读入优化. 读入优化的原理其实就是一个一个字符的读入,再组成数字 ...

  8. c++ 读入优化、输出优化模板

    0. 在有些输入数据很多的变态题中,scanf会大大拖慢程序的时间,cin就更慢了,所以就出现了读入优化.其原理就是一个一个字符的读入,输出优化同理,主要使用getchar,putchar函数. 1. ...

  9. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

随机推荐

  1. canvas利用formdata上传到服务器

    1.首先绘制canvas图片 <canvas id="myCanvas" width="100" height="100" style ...

  2. JQGrid 导出Excel 获取筛选条件

    需求描述:页面加载后,进行相关数据搜索,要求点击导出按钮后  下载Excel文件. 思路:希望在点击[导出Excel]按钮时,获取到表格搜索时的filters内容. 在百度.api.jqgrid.js ...

  3. Java 将html导出word格式

    @RequestMapping("download") public void exportWord( HttpServletRequest request, HttpServle ...

  4. 区块链~Merkle Tree(默克尔树)算法解析~转载

    转载~Merkle Tree(默克尔树)算法解析 /*最近在看Ethereum,其中一个重要的概念是Merkle Tree,以前从来没有听说过,所以查了些资料,学习了Merkle Tree的知识,因为 ...

  5. 土司论坛nc反弹神器使用方法

    说明: PS:我本机是linux,因为没有服务器所以使用win7来演示.倘若你是windows可以在本机生成dll以后再放到服务器上面去执行dll即可反弹shell物理机ip:192.168.1.12 ...

  6. Python阶段复习 - part 3 - Python函数

    利用函数打印9*9乘法表 def cheng(num): for i in range(1,num+1): for j in range(1,i+1): print('{0} * {1} = {2}' ...

  7. 1006. Team Rankings

    Description It's preseason and the local newspaper wants to publish a preseason ranking of the teams ...

  8. HDU 6112 今夕何夕 蔡勒公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6112题意:中文题目 分析:关键点在与如何计算一个日期是星期几,这个可以通过蔡勒公式来计算.基姆拉尔森计 ...

  9. JavaScript中数组对象详解

    Array对象即数组对象用于在单个变量中存储多个值,JS的数组是弱类型的,所以允许数组中含有不同类型的元素,数组元素甚至可以是对象或者其他数组. 创建数组的语法 1.Array构造器 1.var li ...

  10. An In-Depth Look at the HBase Architecture

    https://www.mapr.com/blog/in-depth-look-hbase-architecture An In-Depth Look at the HBase Architectur ...