Arrange
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 547 Accepted Submission(s): 192
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 .
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) .
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 0HintIn 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.
Source
Recommend
wange2014
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 998244353, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n;
int a[N], mn[N], mx[N];
int solve()
{
if (mn[1] != mx[1])return 0;
LL ans = 1;
int num = 0;
for (int i = 2; i <= n; ++i)
{
if (mx[i] < mx[i - 1] || mn[i] > mn[i - 1])return 0;
if (mx[i] > mx[i - 1] && mn[i] < mn[i - 1])return 0;
if (mx[i] > mx[i - 1] || mn[i] < mn[i - 1]);
else ans = ans * num % Z;
num += (mx[i] - mx[i - 1]) + (mn[i - 1] - mn[i]) - 1;
}
return ans;
}
int main()
{
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)scanf("%d", &mn[i]);
for (int i = 1; i <= n; ++i)scanf("%d", &mx[i]);
printf("%d\n", solve());
}
return 0;
}
/*
【trick&&吐槽】
1,这题不需要用树状数组维护,树状数组反而麻烦
2,当区间范围内的数字个数为0的时候,也就相应无解化了。
比如这组数据——
5
1 1 1 1 1
1 1 1 1 1
不过由于我是连续乘上num,num变为0之后答案也自然会停留在0,所以不影响AC~
【题意】
n(1e5)个数的全排列a[]
我们知道——
a[1]~a[i]的最小值为mn[i]
a[1]~a[i]的最大值为mx[i]
问你有多少种合法的a[]
【类型】
贪心
【分析】
显然——
1,mn只会越来越小,mx只会越来越大
2,如果mn和mx的范围被更新了,则方案数唯一
否则方案数位区间数字个数,我们即时更新即可。
【时间复杂度&&优化】
O(Tn)
*/