아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Up Down Subsequence

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

요약
순열과 U/D 문자열이 주어질 때, 앞에서부터 K개의 부등호를 만족하는 부분수열의 최대 길이를 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 이분 탐색, 배열
정답자
아직 제출이 없습니다

문제

Farmer John's NN cows (2≤N≤3⋅1052 \leq N \leq 3\cdot 10^5), conveniently numbered 1…N1 \ldots N as usual, have ordered themselves according to a permutation p_1,p_2,…,p_Np\_1,p\_2,\ldots,p\_N of 1…N1\ldots N. You are also given a string of length N−1N-1 consisting of the letters U and D. Please find the maximum K≤N−1K\le N-1 such that there exists a subsequence a_0,a_1,…,a_Ka\_0,a\_1,\ldots,a\_{K} of pp such that for all 1≤j≤K1\le j\le K, a_j−1<a_ja\_{j - 1} < a\_j if the jjth letter in the string is U, and a_j−1>a_ja\_{j - 1} > a\_j if the jjth letter in the string is D.

입력

The first line contains NN.

The second line contains p_1,p_2,…,p_Np\_1,p\_2,\ldots,p\_N.

The last line contains the string.

출력

Write out maximum possible value of KK.

예제2

  1. 예제 1

    입력
    5
    1 5 3 4 2
    UDUD
    
    예상 출력
    4
    
  2. 예제 2

    입력
    5
    1 5 3 4 2
    UUDD
    
    예상 출력
    3