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 ...
随机推荐
- Hive实现自增列
1.用row_number()函数生成代理键 ) max_id from id_test) t2; 2.用UDFRowSequence生成代理键 ——报错? add jar ...
- php session存入redis
php的会话默认以文件的形式存在,可以配知道NOSQL中,既可以提高访问速度又能好好的实现回话共享,在后期做负载均衡时实现多台服务器session 同步也是比较方便: 一:在php配置文件中改 修改p ...
- Web应用服务器性能压力测试
压力测试需要关注三个方面:如何正确产生压力.如何定位瓶颈.如何预估系统的承载能力 产生压力的方法 通常可以写脚本产生压力机器人对服务器进行发包和收包操作,也可以使用现有的工具(像jmeter.Load ...
- APP性能测试工具-GT(随身调)
GT(随身调)是APP的随身调测平台,它是直接运行在手机上的“集成调测环境”(IDTE, Integrated Debug Environment).利用GT,仅凭一部手机,无需连接电脑,您即可对AP ...
- Linux命令应用大词典-第39章 网络安全
39.1 rtacct:网络统计工具 39.2 nmap:报告远程主机特征 39.3 tcpdump:实现网络数据采集分析 39.4 iptstate:显示IP表状态表条目 39.5 nstat:监控 ...
- word record 01
词义默认包括发音 coil /kɔɪl/ 发音(kuo you) collage /kə'lɑʒ/ 发音(ke la shi) colleague /'kɑliɡ/ 发音 (ka li ge) com ...
- python学习笔记03 --------------程序交互与格式化输出
1.读取用户输入内容 语法:input() 例: name = input('你的名字是?) print('你好'+name) 程序会等待用户输入名字后打印:你好(用户输入的名字) 注意:input接 ...
- 如何区别cookie和token?---测试cookie和token接口时先看。
cookie 是什么? cookie--------------在浏览器中的长相?火狐浏览器 ----------------------------------------------------- ...
- OpenPAI大规模人工智能平台安装部署文档
环境要求: 如果需要图形界面,需要在Ubuntu系统安装,否则centos系统安装时是没有问题的(web端和命令行进行任务提交) 安装过程需要有另外一台控制端机器(注意:区别于集群所在的任何一台服务器 ...
- LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...