Special Tree

2.3

3 votes
Dynamic Programming, Algorithms, Trees, C++
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.

Problem statement

Given a set of N nodes, where each node has an integer A[i] associated with it. 

Construct a tree using N nodes such that the value of the given function Z is maximized.

Z=i=Ni=1[j=Nj=i+1[F(i,j)]], where F(i,j)=LIS(i,j)×LDS(i,j)

here LIS(i,j) represents length of longest increasing subsequence on simple path starting from node i to node and LDS(i,j) represents length of longest decreasing subsequence on simple path starting from node i to node j .

Input

  • First line contains an integer denoting number of nodes in the tree.
  • Next line contains N space separated integers denoting the value of each node i.e. A[i]

Output

  • Print N - 1 lines where each line contains
    • Two space separated integers a b denoting an edge between node a and b in the tree. 

Constraints

2N5001A[i]109

Verdit and Scoring

  • is the score for each test case.
  • If the output is not a tree or contains any invalid entry, then you will receive a Wrong Answer verdict.
Sample Input
3
4 1 9
Sample Output
1 2
2 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the given test case:

  • F(1,2) = LIS(1,2) * LDS(1,2) = 1 * 2 = 2
  • F(1,3) = LIS(1,3) * LDS(1,3) = 2 * 2 = 4
  • F(2,3) = LIS(2,3) * LDS(2,3) = 2 * 1 = 2
  • Z = 2 + 4 + 2 = 8

Hence, score is equal to 8.

Editor Image

?