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 ...
随机推荐
- c# enum 解析
解析定义的枚举 public enum OrderPaymentStatus { /// <summary> /// 未支付 /// </summary> [Descripti ...
- Consul初体验
Preface Today I'm gonna implement a consul in my environment to discover service of MySQL da ...
- 用Anko和Kotlin实现Android上的对话框和警告提示(KAD 24)
作者:Antonio Leiva 时间:Mar 9, 2017 原文链接:https://antonioleiva.com/dialogs-android-anko-kotlin/ 借助Builder ...
- Python零基础入门必知
Python自学知识点总结 //2018.10.09 1. Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido ...
- Siki_Unity_2-10_数据结构与算法
Unity 2-10 数据结构与算法 任务1-1:数据结构简介 数据结构:数据存储的结构,数据之间的关系 数据结构分类: 集合:同属于一个集合 线性结构:数据元素存在一对一的关系 树形结构:数据元素存 ...
- [CodeForce721C]Journey
题目描述 Recently Irina arrived to one of the most famous cities of Berland - the Berlatov city. There a ...
- 【radio-group、radio】 单选项组件说明
radio-group组件是包裹radio组件的容器 原型: <radio-group bindchange="[EventHandle]"> <radio .. ...
- lintcode166 链表倒数第n个节点
链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...
- (原)HUD绘画贴图解析
@小道:临时存放 1\主过程 说明: a\调用DrawTextureSimple时,会将UTexure封装成CavarsItem, 若是正交投射函数执行双,最后CavarsItem.Dra ...
- MySql优化浅析
优化点:合理的使用索引,可以大幅度提升sql查询效率,特别查询的表的数据量大的时候,效果明显.一.引言 公司的产品XX出行上线正式运营,随着数据量的变大,司机2000+,日订单1万+,注册乘客26W+ ...