杭電多校第10場 6887 Task Scheduler(DP)
Problem Description
For a given permutation a1,a2,⋯,an of length n, we defined the neighbor sequence b of a, the length of which is n−1, as following:
bi={01ai<ai+1ai>ai+1
.
For example, the neighbor sequence of permutation 1,2,3,6,4,5 is 0,0,0,1,0.
Now we give you an integer n and a sequence b1,b2,⋯,bn−1 of length n−1, you should calculate the number of permutations of length n whose neighbor sequence equals to b.
To avoid calculation of big number, you should output the answer module 109+7.
Input
The first line contains one positive integer T (1≤T≤50), denoting the number of test cases. For each test case:
The first line of the input contains one integer n,(2≤n≤5000).
The second line of the input contains n−1 integer: b1,b2,⋯,bn−1
There are no more than 20 cases with n>300.
Output
For each test case:
Output one integer indicating the answer module 109+7.
Sample Input
2
3
1 0
5
1 0 0 1
Sample Output
2
11
題意:
求多少種前n個數的排列符合題目中的限制
思路:
定義
f
[
i
]
[
j
]
f[i][j]
f[i][j]為考慮了前
i
i
i個數的排列,第
i
i
i個位置放的數在前
i
i
i個數中為
j
j
j的小方案數。
那麼如果
a
[
i
]
>
a
[
i
−
1
]
,
a[i]>a[i-1],
a[i]>a[i−1],也就是
b
[
i
−
1
]
=
0
b[i-1]=0
b[i−1]=0。
f
[
i
]
[
j
]
=
∑
f
[
i
−
1
]
[
k
]
,
1
≤
k
≤
j
−
1
f[i][j]=∑f[i-1][k],1≤k≤j-1
f[i][j]=∑f[i−1][k],1≤k≤j−1
j
j
j可以等於
k
k
k,因為前
i
i
i個數的第
j
j
j小一定小於前
i
−
1
i-1
i−1個數的第
j
j
j小。下面
j
j
j可以等於
k
k
k同理。
如果
a
[
i
]
<
a
[
i
−
1
]
,
a[i]<a[i-1],
a[i]<a[i−1],也就是
b
[
i
−
1
]
=
1
b[i-1]=1
b[i−1]=1。
f
[
i
]
[
j
]
=
∑
f
[
i
−
1
]
[
k
]
,
j
≤
k
≤
i
−
1
f[i][j]=∑f[i-1][k],j≤k≤i-1
f[i][j]=∑f[i−1][k],j≤k≤i−1
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 5005;
const int mod = 1e9 + 7;
ll f[maxn][maxn];
int a[maxn];
int main() {
int T;scanf("%d",&T);
while(T--) {
int n;scanf("%d",&n);
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= n;j++) {
f[i][j] = 0;
}
}
for(int i = 1;i < n;i++) scanf("%d",&a[i]);
f[1][1] = 1;
for(int i = 2;i <= n;i++) {
ll sum = 0;
if(a[i - 1] == 0) { //a[i]>a[i-1]
for(int j = 1;j <= i;j++) {
f[i][j] = sum;
sum = (sum + f[i - 1][j]) % mod;
}
} else { //a[i]<a[i-1]
for(int j = i - 1;j >= 1;j--) {
sum = (sum + f[i - 1][j]) % mod;
f[i][j] = sum;
}
}
}
ll ans = 0;
for(int i = 1;i <= n;i++) {
ans = (ans + f[n][i]) % mod;
}
printf("%lld\n",ans);
}
return 0;
}
相關文章
- 2024杭電多校第9場
- 2024杭電多校第8場
- 2024杭電多校第九場
- 杭電多校補題
- 【記憶優化搜尋/dp】HDU - 6415 - 杭電多校第九場 - Rikka with Nash Equilibrium優化UI
- 2024牛客多校第10場
- 2024杭電多校覆盤 (1~5)
- HDU 6311 - Cover [2018杭電多校聯賽第二場 C](尤拉通路/迴路)
- HDU 6415(dp/找規律-2018多校第九場1001)
- 2018杭電暑假多校知識點總結(附大一結語)
- Leetcode-Medium 621. Task SchedulerLeetCode
- HDU多校第九次 6415 (dp
- cornerstone中delayed_task,timer_task及scheduler原始碼解析原始碼
- 杭電2046
- 【dp+組合數學】hdu 2018 多校第九場 1001 Rikka with Nash Equilibrium hdu 6415UI
- [DP]HDU6415(2018多校訓練賽第九場 Problem A) Rikka with Nash Equilibrium 題解UI
- 杭電oj 2020
- 杭電No2000
- 『杭電1810』Rating of Tetris
- 『杭電1940』ICPC Scoreboard
- 『杭電1939』He is offside!IDE
- 『杭電1937』Finding Seats
- 杭電hdu2072
- 杭電2048(遞推)
- (杭電1406)完數
- 2024牛客多校第七場
- 2024牛客多校第四場
- 2024牛客多校第三場
- 24牛客多校第一場
- 『杭電1848』Fibonacci again and againAI
- (補題 杭電 1008)Elevator
- 2024牛客多校第一場 - Mirror Maze
- 2020牛客多校第八場K題
- Codeforces 11D A Simple Task 題解 [ 藍 ] [ 狀壓 dp ]
- 杭電HDU2018 奶牛的故事
- Scheduler in Oracle Database 10g(轉)OracleDatabase
- 2020HDU多校第三場 1005 Little W and Contest
- 多執行緒系列(四):Task執行緒