time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The cinema theater hall in Sereja's city is n seats lined up in front of one large screen. There are slots for personal possessions to the left and to the right of each seat. Any two adjacent seats have exactly one shared slot. The figure below shows the arrangement of seats and slots for n = 4.

Today it's the premiere of a movie called "Dry Hard". The tickets for all the seats have been sold. There is a very strict controller at the entrance to the theater, so all n people will come into the hall one by one. As soon as a person enters a cinema hall, he immediately (momentarily) takes his seat and occupies all empty slots to the left and to the right from him. If there are no empty slots, the man gets really upset and leaves.

People are not very constant, so it's hard to predict the order in which the viewers will enter the hall. For some seats, Sereja knows the number of the viewer (his number in the entering queue of the viewers) that will come and take this seat. For others, it can be any order.

Being a programmer and a mathematician, Sereja wonders: how many ways are there for the people to enter the hall, such that nobody gets upset? As the number can be quite large, print it modulo 1000000007 (109 + 7).

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers, the i-th integer shows either the index of the person (index in the entering queue) with the ticket for the i-th seat or a 0, if his index is not known. It is guaranteed that all positive numbers in the second line are distinct.

You can assume that the index of the person who enters the cinema hall is a unique integer from 1 to n. The person who has index 1 comes first to the hall, the person who has index 2 comes second and so on.

Output

In a single line print the remainder after dividing the answer by number 1000000007 (109 + 7).

Examples
Input
11
0 0 0 0 0 0 0 0 0 0 0
Output
1024
Input
6
0 3 1 0 0 0
Output
3

给出一个数n还有n个数ci
如果ci>0,表示排列上的第ci个位置上的数为i
如果ci=0,表示排列上的第ci个位置上的数不确定 即是说,有一个n的排列,其中部分position上的数固定,求有多少种排列满足:
对于排列上的任意一个数x,在x之前不同时存在x+1和x-1这2个数的方案数 solution:
这道题主要在于分情况讨论,得到答案
只要发现一个性质,就可以解决问题了
要保证对于任意一个数x,x+1和x-1不同时存在,则x之前的数放在一起刚好是一个[1,n]的子
区间[L,R],这个自区间的长度为x-1,且有is[x] = L - 1 || is[x] = R + 1
这样我们只需要分情况讨论,
部分情况直接推出公式得到答案
部分情况需要用到递推,同时维护当前的L,R
具体看代码 is[i]表示排列的第i个数固定为is[i]
pre[i]表示第i个数前一个被固定的数,没有则为-1
next[i]表示第i个数后一个被固定的数,没有则为-1
f[i]表示当前考虑到排列的第i个数,当前可行的方案数 ps:
这道题初始化f的时候要注意细节,分情况初始化
  //File Name: cf380D.cpp
//Author: long
//Mail: 736726758@qq.com
//Created Time: 2016年05月20日 星期五 00时32分29秒 #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <map> #define LL long long
#define next NEXT using namespace std; const int MAXN = + ;
const int MOD = (int)1e9 + ; int is[MAXN], pre[MAXN], next[MAXN];
LL f[MAXN],jie[MAXN]; void init(int n){
jie[] = ;
for(int i=;i<=n;i++)
jie[i] = jie[i-] * i % MOD;
int now = -;
for(int i=;i<=n;i++){
pre[i] = now;
if(is[i]) now = i;
}
now = -;
for(int i=n;i>;i--){
next[i] = now;
if(is[i]) now = i;
}
} LL qp(LL x,LL y){
LL res = ;
while(y){
if(y & ) res = res * x % MOD;
x = x * x % MOD;
y >>= ;
}
return res;
} LL get_c(LL x,LL y){
if(y < || x < y) return ;
if(y == || y == x) return ;
return jie[x] * qp(jie[y] * jie[x - y] % MOD,MOD - ) % MOD;
} LL solve(int n,bool flag){
if(!flag) return qp(,n - );
init(n);
//for(int i=1;i<=n;i++){
// printf("i = %d is = %d\n",i,is[i]);
//}
int L = n + , R = ;
for(int i=;i<=n;i++){
if(!is[i]) continue;
if(L <= is[i] && R >= is[i])
return ;
if(pre[i] == - && next[i] == -){
LL ans = ,now;
for(int x=;x < is[i];x++){
now = get_c(i-,x) * get_c(n-i,is[i]-x-) % MOD;
if(x > ) ans = (ans + now * % MOD) % MOD;
else ans = (ans + now) % MOD;
}
return ans;
}
else if(pre[i] > ){
f[i] = f[pre[i]];
//printf("i = %d pre = %d next = %d f = %d\n",i,pre[i],next[i],f[i]);
if(is[i] > is[pre[i]]) R++;
else L--;
//printf("L = %d R = %d\n",L,R);
if(next[i] == -){
return f[i] * get_c(n-i,L-) % MOD;
}
else{
if(is[next[i]] > R){
//printf("i = %d f = %d\n",i,f[i]);
f[i] = f[i] * get_c(next[i]-i-,is[next[i]]-R-) % MOD;
R = is[next[i]] - ;
L = R + - next[i];
//printf("i = %d f = %d\n",i,f[i]);
}
else{
//printf("i = %d f = %d\n",i,f[i]);
f[i] = f[i] * get_c(next[i]-i-,L-is[next[i]]-) % MOD;
L = is[next[i]] + ;
R = L + next[i] - ;
//printf("i = %d f = %d\n",i,f[i]);
}
}
//printf("L = %d R = %d\n",L,R);
}
else{
if(is[i] < is[next[i]]){
R = is[next[i]] - ;
L = R + - next[i];
}
else{
L = is[next[i]] + ;
R = L + next[i] - ;
}
int cnt = abs(is[next[i]] - is[i]);
f[i] = ;
if(i == ){
f[i] = get_c(next[i]-,R-is[i]);
}
else{
//printf("is = %d L = %d R = %d\n",is[i],L,R);
if(i- <= is[i]-L)
f[i] = qp(,i-) * get_c(next[i]-i-,is[i]-L-i+) % MOD;
//printf("f = %d\n",f[i]);
if(i- <= R-is[i])
(f[i] += qp(,i-) * get_c(next[i]-i-,R-is[i]-i+)% MOD) %= MOD;
//printf("f = %d\n",f[i]);
}
//printf("i = %d pre = %d next = %d f = %d\n",i,pre[i],next[i],f[i]);
//printf("L = %d R = %d\n",L,R);
}
}
return -;
} int main(){
int n,u;
bool flag = false;
scanf("%d",&n);
memset(is,,sizeof is);
for(int i=;i<=n;i++){
scanf("%d",&u);
if(u){
is[u] = i;
flag = true;
}
}
printf("%d\n",(int)solve(n,flag));
return ;
}
												

