题目链接:

Arrange

Time Limit: 8000/4000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)

Problem Description
 
Accidentally, Cupid, god of desire has hurt himself with his own dart and fallen in love with Psyche.

This has drawn the fury of his mother, Venus. The goddess then throws before Psyche a great mass of mixed crops.

There are n heaps of crops in total, numbered from 1 to n.

Psyche needs to arrange them in a certain order, assume crops on the i-th position is Ai.

She is given some information about the final order of the crops:

1. the minimum value of A1,A2,...,Ai is Bi.

2. the maximum value of A1,A2,...,Ai is Ci.

She wants to know the number of valid permutations. As this number can be large, output it modulo 998244353.

Note that if there is no valid permutation, the answer is 0.

 
Input
The first line of input contains an integer T (1≤T≤15), which denotes the number of testcases.

For each test case, the first line of input contains single integer n (1≤n≤10^5).

The second line contains n integers, the i-th integer denotes Bi (1≤Bi≤n).

The third line contains n integers, the i-th integer denotes Ci (1≤Ci≤n).

 
Output
For each testcase, print the number of valid permutations modulo 998244353.
 
Sample Input
2
3
2 1 1
2 2 3
5
5 4 3 2 1
1 2 3 4 5
 
Sample Output
1
0
 
 
题意:

有多少个[1,n]的全排列,满足b[],c[];
b[i]为[1,i]的最小值,c[i]为[1,i]的最大值;
 
思路:
 
合法的b[],c[]应该是一个单调的函数;
每次可以得到一个区间[b[i]+1,c[i]-1],再减去这个区间里面的已经被用的数就是这一位能选的数目了;相乘取模就行;
 
 
AC代码:
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=998244353;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e3+10;
const double eps=1e-6; int b[N],c[N]; int main()
{
int t;
read(t);
while(t--)
{
int n;
read(n);
For(i,1,n)read(b[i]);
For(i,1,n)read(c[i]);
LL ans=1;
int cnt=0;
For(i,1,n)
{
if(i==1)
{
if(b[i]!=c[i]){ans=0;break;}
else cnt++;
continue;
}
if(c[i]<i||b[i]>n-i+1){ans=0;break;}
if(b[i]>b[i-1]||c[i]<c[i-1]||b[i]>c[i]){ans=0;break;}
if(b[i]<b[i-1]&&c[i]==c[i-1])cnt++;
else if(c[i]>c[i-1]&&b[i]==b[i-1])cnt++;
else if(c[i]==c[i-1]&&b[i]==b[i-1])
{
int len=c[i]-b[i]+1-cnt;
cnt++;
ans=ans*(LL)len%mod;
}
else {ans=0;break;}
}
cout<<ans<<"\n";
}
return 0;
}

  

hdu-5719 Arrange(组合数学)的更多相关文章

  1. hdu 5719 Arrange 贪心

    Arrange Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Proble ...

  2. hdu 5719(Arrange)(冷静分析)

    A数组显示从0到i的最小值B数组显示从0到i的最大值由此可得:A数组是单调不增的(怎么也会不使得最小值变大)B数组是单调不减的.设premin和premax为i位以前的最小值和最大值.可以得出以下几点 ...

  3. HDU 5719 Arrange

    根据条件,某些位置的数字就可以确定了.确定过程中如果有冲突,则无解. 如果B中出现了递增,C中出现了递减,则无解. 对于每一个未确定的a[i],ans需要更新,ans=ans*((c[i]-b[i]+ ...

  4. hdu 5719 BestCoder 2nd Anniversary B Arrange 简单计数问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5719 题意:一个数列为1~N的排列,给定mn[1...n]和mx[1...n],问有符合的排列数为多少 ...

  5. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  6. BestCoder 2nd Anniversary/HDU 5719 姿势

    Arrange Accepts: 221 Submissions: 1401 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/2 ...

  7. poj 1286 Necklace of Beads poj 2409 Let it Bead HDU 3923 Invoker <组合数学>

    链接:http://poj.org/problem?id=1286 http://poj.org/problem?id=2409 #include <cstdio> #include &l ...

  8. HDU 4609 3-idiots (组合数学 + FFT)

    题意:给定 n 条边,问随机选出 3 条边,能组成三角形的概率是多少. 析:答案很明显就是  能组成三角形的种数 / (C(n, 3)).现在的问题是怎么求能组成三角形的种数. 这个博客说的非常清楚了 ...

  9. A - Dogs and Cages HDU - 6243(组合数学)

    题意:在1—n的数字,放入编号为1—n的框中,每个框只放一个数字,问数字与所放的框的编号不同的个数的期望值. 思路:在1—n中任选一个数字,设为k 那么 k 排到非k编号的框中的方案数为 n!-(n- ...

随机推荐

  1. msp430项目编程50

    msp430综合项目---gsm无线采集传输平台系统50 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  2. 马蜂窝ABTest多层分流系统的设计与实现

      什么是 ABTest 产品的改变不是由我们随便「拍脑袋」得出,而是需要由实际的数据驱动,让用户的反馈来指导我们如何更好地改善服务.正如马蜂窝 CEO 陈罡在接受专访时所说:「有些东西是需要 Sen ...

  3. 详解DNS,你真的懂吗?

    what`s  this ? 概念 域名系统(英文:DomainNameSystem,缩写:DNS)是互联网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.D ...

  4. configure: error: Cannot find php_pdo_driver.h.

    安装pdo_mysql cd /usr/local/src/php-5.4.0/ext/pdo_mysql/ /usr/local/php/bin/phpize   # /usr/local/php为 ...

  5. 三角网格上的寻路算法Part.1—Dijkstra算法 等

    http://www.cnblogs.com/chnhideyoshi/p/AStar.html

  6. 移动端日历选择控件(支持Zepto和JQuery)

    移动端日历选择控件(支持Zepto和JQuery) <!DOCTYPE html> <html> <head> <meta charset="utf ...

  7. 【内核研究】处理者_Handler

    虽然MessageQueue提供了直接读/写的函数接口.但对于程序猿来说,一般不直接读/写消息队列.之前了解到,在Looper.loop()函数中.当取出消息后,会回调msg.target对象的han ...

  8. BubbleGum96 开箱杂谈与软件资源

    前言 原创文章,转载引用务必注明链接. 拿到有一段时间了,一直在想写哪些内容.96boards发布以来,吸引了很多眼球.这里我就慢慢随便聊聊,希望能让大家对96boards有更多了解. 开箱 [开箱图 ...

  9. webpack4.0入门以及使用

    1. 安装webpack 先新建一个文件夹(demos),然后 npm init -y 新建一个package.json然后在当前目录执行webpack命令 webpack 模块未发现或者未找到src ...

  10. memchached你知道和不知道的事