CodeChef: Codechef
Site under maintenance
  • Register
  • Forgot Password?
  • PRACTICE
    • Easy
    • Medium
    • Hard
  • COMPETE
    • August Mini Challenge
    • August Algorithm Challenge
    • July Algorithm Challenge
    • June Algorithm Challenge
    • May Gamers Challenge
  • DISCUSS
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef TechTalks
    • CodeChef Meetups
    • Campus Chapters
    • Host Your Contest
    • User Groups
  • HELP
    • Frequently Asked Questions
    • FAQ for Problem Setters
    • Tutorial: Paying Up
    • Tutorial: Small Factorials
    • Tutorial: Input and Output(I/O)
    • Tutorial: Your First Non-Trivial Problem
  • ABOUT
    • About CodeChef
    • About Directi
    • CEO's Corner
    • Press Room
    • Careers

Home » Problems (hard) » Find a Subsequence

Find a Subsequence

Problem code: FINDSEQ

  • Submit
  • All submissions

All submissions for this problem are available.

Given an array A of N integers A[0], A[1], ..., A[N-1] and a string B which is a permutation of "12345".
You have to find a subsequence of 5 elements from A having distinct values such that their relative order is same as B.
Meaning that, if (i0, i1, i2, i3, i4) are the indexes of such a subsequence (0 <= i0 < i1 < i2 < i3 < i4 < N) then:

  • A[i0] is the B[0]-th smallest element among the 5 numbers.
  • A[i1] is the B[1]-th smallest element among the 5 numbers.
  • A[i2] is the B[2]-th smallest element among the 5 numbers.
  • A[i3] is the B[3]-th smallest element among the 5 numbers.
  • A[i4] is the B[4]-th smallest element among the 5 numbers.
  • Input

    The first line of input contains T(<=60) which is the number of tests cases. The first line of each test case will be an integer N (5<=N<=1000) and a string B containing a permutation of "12345". Next line will contain N integers A[0], A[1], ..., A[N-1]. Each of them will be between -109 and +109 (inclusive).

    Output

    For each test case output five space separated integers in ascending order which are the indexes (i0, i1, i2, i3, i4) of a five length subsequence described before. You may output any solution. If there is no solution just output "-1" without quotes.

    Example

    Input:
    2
    7 32145
    6 17 5 3 13 8 10
    7 12345
    10 20 30 40 40 20 10
    
    Output:
    0 2 3 5 6
    -1
    


    Explanation of 1st sample: {0, 2, 3, 5, 6} is a valid solution because the values of those indexes {6, 5, 3, 8, 10} will have ranks {3, 2, 1, 4, 5} after sorting. But {0, 2, 3, 4, 5} is not a solution as the values {6, 5, 3, 13, 8} will have ranks {3, 2, 1, 5, 4} after sorting.
    Explanation of 2nd sample: There is no five length subsequence in the array that will have ranks {1, 2, 3, 4, 5} after sorting.


    Date:2011-12-07
    Time limit:9s-14s
    Source limit:50000B
    Languages:All


    • Submit

    Comments

    Loading Comments...

    SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

    Loading Submissions...

    RECENT ACTIVITY FOR THIS PROBLEM:

    Loading Recent Activity...

    HELP

    Program should read from standard input and write to standard output.

    After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

    • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
    • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
    • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
    • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
    • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

    If you are still having problems, see a sample solution here.

    • About CodeChef
    • About Directi
    • CEO's Corner
    • Careers
    • feedback@codechef.com

    © 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs

    Sponsors
    The time now is: