hdu 5719 Arrange(排列组合)
2016-07-17 22:32
260 查看
Arrange
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 21 Accepted Submission(s): 9
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≤105).
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
Hint
In the first example, there is only one valid permutation (2,1,3) .
In the second example, it is obvious that there is no valid permutation.
题意:Ai是1~n的某种排列,Bi表示前i个数的最小值,Ci表示前i个数的最大值
先给出B和C,求A序列有多少种可能
思路:如果Bi>B(i-1),Ci<C(i-1),Bi>Ci,B1!=C1那么都是不合法的
然后我们枚举i点
1:Bi==Bi-1&&Ci==Ci-1 说明Ai是A1~A(i-1)中出现过的一个数,一共有max-min+1种可能性,不过因为最后的序列必须包含1~n,所以这里设置一个计数器,表示从min-max中有多少个数是已经存在的了,这里必须填一个不存在的数。结果用乘法原理乘起来就OK
2:Bi<Bi-1&&Ci==Ci-1 说明此时Ai==Bi
3::Ci>Ci-1&&Bi==Bi-1 说明此时Ai==Ci
4:Bi<Bi-1&&Ci>Ci-1 Ai又要小于当前最小值又要大于当前最大值,显然矛盾,直接输出0
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 200050
#define mod 998244353
int b
,c
;
int ans
;
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&b[i]);
for(int i=1; i<=n; i++)
scanf("%d",&c[i]);
int flag=0;
for(int i=2; i<=n; i++)
if(b[i]>c[i]||b[i]>b[i-1]||c[i]<c[i-1]||b[i]<1||b[i]>n||c[i]<1||c[i]>n)
{
flag=1;
break;
}
if(b[1]!=c[1]||flag||b[1]<1||b[1]>n)
{
printf("0\n");
continue;
}
__int64 ans=1;
__int64 minn=b[1],maxn=b[1];
int num=1;
for(int i=2; i<=n; i++)
{
if(b[i]==b[i-1]&&c[i]==c[i-1])
{
ans=ans*(maxn-minn-num+1)%mod;
num++;
}
else if(b[i]<b[i-1]&&c[i]==c[i-1])
{
minn=b[i];
num++;
}
else if(b[i]==b[i-1]&&c[i]>c[i-1])
{
maxn=c[i];
num++;
}
else
{
flag=1;
break;
}
}
if(flag) printf("0\n");
else printf("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- ListView系列(七)——Adapter内的onItemClick监听器四个arg参数
- linux 用户空间与内核空间——高端内存详解
- .NET设计规范————类型设计规范
- 证书知识简单介绍
- 遍历map的方法
- Path Sum
- 安卓中的handler消息传递机制
- 支付网关接入中的ssl连接和签名调试tips
- 几种网站攻击和应对措施
- CentOS 7.0关闭默认防火墙启用iptables防火墙
- Python将'\u'开头的字符串转为unicode编码
- PAT乙级.1001. 害死人不偿命的(3n+1)猜想
- POJ 2607 Fire Station 消防站
- 辩证:权限与EFS加密(win2003 srv)
- hangfire
- hudson安装
- 循环嵌套
- This in JavaScript
- EasyDarwin Rtsp流转发(二)
- HTML页面跳转的5种方法