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]
[align=left]Sample Output[/align]
首先,根据题意可得B数组应是单调不升的,C数组是单调不降的。
可以发现A1=B1=C1 A_1 = B_1 = C_1
A1=B1=C1,所以如果B1≠C1
B_1 \neq C_1 B1≠C1无解。
进一步,我们发现如果Bi<Bi−1 B_i < B_{i-1}
Bi<Bi−1,Ai=Bi
A_i = B_i Ai=Bi;如果Ci>Ci−1
C_i > C_{i-1} Ci>Ci−1,Ai=Ci
A_i = C_i Ai=Ci。但是如果Bi<Bi−1
B_i < B_{i-1} Bi<Bi−1和Ci>Ci−1
C_i > C_{i-1} Ci>Ci−1同时满足,就会产生冲突导致无解。
考虑Bi=Bi−1 B_i = B_{i-1}
Bi=Bi−1和Ci=Ci−1
C_i = C_{i-1} Ci=Ci−1同时满足的情况,此时应有Ai∈(Bi,Ci)
A_i \in (B_i,C_i) Ai∈(Bi,Ci)且Ai
A_i Ai没有在之前使用过。因为(Bi,Ci)
(B_i,C_i) (Bi,Ci)是不断变大的,我们只需维护一下这个区间内有多少值已经被使用过了,用乘法原理统计答案即可。注意到如果某时刻Ai
A_i Ai没有值可以使用,也会导致无解。
时间复杂度O(Tn) O(Tn) O(Tn)。
题目:
[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
A1=B1=C1,所以如果B1≠C1
B_1 \neq C_1 B1≠C1无解。
进一步,我们发现如果Bi<Bi−1 B_i < B_{i-1}
Bi<Bi−1,Ai=Bi
A_i = B_i Ai=Bi;如果Ci>Ci−1
C_i > C_{i-1} Ci>Ci−1,Ai=Ci
A_i = C_i Ai=Ci。但是如果Bi<Bi−1
B_i < B_{i-1} Bi<Bi−1和Ci>Ci−1
C_i > C_{i-1} Ci>Ci−1同时满足,就会产生冲突导致无解。
考虑Bi=Bi−1 B_i = B_{i-1}
Bi=Bi−1和Ci=Ci−1
C_i = C_{i-1} Ci=Ci−1同时满足的情况,此时应有Ai∈(Bi,Ci)
A_i \in (B_i,C_i) Ai∈(Bi,Ci)且Ai
A_i Ai没有在之前使用过。因为(Bi,Ci)
(B_i,C_i) (Bi,Ci)是不断变大的,我们只需维护一下这个区间内有多少值已经被使用过了,用乘法原理统计答案即可。注意到如果某时刻Ai
A_i Ai没有值可以使用,也会导致无解。
时间复杂度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; } }
相关文章推荐
- C++字符串和字符串结束标志
- vc 调用外部文件的方法
- ubuntu16.04下安装jdk和android studio
- MySQL---二进制日志
- 跨应用开启Service
- 动作代码
- 树莓派+串口墨水电子屏幕+温度湿度传感器打造专属时钟
- 如何在多个LinearLayout中添加分割线
- Parcelable接口的使用
- 更改mysql ini 拒绝访问
- Git pull时出错
- BestCoder 2nd Anniversary 1001/hdu5718 Oracle
- fscanf()函数详解
- webapp适配 rem实现
- HDU 5721 Palace
- ACM集训day6
- ajaxFileUpload 报这错jQuery.handleError is not a function
- hdu--1212 Big Number
- dataTransfer 对象
- 定时器的合并和变速