Limited Permutation

Problem Description
As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplicated for each 1≤i≤n to calculate (li,ri) meeting the condition that min(pL,pL+1,⋯,pR)=pi if and only if li≤L≤i≤R≤ri for each 1≤L≤R≤n.

Given the positive integers n, (li,ri) (1≤i≤n), you are asked to calculate the number of possible permutations p1,p2,⋯,pn from 1 to n, meeting the above condition.

The answer may be very large, so you only need to give the value of answer modulo 109+7.

 
Input
The input contains multiple test cases.

For each test case:

The first line contains one positive integer n, satisfying 1≤n≤106.

The second line contains n positive integers l1,l2,⋯,ln, satisfying 1≤li≤i for each 1≤i≤n.

The third line contains n positive integers r1,r2,⋯,rn, satisfying i≤ri≤n for each 1≤i≤n.

It's guaranteed that the sum of n in all test cases is not larger than 3⋅106.

Warm Tips for C/C++: input data is so large (about 38 MiB) that we recommend to use fread() for buffering friendly.

size_t fread(void *buffer, size_t size, size_t count, FILE *stream); // reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by buffer; the total number of elements successfully read is returned.
 
Output
For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 
Sample Input
3 1 1 3 1 3 3 5 1 2 2 4 5 5 2 5 5 5
 
Sample Output
Case #1: 2
Case #2: 3
 

题解:

  看懂题意

  每个pi掌管 L ,R,题意是指超过这段范围就有比pi还要小的值

  所有必然有一个pi 值掌管 1,n的,推出  必有  pj,pk分别 掌管 (1,i - 1), (i+1,n)

  dfs下去计算方案

  还有就是必须用读入挂才能过

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e6+, M = 1e3+,inf = 2e9,mod = 1e9 + ;
namespace IO {
const int MX = 4e7; //1e7占用内存11000kb
char buf[MX]; int c, sz;
void begin() {
c = ;
sz = fread(buf, , MX, stdin);
}
inline bool read(int &t) {
while(c < sz && buf[c] != '-' && (buf[c] < '' || buf[c] > '')) c++;
if(c >= sz) return false;
bool flag = ; if(buf[c] == '-') flag = , c++;
for(t = ; c < sz && '' <= buf[c] && buf[c] <= ''; c++) t = t * + buf[c] - '';
if(flag) t = -t;
return true;
}
} const int MOD = (int)1e9 + ;
int F[N], Finv[N], inv[N];//F是阶乘,Finv是逆元的阶乘
void init(){
inv[] = ;
for(int i = ; i < N; i ++){
inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
}
F[] = Finv[] = ;
for(int i = ; i < N; i ++){
F[i] = F[i-] * 1ll * i % MOD;
Finv[i] = Finv[i-] * 1ll * inv[i] % MOD;
}
}
inline LL C(int n, int m){//comb(n, m)就是C(n, m)
if(m < || m > n) return ;
return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
} struct ss{
int l,r,id;
bool operator<(const ss& x) const{
if(l == x.l) return r > x.r;
else return l < x.l;
}
}a[N];
int now,ok;
inline LL dfs(int ll,int rr) {
if(!ok) return ;
if(ll > rr) return 1LL;
if(a[now].l != ll || a[now].r != rr) {
ok = ;
return ;
}
int ids = a[now++].id;
return dfs(ll,ids-) * dfs(ids+,rr) % mod * C(rr-ll,ids-ll) % mod;
}
int main() {
init();
int cas = ,n;
IO::begin();
while(IO::read(n)) {
for(int i = ; i <= n; ++i) IO::read(a[i].l);
for(int i = ; i <= n; ++i) IO::read(a[i].r),a[i].id = i;
now = , ok = ;
sort(a+,a+n+);
printf("Case #%d: %lld\n",cas++,dfs(,n));
}
return ;
}

最新文章

  1. PHP与MYSQL事务处理
  2. Win10 UI入门 RenderTransform属性分析之Translate 平移变形
  3. iOS开发工程师面试题(一)
  4. 调整 ANTD 组件菜单的字体大小。
  5. 全站 HTTPS 来了
  6. android学习之EdieText组件的使用
  7. oreData的学习记录
  8. JS论坛地址备忘
  9. 从零开始学java(小游戏 石头剪刀布)
  10. iOS -不同模拟器字体适配
  11. Oracle execute and call
  12. 含服务端,客户端,数据库的注册/登录/聊天/在线/离线查看的聊天demo
  13. es6的几种写法
  14. 第九节 JS运动应用
  15. codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)
  16. Mysql 5.7 报错 3534 错误
  17. Solr相似度算法二:BM25Similarity
  18. noip 模拟赛 After 17(递推+特殊的技巧)
  19. 如何编译部署 UIKit 离线文档?
  20. skynet中动态库的处理

热门文章

  1. .NET重构(一):抽象工厂模式实现登录
  2. spring配置mybatis3
  3. Oracle 数据库有五个必需的后台进程,DBWR,LGWR,CKPT,SMON,PMON
  4. mysql查询死锁,执行语句,服务器状态等语句集合
  5. maven配置中国下载源【转:http://www.cnblogs.com/libingbin/p/5949483.html】
  6. 【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题
  7. C/C++怎样传递二维数组,转载自CSDN
  8. Netty和Akka有什么不同?
  9. 快速掌握RabbitMQ(三)——消息确认、持久化、优先级的C#实现
  10. BZOJ——1607: [Usaco2008 Dec]Patting Heads 轻拍牛头