HDU 5719Arrange(类似排列组合问题)

Arrange

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


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 $ A_i $.

She is given some information about the final order of the crops:

1. the minimum value of $ A_1, A_2, ..., A_i $ is $ B_i $.

2. the maximum value of $ A_1, A_2, ..., A_i $ is $ C_i $.

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 \le T \le 15) $, which denotes the number of testcases.

For each test case, the first line of input contains single integer $ n $ $ (1 \le n \le 10 ^ 5) $.

The second line contains $ n $ integers, the $ i $-th integer denotes $ B_i $ $ (1 \le B_i \le n) $.

The third line contains $ n $ integers, the $ i $-th integer denotes $ C_i $ $ (1 \le C_i \le 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 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.

题意:给出一排前i个值得最小值,给出一排前i个值得最大值,问有多少种满足情况的序列

思路:第一排非递增,第二排费递减,否则没有满足情况的序列。由刚才条件得可满足区间是递增的,但是题目要求每个值只能用一次,所以我们还得知道哪些值用过了。

细想可知,可满足区间内用过的值只会出现的前面,不用管后面。

所以①当出现新的最小值,可满足区间sum扩大(刚才的最小值minn[i-1]减去现在的最小值minn[i]减一)。

②当出现新的最大值,可满足区间sum扩大(现在的最大值t减去刚才的最大值maxx减一)。

③当最大值不变,这个数随机的,乘以可满足区间sum,然后sum--即可。


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
#define mod 998244353
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 1e5+5;
const int ff = 0x3f3f3f3f;

int n;
ll minn[maxn];//记录前i项的最小值
int vis[maxn];//记录第i个值是否可以确定

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		mem(vis,0);
		ll ans = 1,sum = 0;
		int flag = 1;
		scanf("%d",&n);
		ll minx = ff,maxx;
		for(int i = 1;i<= n;i++)
		{
			ll t;
			scanf("%lld",&t);
			if(t< minx)
			{
				minx = t;
				vis[i] = 1;
			}
			else if(t == minx)
				vis[i] = 0;
			else
				flag = 0;
			minn[i] = minx;
		}
		
		maxx = minn[1];
		for(int i = 1;i<= n;i++)
		{
			ll t;
			scanf("%lld",&t);
			if(vis[i] == 1)//如果当前值已经可以确定
			{
				if(t == maxx)
				{
					if(i!= 1)//对1特判 
						sum+= minn[i-1] - minn[i]-1;//更新可满足区间 
				}
				else
					flag = 0;
				continue;
			}
			
			if(t> maxx)//更新可满足区间 
			{
				sum+= t-maxx-1;
				maxx = t;
			}
			else if(t == maxx)
			{
				ans = ans*sum%mod;
				sum--;
			}
			else
				flag = 0;
		}
		if(flag)
			printf("%lld\n",ans);
		else
			printf("0\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值