1280. Permutation
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Given a permutation of n elements (1, 2, ..., n): A = (a1, a2, ..., an).We define a sequence P(A)=(p1, p2, …, p(n-1)) where pi = 0 if A(i) >A(i+1) and pi = 1 if A(i) < A(i+1). Given a permutation B, find thenumber of all permutations C where P(C)=P(B) including the permutation B itself.
Input
The input text file contains several tests, each on a separate line. The first number in the test is n, which not greater than 100, followed by n numbers representing the permutation all of them separated by a single space. The last line in the input contatins only 0 and should not be processed.
Output
The output should be written on the standard output. For each line in the input (excluding the last one, 0) you should write the result i.e. the number of the permutations having the same value for P(A) whengiven the permutation A.
Sample Input
2 1 2 4 1 3 2 4 0
Sample Output
1 5
Problem Source
ZSUACM Team Member
http://blog.csdn.net/rappy/article/details/1797699
看了别人的解题报告,都写得那么好了,我这种渣渣不用说什么了,按照他的思路加上些图辅助说明吧。
#include <stdio.h>
#include <string.h>
int main() {
int n, a[101], b[101], ans[101][101];
while (scanf("%d", &n) && n) {
memset(ans, 0, sizeof(ans));
memset(b, 0, sizeof(b));
ans[0][0] = 1;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n - 1; i++) {
if (a[i] < a[i + 1])
b[i + 1] = 1;
}
for (int i = 1; i < n; i++) {
if (b[i] == 1) {
for (int j = 0; j <= i; j++) {
for (int k = j + 1; k <= i; k++) { // 如果上升则 cnt [i - 1] [j] 对应的位置都能够产生 cnt [i] [j+1..i] 对应的某个位置,上升的话,比当前点低的点数就是j+1到i
ans[i][k] += ans[i - 1][j];
}
}
} else {
for (int j = 0; j <= i; j++) {
for (int k = 0; k <= j; k++) {
ans[i][k] += ans[i - 1][j];// 如果下降则 cnt [i - 1] [j] 对应的位置都能够产生 cnt [i] [0..j] 对应的某个位置,下降的话,比当前点低的点数就是0到j
}
}
}
}
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += ans[n - 1][i];
}
printf("%lld\n", sum);
}
return 0;
}