题目链接:https://vjudge.net/contest/398708#problem/I

DreamGrid has an interesting permutation of 1,2,…,n denoted by a1,a2,…,an. He generates three sequences f, g and h, all of length n, according to the permutation a in the way described below:

For each 1≤i≤n, fi=max{a1,a2,…,ai};
For each 1≤i≤n, gi=min{a1,a2,…,ai};
For each 1≤i≤n, hi=fi−gi.
BaoBao has just found the sequence h DreamGrid generates and decides to restore the original permutation. Given the sequence h, please help BaoBao calculate the number of different permutations that can generate the sequence h. As the answer may be quite large, print the answer modulo 10^9+7.

Input
The input contains multiple cases. The first line of the input contains a single integer T (1≤T≤20000), the number of cases.

For each case, the first line of the input contains a single integer n (1≤n≤105), the length of the permutation as well as the sequences. The second line contains n integers h1,h2,…,hn (1≤i≤n,0≤hi≤10^9).

It’s guaranteed that the sum of n over all cases does not exceed 2⋅106.

Output
For each case, print a single line containing a single integer, the number of different permutations that can generate the given sequence h. Don’t forget to print the answer modulo 10^9+7.

Input

3
3
0 2 2
3
0 1 2
3
0 2 3

Output

2
4
0

Note
For the first sample case, permutations {1,3,2} and {3,1,2} can both generate the given sequence.

For the second sample case, permutations {1,2,3}, {2,1,3}, {2,3,1} and {3,2,1} can generate the given sequence.

翻译:

原排列p为a1,a2,…,an
fi=max(a1,a2,…,ai),gi=min(a1,a2,…,ai)fi= max (a1,a2,…,ai),gi=min(a1,a2,…,ai )fi=max(a1,a2,…,ai),gi=min(a1,a2,…,ai) ,hi=fi−gihi=fi−gihi=fi−gi,现给出序列h,求满足h的P的个数

分析:
h数列只能是递增的,且第一个数必为0,最大值为n-1

对于一个递增的数列只有两种情况:
1.h[i]=h[i-1]
2.h[i]>h[i-1]

对应第二种情况是:最大值有更新或最小值有更新,对应的结果*2
对应第一种情况:a[i]的取值范围为:
min(a1,a2,,,,,,ai−1)<=a[i]<=max(a1,a2,,,,,,ai−1)min(a_1,a_2,,,,,,a_{i-1})<=a[i]<=max(a_1,a_2,,,,,,a_{i-1})min(a1​,a2​,,,,,,ai−1​)<=a[i]<=max(a1​,a2​,,,,,,ai−1​)
定义大于等于min,小于等于max的数为中间数
假设中间数的个数为num,对应的结果*num

当h[i]>h[i-1]时,h[i]-h[i-1]-1为加上h[i]后新增的中间数的个数

完整代码:

