超级读入挂HDU(6044)
来源:互联网 发布:js 函数对象的构造函数 编辑:程序博客网 时间:2024/04/30 05:45
#include <cstdio>#include <cstring>#include <algorithm>typedef long long LL;namespace fastIO { #define BUF_SIZE 100000 //fread -> read bool IOerror = 0; inline char nc() { static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE; if(p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, BUF_SIZE, stdin); if(pend == p1) { IOerror = 1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } inline void read(int &x) { char ch; while(blank(ch = nc())); if(IOerror) return; for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0'); } #undef BUF_SIZE};using namespace fastIO;const int maxn = 1000001, mod = 1000000007;int fact[maxn], iact[maxn];int n, l[maxn], r[maxn], ord[maxn], pL[maxn], pR[maxn], sz[maxn], tmp[maxn];int bfs(int rt){ int L = 0, R = 0; static int que[maxn], f[maxn]; que[R++] = rt; while(L < R) { int u = que[L++], vL = pL[u], vR = pR[u]; // [L, R] at x -> [L, x-1] [x+1, R] if(l[u] == u) { if(vL != -1) return 0; } else { if(vL == -1 || vL >= u || r[vL] != u - 1) return 0; que[R++] = vL; } if(u == r[u]) { if(vR != -1) return 0; } else { if(vR == -1 || vR <= u || l[vR] != u + 1) return 0; que[R++] = vR; } } for(int i = n - 1; i >= 0; --i) { int u = que[i], ret = 1, szL, szR; if(l[u] == u) szL = 0; else { ret = (LL)ret * f[pL[u]] % mod; szL = sz[pL[u]]; } if(u == r[u]) szR = 0; else { ret = (LL)ret * f[pR[u]] % mod; szR = sz[pR[u]]; } if(szL && szR) ret = (LL)ret * fact[szL + szR] % mod * iact[szL] % mod * iact[szR] % mod; sz[u] = szL + szR + 1; f[u] = ret; } return f[rt];}int main(){ iact[1] = 1; for(int i = 2; i < maxn; ++i) iact[i] = mod - (int)(mod / i * (LL)iact[mod % i] % mod); fact[0] = iact[0] = 1; for(int i = 1; i < maxn; ++i) { fact[i] = (LL)fact[i - 1] * i % mod; iact[i] = (LL)iact[i - 1] * iact[i] % mod; } for(int Case = 1; read(n), !fastIO::IOerror; ++Case) { for(int i = 1; i <= n; ++i) read(l[i]); for(int i = 1; i <= n; ++i) read(r[i]); ord[0] = ord[n + 1] = 0; int *cnt = sz; memset(cnt + 1, 0, n * sizeof(int)); for(int i = 1; i <= n; ++i) ++cnt[r[i]]; for(int i = 2; i <= n; ++i) cnt[i] += cnt[i - 1]; for(int i = n; i >= 1; --i) tmp[cnt[r[i]]--] = i; memset(cnt + 1, 0, n * sizeof(int)); for(int i = 1; i <= n; ++i) ++cnt[l[i]]; for(int i = 2; i <= n; ++i) cnt[i] += cnt[i - 1]; for(int i = n; i >= 1; --i) ord[cnt[l[tmp[i]]]--] = tmp[i]; for(int i = 1; i <= n; ++i) pL[ord[i]] = l[ord[i - 1]] == l[ord[i]] ? ord[i - 1] : -1; memset(cnt + 1, 0, n * sizeof(int)); for(int i = 1; i <= n; ++i) ++cnt[l[i]]; for(int i = 2; i <= n; ++i) cnt[i] += cnt[i - 1]; for(int i = n; i >= 1; --i) tmp[cnt[l[i]]--] = i; memset(cnt + 1, 0, n * sizeof(int)); for(int i = 1; i <= n; ++i) ++cnt[r[i]]; for(int i = 2; i <= n; ++i) cnt[i] += cnt[i - 1]; for(int i = n; i >= 1; --i) ord[cnt[r[tmp[i]]]--] = tmp[i]; for(int i = 1; i <= n; ++i) pR[ord[i]] = r[ord[i]] == r[ord[i + 1]] ? ord[i + 1] : -1; int pos = -1; for(int i = 1; i <= n; ++i) if(l[i] == 1 && r[i] == n) { pos = i; break; } printf("Case #%d: %d\n", Case, pos == -1 ? 0 : bfs(pos)); } return 0;}
阅读全文
1 0
- 超级读入挂HDU(6044)
- 2017多校训练赛第一场 HDU 6044 Limited Permutation(虚建笛卡尔树+超级读入挂)
- 超级读入挂
- 超级读入挂
- HDU 6044 Limited Permutation dfs + 组合数(读入挂)
- 2017 Multi-University Training Contest 10 1008 Monkeys&& HDU 6178 (贪心+超级读入挂fread)
- hdu 6178 dfs+超神读入挂
- hdu-超级密码(BFS)
- 超级快的读入优化实现
- 读入挂
- 读入挂
- 读入挂
- 读入挂
- HDU 5047 Sawtooth (规律+java各种读入输出模板)
- HDU 1226 超级密码 (搜素)
- HDU-#2041 超级楼梯(Fibonacci数列)
- hdu 2041 超级楼梯(水题)
- hdu 2041 超级楼梯(java)
- quickFix Java Demo
- JavaMail原理解析
- Java线程监听,意外退出线程后自动重启
- FineReport(二)帆软报表图表的
- stratum mining protocol demo
- 超级读入挂HDU(6044)
- nexus更新Central仓库索引
- poj 3616 Milking Time (DP)
- 10. ESP8266通过OTA更新固件的实践
- 具体名词的理解、单词的深意
- ssm单文件下载ftp服务器到浏览器
- jdk1.8 新特性之 forEach 循环遍历
- JQuery
- Ceres-Solver学习笔记(2)