grupe | 프로그래밍의 벗 PivotOJ
PivotOJ

grupe

시간 제한: 1000ms메모리 제한: 128MB출처: CHC 2004 Final Exam #1BOJ 3224

문제

First N positive integers (numbers from 1 to N) are written on the blackboard in some arbitrary order (from left to right).

A group is set of integers which form an interval. For example, sets [2], [4 5] and [3 5 6 4] are groups, but [5 7 2] and [2 4 5] aren't. At the beginning, we assume that each number on the blackboard forms a single group with only itself in it.

There is only one allowed operation - concatenating two adjacent groups, but only if the new set would be a group.

Write a program which will determine wheather sequence of N-1 operations exists, after which all written numbers will be in the same group. If such sequence exists, your program must find at least one of them.

Example (one possible solution for third example):

[6] [3] [2] [1] [4] [5]
[6] [3] [2 1] [4] [5]
[6] [3 2 1] [4] [5]
[6] [3 2 1] [4 5]
[6] [3 2 1 4 5]
[6 3 2 1 4 5] 

입력

In the first line, there will be integer N, 1 ≤ N ≤ 500,000.

In the second line, there will be N positive integers, separated by single space. These numbers are written on the blackboard (i.e. this is an initial state).

출력

In the first line there must be word 'DA' (yes) or 'NE' (no), depending wheather requested sequence exists. If given answer is 'NE', then first line must be the last.

If the written word is 'DA', then the next N–1 lines should consist of two integers a and b so that those numbers in the line (i+1) are the smallest and the biggest number in the group made in i-th gathering.

예제

예제 1

입력
2
2 1
출력
DA
1 2

예제 2

입력
6
1 4 2 5 3 6
출력
NE

예제 3

입력
6
6 3 2 1 4 5
출력
DA
1 2
1 3
4 5
1 5
1 6
코드를 제출하려면 로그인하세요.