Permutation Pattern

시간 제한2초메모리 제한1024 MB

요약
순열에서 231 패턴을 피하는 부분수열의 개수를 센다. n은 최대 50이다.
난이도

어려움10점 중 8점

유형
동적 계획법, 조합론, 수학
정답자
아직 제출이 없습니다

문제

A sequence a_1,…,a_ma\_1, \dots, a\_m of mm distinct numbers is called *without 231* if there is **no** triples (i,j,k)(i, j, k) where 1≤i<j<k≤m1 \leq i < j < k \leq m and a_k<a_i<a_ja\_k < a\_i < a\_j.

Bobo has a permutation p_1,…,p_np\_1, \dots, p\_n of 1,…,n1, \dots, n, and he can remove some (possibly none, but not all) elements from the permutation. Find the number of sequences without 231231 among (2n−1)(2^n - 1) resulting permutations.

입력

The input consists of several test cases terminated by end-of-file. For each test case,

The first line contains an integer nn.

The second line contains nn integers p_1,…,p_np\_1, \dots, p\_n.

출력

For each test case, output an integer which denotes the number of sequences.

제한

  • 1≤n≤501 \leq n \leq 50
  • 1≤p_i≤n1 \leq p\_i \leq n for each 1≤i≤n1 \leq i \leq n
  • In each input, the sum of nn does not exceed 500500.

예제1

  1. 예제 1

    입력
    2
    2 1
    3
    1 2 3
    4
    2 3 4 1
    
    예상 출력
    3
    7
    11