RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
ICPC00030

H. Matryoshka

Unrated 레이팅 미적용
난이도
5s
시간 제한
1024MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

Matryoshkas are sets of traditional Russian wooden dolls of decreasing size placed one inside the other. A matryoshka doll can be opened to reveal a smaller figure of the same sort inside, which has, in turn, another figure inside, and so on. The Russian Matryoshka Museum recently exhibited a c\(ol- le\)ction of similarly designed matryoshka sets, differing only in the number of nested dolls in each set. Unf\(or- tu\)nately, some ov\(er-ze\)alous (and obviously unsupervised) children separated these sets, placing all the individual dolls in a row. There are \(n\) dolls in the row, each with an integer size. You need to reassemble the matryoshka sets, knowing neither the number of sets nor the number of dolls in each set. You know only that every complete set consists of dolls with consecutive sizes from 1 to some number \(m\), which may vary between the different sets. When reassembling the sets, you must follow these rules:

  • You can put a doll or a nested group of dolls only inside a larger doll.

  • You can combine two groups of dolls only if they are adjacent in the row.

  • Once a doll becomes a member of a group, it cannot be transferred to another group or p\(er- ma\)nently separated from the group. It can be temporarily separated only when combining two groups. Your time is valuable, and you want to do this reassembly process as quickly as possible. The only ti\(me-co\)nsuming part of this task is opening and subsequently closing a doll, so you want to minimize how often you do this. For example, the minimum number of openings (and subsequent closings) when combining group [1, 2, 6] with the group [4] is two, since you have to open the dolls with sizes 6 and 4. When combining group [1, 2, 5] with the group [3, 4], you need to perform three openings. Write a program to calculate the minimum number of openings required to combine all disassembled matryoshka sets.

제약
입력 형식

The input consists of a single test case. A test case consists of two lines. The first line contains one integer \(n\) (\(1 \le n \le 500\)) representing the number of individual dolls in the row. The second line contains \(n\) positive integers specifying the sizes of the dolls in the order they appear in the row. Each size is between 1 and 500 inclusive.

출력 형식

Display the minimum number of openings required when reassembling the matryoshka sets. If reass\(em- bl\)ing cannot be done (some of the kids might have been excessively zealous and taken some dolls), display the word impossible.

예제 1
입력
7
1 2 3 2 4 1 3
출력
7
예제 2
입력
7
1 2 1 2 4 3 3
출력
impossible
문제 정보

생성자가 기록되지 않았습니다.

출처 ICPC World Finals 2013

평가 및 의견

H. Matryoshka

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

H. Matryoshka

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8