#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=1e9+7;
int n;
LL height[N];
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=0; i<n; i++)scanf("%lld",&height[i]);bool book=false;for(int i=1; i<n; i++){if(height[i-1]>height[i]||height[i]>=n){book=true;break;}}if(height[0]!=0)book=true;if(book)printf("0\n");else{LL sum=1,ans=0;for(int i=1; i<n; i++){if(height[i]>height[i-1]){sum=(sum*2)%mod;ans+=height[i]-height[i-1]-1;///新增的中间数}else{sum=(sum*ans)%mod;ans--;///用掉一个}}printf("%lld\n",sum%mod);}}return 0;
}

Gym - 102394I Interesting Permutation(思维)相关推荐

  1. I - Interesting Permutation Gym - 102394I(排列组合)

    题意: 纯数题 1≤i≤n, fi=max{a1,a2,-,ai}; 1≤i≤n, gi=min{a1,a2,-,ai}; 1≤i≤n, hi=fi−gi. 数列a是一个排列,问多少种排列方式满足h数 ...

  2. CodeForces - 618B Guess the Permutation(思维+构造)

    题目链接:点击查看 题目大意:先给出一个长度为n的序列ai,这个序列是1~n全排列中的其中一种,再给出一个n*n的矩阵,maze[i][j]=val代表min(ai,aj)=val,要求我们构造出原始 ...

  3. Gym - 101972H Beautiful Substrings(思维+模拟)

    题目链接:点击查看 题目大意:题目的意思挺难理解的..尤其是对我这种英语渣来说,简单说一下,就是先给出三个数字n,m,k,然后再给出两个字符串a和b,n和m代表的是字符串a和b的长度,然后描述题意: ...

  4. leetcode 484. Find Permutation 思维题

    https://leetcode.com/contest/leetcode-weekly-contest-16a/problems/find-permutation/ 设原本的数字是0,那么按照它的D ...

  5. Gym - 101350E-Competitive Seagulls-博弈-思维

    https://vjudge.net/problem/Gym-101350E 题解见代码注释. #include <bits/stdc++.h> /*博弈,两个海鸥在玩游戏,长度为l的一个 ...

  6. 【Gym - 100482B Farmer 】 思维

    B - Farmer John is a successful farmer and he would like to expand his business. For this reason he ...

  7. c语言六角填数蓝桥杯答案,六角填数(全排列)蓝桥杯真题

    六角填数(全排列)蓝桥杯真题 六角填数(全排列)蓝桥杯真题 如图所示六角形中填入1-12的数字,使每条直线上的数字和相等,图中已经填好了3个数字,请你计算*号数字是多少 蓝桥杯老套路,经常这样考全排列 ...

  8. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)

    Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) 题号 题目 知识点 A XORinacci B Uniqueness ...

  9. QDU-Training-01

    QDU-Training-01 题号 题目 知识点 难度 CodeForces 76E Points 数论 HDU 4608 I-number 模拟 CodeForces 616D Longest k ...

最新文章

  1. iOS10 UI教程子视图和父视图UI层次结构和Views继承
  2. 知识图谱前端插件_大型前端项目可持续演进开发的思考
  3. python函数中可变参数的传递方式是_详解Python函数可变参数定义及其参数传递方式...
  4. 关于 Oracle分页数据重复的问题
  5. textarea回车不换行 小程序_微信小程序商城到底值得不值得开通?
  6. 【Elasticsearch】如何使用 Elasticsearch 6.2 搜索中文、日文和韩文文本 - 第 1 部分: 分析工具
  7. centos 源码安装 mysql
  8. LeetCode刷题(33)
  9. 新浪微博 API 使用入门
  10. java scanner以回车结束_大佬看了直呼内行,你当初Java刚入门是否也是这样写代码?...
  11. 视频教程-excel VBA编程番外篇(字典+正则表达式+FSO)-Office/WPS
  12. C语言知识点总结 (一 )
  13. 从支付架构到风控报警,支付系统的设计如何环环相扣?
  14. 关于名为民间借贷实为诈骗案件的讨论
  15. matlab直方图概率密度图,histeq从用法到原理——Matlab直方图均衡化函数
  16. tempo js渲染引擎
  17. 非IE浏览器(谷歌、火狐、Edge)使用IE打开指定链接
  18. 哈尔滨工业大学 计算机系统大作业
  19. 大文件上传到linux服务器,上传文件到服务器的Linux命令
  20. 《大学两年的摸爬滚打:新征程》

热门文章

  1. 林群院士:从数学谈教育
  2. Linux双独立显卡SLI,完美的解决方案:双显卡不需要使用双水冷Tt提供SLi冷却解决方案...
  3. js控制的Flv视频播放器源码下载
  4. 大数据培训就业班毕业后通常可以从事哪些领域做哪些方面工作
  5. Java面试题--dalao总结版
  6. 2021华为杯数学建模D题完整思路
  7. 前端文档网站快速生成工具
  8. TOSCA自动化测试工具
  9. macOS运行Xcode缓慢
  10. python因数之和等于数字本身,完全数,盈数,亏数到底是什么鬼?python实现给你看...