Sicily 1280. Permutation

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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值