Arrange

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 .
 

Input
The first line of input contains an integer T (1T15) , which denotes the number of testcases.

For each test case, the first line of input contains single integer n (1n105) .

The second line contains n integers, the i -th integer denotes Bi (1Bin) .

The third line contains n integers, the i -th integer denotes Ci (1Cin) .
 

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.
 

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
*/
啊啊啊~我好蠢啊~~我怎么会这么蠢啊~~
### `arrange` 函数的功能与使用方法 在 R 语言的 `dplyr` 包中,`arrange` 函数用于对数据框(data frame)按照一个或多个变量进行排序。该函数默认按升序排列数据,但也可以通过 `desc()` 函数实现降序排序。`arrange` 函数的基本语法如下: ```r arrange(data, var1, var2, ...) ``` 其中,`data` 是要排序的数据框,`var1`, `var2` 等是要排序的列名。若需要对某一列进行降序排序,可以在该列名前加上 `desc()` 函数 [^2]。 例如,若有一个数据框 `df`,包含 `name`、`age` 和 `score` 三列,可以通过以下代码按照 `age` 升序排列: ```r arrange(df, age) ``` 如果需要按照 `age` 升序且 `score` 降序排列,可以使用以下代码: ```r arrange(df, age, desc(score)) ``` 此外,`arrange` 函数还支持使用负号 `-` 来实现降序排列,例如: ```r arrange(df, -score) ``` 上述代码将按照 `score` 列进行降序排列 [^3]。 ### 与 Python 中 `np.arange` 的区别 需要注意的是,R 语言中的 `arrange` 函数与 Python 中 NumPy 库的 `arange` 函数不同。Python 的 `np.arange` 主要用于生成一维数组,其基本形式为: ```python np.arange(start, stop, step) ``` 其中 `start` 是起始值,`stop` 是结束值(不包含在结果中),`step` 是步长。例如: ```python np.arange(0, 5, 1) # array([0, 1, 2, 3, 4]) np.arange(0, 1, 0.3) # array([0. , 0.3, 0.6, 0.9]) ``` 与 R 的 `arrange` 不同,`np.arange` 主要用于生成数值序列,而不是对数据框进行排序 [^4]。 ### 使用前的准备 在使用 `arrange` 函数之前,需要先加载 `dplyr` 包,可以通过以下代码实现: ```r library(dplyr) ``` 加载完成后,即可使用 `arrange` 函数对数据框进行排序操作 [^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值