cf380D Sereja and Cinema 组合数学的更多相关文章

  1. Codeforces 380D Sereja and Cinema (看题解)

    Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...

  2. Codeforces 380 简要题解

    ABC见上一篇. 感觉这场比赛很有数学气息. D: 显然必须要贴着之前的人坐下. 首先考虑没有限制的方案数.就是2n - 1(我们把1固定,其他的都只有两种方案,放完后长度为n) 我们发现对于一个限制 ...

  3. CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)

    Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. Howev ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  8. 组合数学or not ---- n选k有重

    模板问题: 1. 取物品 (comb.pas/c/cpp) [问题描述] 现在有n个物品(有可能相同),请您编程计算从中取k个有多少种不同的取法.[输入] 输入文件有两行,第一行包含两个整数n,k(2 ...

  9. 组合数学(全排列)+DFS CSU 1563 Lexicography

    题目传送门 /* 题意:求第K个全排列 组合数学:首先,使用next_permutation 函数会超时,思路应该转变, 摘抄网上的解法如下: 假设第一位是a,不论a是什么数,axxxxxxxx一共有 ...

随机推荐

  1. HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. Mysql存储日期类型用int、timestamp还是datetime?

    通常存储时间用datetime类型,现在很多系统也用int存储时间,它们有什么区别?个人更喜欢使用int这样对于日期计算时比较好哦,下面我们一起来看到底那种会好些. int ().4个字节存储,INT ...

  3. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  4. Web前端开发笔试&面试_05_other 2016104399MS

    1.数据传送的方式,get post 的区别是? 2.你要怎么绑定页码(比如给你第三页,)? 3.数据流是如何实现,用for 循环? 4.轮播怎么实现?用原生JS实现. 5.布局,B是固定宽度,A的内 ...

  5. Void 0

    void anything 都返回 undefined , 使用Void 0 ,仅仅是因为习惯而已,所以不必介怀. 比较好的写法应该是 void(0)

  6. CSS控制print打印样式

    来源:http://blog.csdn.net/pangni/article/details/6224533 一.添加打印样式 1. 为屏幕显示和打印分别准备一个css文件,如下所示:   用于屏幕显 ...

  7. 透明度兼容性(ie8以上)

    转载:http://www.cnblogs.com/PeunZhang/p/4089894.html demo代码:文件中,背景透明,文字不透明的研究和使用.zip

  8. 功能更强大的格式化工具类 FormatUtils.java

    package com.util; import java.text.DecimalFormat; import java.text.ParseException; import java.text. ...

  9. SqlServer数据库的查询优化

    建立一个web 应用,分页浏览功能必不可少.这个问题是数据库处理中十分常见的问题.经典的数据分页方法是:ADO 纪录集分页法,也就是利用ADO自带的分页功能(利用游标)来实现分页.但这种分页方法仅适用 ...

  10. AS3 编码解码函数 特殊字符转义

    有时候传输特殊字符的时候,需要将字符转义, trace(escape("!@#$%^&*()_+<>?'"));//输出:%21@%23%24%25%5E%26 ...