【HDU5719 BestCoder 2nd AnniversaryB】【模拟水题 范围更新】Arrange 已知最大最小值前缀和求全排列方案数

99 篇文章 0 订阅
36 篇文章 0 订阅

Arrange

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


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
 

Recommend
wange2014


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 998244353, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n;
int a[N], mn[N], mx[N];
int solve()
{
	if (mn[1] != mx[1])return 0;
	LL ans = 1;
	int num = 0;
	for (int i = 2; i <= n; ++i)
	{
		if (mx[i] < mx[i - 1] || mn[i] > mn[i - 1])return 0;
		if (mx[i] > mx[i - 1] && mn[i] < mn[i - 1])return 0;
		if (mx[i] > mx[i - 1] || mn[i] < mn[i - 1]);
		else ans = ans * num % Z;
		num += (mx[i] - mx[i - 1]) + (mn[i - 1] - mn[i]) - 1;
	}
	return ans;
}
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i)scanf("%d", &mn[i]);
		for (int i = 1; i <= n; ++i)scanf("%d", &mx[i]);
		printf("%d\n", solve());
	}
	return 0;
}
/*
【trick&&吐槽】
1,这题不需要用树状数组维护,树状数组反而麻烦
2,当区间范围内的数字个数为0的时候,也就相应无解化了。

比如这组数据——
5 
1 1 1 1 1
1 1 1 1 1
不过由于我是连续乘上num,num变为0之后答案也自然会停留在0,所以不影响AC~

【题意】
n(1e5)个数的全排列a[]
我们知道——
a[1]~a[i]的最小值为mn[i]
a[1]~a[i]的最大值为mx[i]
问你有多少种合法的a[]

【类型】
贪心

【分析】
显然——
1,mn只会越来越小,mx只会越来越大
2,如果mn和mx的范围被更新了,则方案数唯一
否则方案数位区间数字个数,我们即时更新即可。

【时间复杂度&&优化】
O(Tn)


*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值