hdu5719 Arrange

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

可以发现A_1 = B_1 = C_1A1​​=B1​​=C1​​,所以如果B_1 \neq C_1B1​​C1​​无解。

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

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

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

 

 

Arrange

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 543    Accepted Submission(s): 191


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
 
 
#include<iostream>
#include<stdio.h>
using namespace std;
const int maxx = 100005;
int b[maxx];
int c[maxx];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        int flag=1;
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&b[i]);
            if(i!=0){
                if(b[i]>b[i-1]) flag=0;
            }
        }
        long long ans=1;
        for(int i=0;i<n;i++){
            scanf("%d",&c[i]);
            if(i&&flag){
               if(c[i]<c[i-1]) flag=0;
               if(c[i]!=c[i-1]&&b[i]!=b[i-1]) flag=0;
               if(c[i]-b[i]<i) flag=0;
               if(c[i]<=b[i]) flag=0;
               if(c[i]==c[i-1]&&b[i]==b[i-1])
                 ans*=(c[i]-b[i]+1-i);
               else ans*=1;
               ans%=998244353;
//                cout<<"  "<<"i"<<i<<"  ans:  "<<ans<<endl;
            }else{
                if(b[i]!=c[i]){
                    flag=0;
                }
            }
        }
        if(flag){
            printf("%lld\n",ans);
        }else{
            printf("0\n");
        }
    }
    return 0;
}
View Code

 

posted @ 2016-07-18 15:02  超级学渣渣  阅读(186)  评论(0编辑  收藏  举报