51NOD 1934:受限制的排列——题解
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:受限制的排列——题解的更多相关文章
- 51nod 1934 受限制的排列——笛卡尔树
题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1934 根据给出的信息,可以递归地把笛卡尔树建出来.一个点只应该有 0/1/2 ...
- 【51nod】1934 受限制的排列
题解 这题还要判无解真是难受-- 我们发现我们肯定能确定1的位置,1左右的两个区间是同理的可以确定出最小值的位置 我们把区间最小值看成给一个区间+1,构建出笛卡尔树,就求出了每一次取最小值和最小值左右 ...
- HAOI2006 (洛谷P2341)受欢迎的牛 题解
HAOI2006 (洛谷P2341)受欢迎的牛 题解 题目描述 友情链接原题 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之 ...
- 51nod 1812 树的双直径 题解【树形DP】【贪心】
老了-稍微麻烦一点的树形DP都想不到了. 题目描述 给定一棵树,边权是整数 \(c_i\) ,找出两条不相交的链(没有公共点),使得链长的乘积最大(链长定义为这条链上所有边的权值之和,如果这条链只有 ...
- #P2341 [HAOI2006]受欢迎的牛 题解
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
- 51NOD 1709:复杂度分析——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1709 (我什么时候看到二进制贡献才能条件反射想到按位处理贡献呢……) 参 ...
- 51NOD 1559:车和矩形——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1559 波雷卡普有一个n×m,大小的棋盘,上面有k个车.他又放了q个矩形在 ...
- 51NOD 2026:Gcd and Lcm——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...
- 51NOD 1594:Gcd and Phi——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1594 参考及详细推导:http://www.cnblogs.com/ri ...
随机推荐
- OSG-基本几何图形
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- lesson 15 Fifty pence worth of trouble
lesson 15 Fifty pence worth of trouble appreciate =be grateful for We really appreciate all the help ...
- Linux 文件的常识
文件 文件的分类 文件 目录 链接 区分办法,ls -la 查看 十个标志符中的第一个 如:drwxrwxr-x. 2 normal normal 4096 8月 31 23:43 dir 目录是d ...
- JS里点击事件判断是否 触发了节点 和给标签添加class属性
$("#activityType").click(function(e){ if(e.target==$("#bb")[0]){ var bb=document ...
- Twaver的mono-desiner导出的json文件解析
以画的交换机为例,其他大概都差不多. 利用Twaver做出交换机模型如图1所示,其中,每一个端口都是一个单独的对象.具体Twaver操作流程参见网址:http://twaver.servasoft.c ...
- HDU 1042 N!(高精度乘)
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in ...
- js经典试题之原型与继承
js经典试题之原型与继承 1:以下代码中hasOwnProperty的作用是? var obj={} …….. obj.hasOwnProperty("val") 答案:判断obj ...
- 硬件PCB Layout布局布线Checklist检查表(通用版)
按部位分类 技术规范内容 1 PCB布线与布局 PCB布线与布局隔离准则:强弱电流隔离.大小电压隔离,高低频率隔离.输入输出隔离.数字模拟隔离.输入输出隔离,分界标准为相差一个数量级.隔离方法包括:空 ...
- linux上使用J-Link调试S3C2440裸机代码
linux上使用J-Link调试S3C2440裸机代码 工具: segger的jlink仿真器 segger的jlink for linux 交叉编译工具链里面的arm-xx-linux-xx-gdb ...
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...