您的位置:首页 > 其它

BestCoder 2nd Anniversary 1002/hdu5719 Arrange

2016-07-18 19:24 323 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5719

题目:

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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).

[align=left]Output[/align]
For each testcase, print the number of valid permutations modulo
998244353.

[align=left]Sample Input[/align]

2
3
2 1 1
2 2 3
5
5 4 3 2 1
1 2 3 4 5


[align=left]Sample Output[/align]

1
0


首先,根据题意可得B数组应是单调不升的,C数组是单调不降的。

可以发现A1=B1=C1 A_1 = B_1 = C_1
A​1​​=B​1​​=C​1​​,所以如果B1≠C1
B_1 \neq C_1 B​1​​≠C​1​​无解。

进一步,我们发现如果Bi<Bi−1 B_i < B_{i-1}
B​i​​<B​i−1​​,Ai=Bi
A_i = B_i A​i​​=B​i​​;如果Ci>Ci−1
C_i > C_{i-1} C​i​​>C​i−1​​,Ai=Ci
A_i = C_i A​i​​=C​i​​。但是如果Bi<Bi−1
B_i < B_{i-1} B​i​​<B​i−1​​和Ci>Ci−1
C_i > C_{i-1} C​i​​>C​i−1​​同时满足,就会产生冲突导致无解。

考虑Bi=Bi−1 B_i = B_{i-1}
B​i​​=B​i−1​​和Ci=Ci−1
C_i = C_{i-1} C​i​​=C​i−1​​同时满足的情况,此时应有Ai∈(Bi,Ci)
A_i \in (B_i,C_i) A​i​​∈(B​i​​,C​i​​)且Ai
A_i A​i​​没有在之前使用过。因为(Bi,Ci)
(B_i,C_i) (B​i​​,C​i​​)是不断变大的,我们只需维护一下这个区间内有多少值已经被使用过了,用乘法原理统计答案即可。注意到如果某时刻Ai
A_i A​i​​没有值可以使用,也会导致无解。

时间复杂度O(Tn) O(Tn) O(Tn)。

#include<cstdio>
#include<iostream>
#define N 110000
#define ll long long
#define MOD 998244353
using namespace std;

int b
,c
;

int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=0;i<n;i++)    scanf("%d",&b[i]);
for(int i=0;i<n;i++)    scanf("%d",&c[i]);
if(b[0]!=c[0])
{
cout<<0<<endl;
continue;
}
int cnt=0;
ll ans=1;
for(int i=1;i<n;i++)
{
if(b[i]>b[i-1]||c[i]<c[i-1]||b[i]<b[i-1]&&c[i]>c[i-1])
{
ans=0;
break;
}
if(b[i]==b[i-1]&&c[i]>c[i-1]||c[i]==c[i-1]&&b[i]<b[i-1])
cnt+=c[i]-c[i-1]+b[i-1]-b[i]-1;
else
{
if(cnt<=0)
{
ans=0;
break;
}
ans=ans*cnt%MOD;
cnt--;
}
}
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: