Arrange
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 715 Accepted Submission(s): 248
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
BestCoder 2nd Anniversary
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cctype>
using namespace std;
int main()
{
int n;
int a;
int flag = 0;
cin >> n;
while (n--)
{
flag = 0;
cin >> a;
int b[a];
int c[a];
int d[a+1];
memset(d, 0, sizeof(d));
for (int i=0; i<a; i++)
{
cin >> b[i];
}
for (int i=0; i<a; i++)
{
cin >> c[i];
}
if (b[0] != c[0])
{
cout << "0" << endl;
continue;
}
else
{
d[b[0]] = 1;
if (a == 1)
{
cout << "1" << endl;
continue;
}
}
for (int i=1; i<a; i++)
{
if (b[i] > b[i-1] || c[i] < c[i-1])
{
flag = 1;
break;
}
}
for (int i=1; i<a; i++)
{
if (b[i] < b[i-1] && c[i] > c[i-1])
{
flag = 1;
break;
}
}
for (int i=1; i<a; i++)
{
if (b[i] == c[i])
{
if (d[b[i]] == 1)
{
flag = 1;
}
else
{
d[b[i]] = 1;
}
}
}
for (int i=1; i<a; i++)
{
if (c[i] > c[i-1] && b[i] == b[i-1])
{
if (d[c[i]] == 1)
flag = 1;
else
d[c[i]] = 1;
//cout << "c " << c[i-1] << " " << c[i] << endl;
}
if (b[i] < b[i-1] && c[i] == c[i-1])
{
if (d[b[i]] == 1)
flag = 1;
else
d[b[i]] = 1;
//cout << "b " << b[i-1] << " " << b[i] << endl;
}
}
long long ans = 1;
for (int i=0; i<a; i++) //好家伙,就这一小for循环~~
{
if (c[i] == c[i-1] && b[i] == b[i-1])
{
ans = ans * (c[i]-b[i]-i+1)%998244353;
}
}
/*for (int i=1; i<a; i++)
{
int f = 0;
if (c[i] == c[i-1] && b[i] == b[i-1])
{
int y = max (b[i], c[i]);
int x = min (b[i], c[i]);
for(int j=x; j<=y; j++)
{
if (d[j] == 0)
{
f = 1;
}
}
if (f == 0)
{
flag = 1;
break;
}
}
}
long long amo = 0;
for (int i=1; i<a+1; i++)
{
if (d[i] == 0)
{
amo++;
}
}
int i;
for (i=1; amo>0 && i<a; i++)
{
if (b[i] != c[i])
{
ans = ans * (amo--);
}
}
if (i != a)
{
flag = 1;
}*/
if (flag)
{
cout << "0" << endl;
}
else
{
ans = ans % 998244353;
cout << ans << endl;
}
}
return 0;
}
/*
10
5
2 1 1 1 1
2 2 5 5 5
3
2 1 1
2 2 3
5
5 4 3 2 1
1 2 3 4 5
*/
啊啊啊~我好蠢啊~~我怎么会这么蠢啊~~