HDU 5719 BestCoder 2nd Anniversary Arrange (DP)
来源:互联网 发布:淘宝医疗器械类目 编辑:程序博客网 时间:2024/05/23 17:13
Arrange
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 558 Accepted Submission(s): 198
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 aren heaps of crops in total, numbered from 1 to n .
Psyche needs to arrange them in a certain order, assume crops on thei -th position is Ai .
She is given some information about the final order of the crops:
1. the minimum value ofA1,A2,...,Ai is Bi .
2. the maximum value ofA1,A2,...,Ai is Ci .
She wants to know the number of valid permutations. As this number can be large, output it modulo998244353 .
Note that if there is no valid permutation, the answer is0 .
This has drawn the fury of his mother, Venus. The goddess then throws before Psyche a great mass of mixed crops.
There are
Psyche needs to arrange them in a certain order, assume crops on the
She is given some information about the final order of the crops:
1. the minimum value of
2. the maximum value of
She wants to know the number of valid permutations. As this number can be large, output it modulo
Note that if there is no valid permutation, the answer is
Input
The first line of input contains an integerT (1≤T≤15) , which denotes the number of testcases.
For each test case, the first line of input contains single integern (1≤n≤105) .
The second line containsn integers, the i -th integer denotes Bi (1≤Bi≤n) .
The third line containsn integers, the i -th integer denotes Ci (1≤Ci≤n) .
For each test case, the first line of input contains single integer
The second line contains
The third line contains
Output
For each testcase, print the number of valid permutations modulo998244353 .
Sample Input
232 1 12 2 355 4 3 2 11 2 3 4 5
Sample Output
10HintIn 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
Recommend
wange2014 | We have carefully selected several similar problems for you: 5722 5721 5720 5717 5716
题目大意: 求出合法的1~n的全排列的种数。
他会给你两行数。
第一行表示的Ai 表示这个排列的前 i 个的最小值为Ai。
第二行表示的B i表示这个排列的前 i 个的最大值为Bi。、
要你求出符合这些条件的全排列的种数。
题解:首先排除掉5种不可能的情况,1.b[i]>b[i-1] 2,c[i]<c[i-1] 3,b[i]>c[i] 4.c[1]!=b[1] 5.b[i],c[i] < 1 || > n ,
然后递推,如果当前产生的新的 b[i]或者 c[i] 那么dp[i] = dp[i-1] ,
如果当前 b[i-1] = b[i] && c[i-1] = c[i] ,那么我们可以在 [b[i],c[i]]中任选一个数,
但是由于这些数是互不相同的,所以每次我们的选的数都会逐渐变少。
用num计算一下当前已经选了多少种,减掉之后答案即为 dp[i] = dp[i-1]*(c[i]-b[i]+1-num)。
然后递推,如果当前产生的新的 b[i]或者 c[i] 那么dp[i] = dp[i-1] ,
如果当前 b[i-1] = b[i] && c[i-1] = c[i] ,那么我们可以在 [b[i],c[i]]中任选一个数,
但是由于这些数是互不相同的,所以每次我们的选的数都会逐渐变少。
用num计算一下当前已经选了多少种,减掉之后答案即为 dp[i] = dp[i-1]*(c[i]-b[i]+1-num)。
AC代码:
#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm> using namespace std;typedef long long LL;const int mod =998244353;const int N=100005;int b[N];int c[N];LL dp[N];int main(){ int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int Min=999999; int Max=-1; int flag=1; for(int i=1;i<=n;i++) { scanf("%d",&b[i]); if(b[i]>Min) flag=0; if(b[i]<1||b[i]>n) flag=0; Min=min(Min,b[i]);}for(int i=1;i<=n;i++){scanf("%d",&c[i]);if(c[i]<Max)flag=0;if(c[i]<1||c[i]>n) flag=0;if(c[i]<b[i]) flag=0;Max=max(Max,c[i]);}if(!flag||c[1]!=b[1]) puts("0"); //前 1个的最大最小值一定相等 ,都是这个数嘛 else{memset(dp,0,sizeof(dp));dp[1]=1;int num=1;for(int i=2;i<=n;i++) //dp核心 {if(c[i]==c[i-1]&&b[i]==b[i-1]&&b[i]!=c[i]){dp[i]=dp[i-1]*(c[i]-b[i]+1-num)%mod;}else if( b[i]<b[i-1]&&c[i-1]==c[i] || b[i]==b[i-1]&&c[i-1]<c[i] ){//dp[i] = dp[i-1]+1;dp[i]=dp[i-1];}num++; //已经选了多少种} printf("%I64d\n",dp[n]);}} return 0;}
1 0
- HDU 5719 BestCoder 2nd Anniversary Arrange (DP)
- BestCoder 2nd Anniversary Arrange
- BestCoder 2nd Anniversary HDU5719 Arrange
- BestCoder 2nd Anniversary 1002/hdu5719 Arrange
- BestCoder 2nd Anniversary B hdu 5719 树状数组
- HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)
- HDU 5718 oracle BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary
- BestCoder 2nd Anniversary-1005
- BestCoder 2nd Anniversary Oracle
- BestCoder 2nd Anniversary Palace
- BestCoder 2nd Anniversary HDU5720
- BestCoder 2nd Anniversary Oracle
- BestCoder 2nd Anniversary A hdu 5718 模拟
- AndroidManifest.xml中属性
- c字符串的一些操作
- textField 占位字体的颜色改变
- Android Toast使用详解
- 又一次内存分配失败(关于overcommit_memory)
- HDU 5719 BestCoder 2nd Anniversary Arrange (DP)
- JRE System Library [JavaSE-1.7](unbound)
- iOS开发之应用内检测手机锁屏,解锁状态
- 随笔记录
- 从苏宁电器到卡巴斯基第15篇:我在苏宁电器当营业员 VII
- cocoaPods版本更新问题
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- KD-Tree Python实现
- StartedService和BoundService的区别