http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1934

听说会笛卡尔树的人这题都秒了啊……

参考:https://blog.csdn.net/vectorxj/article/details/79475244

首先题得看懂(我就是看题解才看懂题面的……),它告诉你对于i,我们有最大的(li,ri)使得这个区间内pi最小。

于是最小的数一定是(1,n)区间内的,设为pos,那么我们只需要递归处理(1,pos-1)和(pos+1,n)的即可。

当然我们的情况数要乘以给左区间的数的情况数。

中途如果出现各种无解情况直接返回0即可。

注意读入优化!

#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct fastio{
static const int bs=;
char c(){
static char buf[bs],*S=buf,*T=buf;
if(S==T){
T=(S=buf)+fread(buf,,bs,stdin);
if(S==T)return EOF;
}
return *S++;
}
int operator()(){
int X=;char ch=c();
if(ch==EOF)return ;
while(!isdigit(ch))ch=c();
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=c();
return X;
}
}read;
const int N=1e6+;
const int p=1e9+;
inline int qpow(int k,int n){
int res=;
while(n){
if(n&)res=(ll)res*k%p;
k=(ll)k*k%p;n>>=;
}
return res;
}
map<int,int>mp[N];
int n,cnt,l[N],r[N];
int jc[N],inv[N];
void init(int k){
jc[]=;
for(int i=;i<=k;i++)jc[i]=(ll)jc[i-]*i%p;
inv[k]=qpow(jc[k],p-);
for(int i=k-;i;i--)inv[i]=(ll)inv[i+]*(i+)%p;
inv[]=;
}
inline int C(int a,int b){
return (ll)jc[a]*inv[b]%p*inv[a-b]%p;
}
int work(int L,int R){
if(L>R)return ;
int pos=mp[L][R];
if(L==pos&&pos==R)return ;
if(pos<L||R<pos)return ;
return (ll)C(R-L,pos-L)*work(L,pos-)%p*work(pos+,R)%p;
}
int main(){
init(1e6);
while(n=read()){
for(int i=;i<=n;i++)mp[i].clear();
for(int i=;i<=n;i++)l[i]=read();
for(int i=;i<=n;i++)r[i]=read();
bool flag=;
for(int i=;i<=n;i++){
if(mp[l[i]].count(r[i]))flag=;
mp[l[i]][r[i]]=i;
}
if(!flag)printf("Case #%d: 0\n",++cnt);
else printf("Case #%d: %d\n",++cnt,work(,n));
}
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

51NOD 1934:受限制的排列——题解的更多相关文章

  1. 51nod 1934 受限制的排列——笛卡尔树

    题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1934 根据给出的信息,可以递归地把笛卡尔树建出来.一个点只应该有 0/1/2 ...

  2. 【51nod】1934 受限制的排列

    题解 这题还要判无解真是难受-- 我们发现我们肯定能确定1的位置,1左右的两个区间是同理的可以确定出最小值的位置 我们把区间最小值看成给一个区间+1,构建出笛卡尔树,就求出了每一次取最小值和最小值左右 ...

  3. HAOI2006 (洛谷P2341)受欢迎的牛 题解

    HAOI2006 (洛谷P2341)受欢迎的牛 题解 题目描述 友情链接原题 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之 ...

  4. 51nod 1812 树的双直径 题解【树形DP】【贪心】

    老了-稍微麻烦一点的树形DP都想不到了. 题目描述 给定一棵树,边权是整数 \(c_i\) ,找出两条不相交的链(没有公共点),使得链长的乘积最大(链长定义为这条链上所有边的权值之和,如果这条链只有 ...

  5. #P2341 [HAOI2006]受欢迎的牛 题解

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  6. 51NOD 1709:复杂度分析——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1709 (我什么时候看到二进制贡献才能条件反射想到按位处理贡献呢……) 参 ...

  7. 51NOD 1559:车和矩形——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1559 波雷卡普有一个n×m,大小的棋盘,上面有k个车.他又放了q个矩形在 ...

  8. 51NOD 2026:Gcd and Lcm——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...

  9. 51NOD 1594:Gcd and Phi——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1594 参考及详细推导:http://www.cnblogs.com/ri ...

随机推荐

  1. hive 中的float和double

    表employees中字段 taxes(税率)用类型float存储 hive> select name, salary, taxes from employees where taxes  &g ...

  2. Weka java.lang.reflect.InvocationTargetException

    在用Weka导入数据的时候报 java.lang.reflect.InvocationTargetException 错误,Weka运行包没有给出详细的错误信息,无法查到. 直接调试Weka源码,发现 ...

  3. FastJson - 从HttpEntity到Json

    在使用java + httpClient施行API自动化时,不可避免地遇到了如下问题: 1. 用Http Response数据做断言: 2. 用上一个请求的Response内容,作为下一个请求的参数: ...

  4. 第三模块:面向对象&网络编程基础 第2章 网络编程

    01-计算机基础 02-什么是网络 03-五层协议详解 04-传输层详解 05-什么是Socket 06-基于socket实现简单套接字通信 07-在简单套接字基础上加上通信循环 08-客户端与服务端 ...

  5. Pyhton网络爬虫实例_豆瓣电影排行榜_Xpath方法爬取

    -----------------------------------------------------------学无止境------------------------------------- ...

  6. 小程序开发中,纯css实现内容收起折叠功能

    不多说,直接上代码: wxml页面: <!--收起折叠 begin--> <view style='width:100%;background:#fff;border-top:1px ...

  7. 最全的Markdown语法

    目录 Markdown语法 多级标题 引用与注释 插入代码 行内代码 代码段 图片 超链接 行内超链接 参数式超链接 字体 表格 分割线 多级列表 无序列表 有序列表 多选框 LaTeX公式 行内La ...

  8. Period :KMP

    I - Period Problem Description For each prefix of a given string S with N characters (each character ...

  9. 常用web资源

    ip相关 新浪:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=220.181.38.110 (不带参数本机) ...

  10. Crawling is going on - Beta版本测试报告

    [Crawling is going on - Beta版本] 测试报告 文件状态: [] 草稿 [√] 正式发布 [] 正在修改 报告编号: 当前版本: 2.0.2 编写人: 周萱.刘昊岩.居玉皓 ...