Under construction

Still being built. Things may change

Can't Stop (Large)

Time limit30sMemory limit512 MB

Summary
Find the longest run of consecutive roll sets where every set contains at least one of k chosen numbers.
Level

Medium7 of 10

Topics
Sliding window, Backtracking
Solved
No attempts yet

Problem

This problem takes its idea from Can't Stop, a board game designed by Sid Sackson. You do not need to have played Can't Stop to solve it.

You are playing a game on a very large board. The game gives you NN roll sets in order. Each roll set consists of DD die rolls, and every die roll is an integer.

To win the game you have to find the longest totally awesome interval of the sequence. An interval is any run of consecutive roll sets. An interval is called totally awesome if you can pick kk numbers such that every roll set in the interval contains at least one of them.

For example, suppose D=2D=2, k=3k=3, and the roll sets are these.

SetRolls
010 20
150 60
270 30
340 40
430 30
520 40

The interval from set 0 to set 2 is totally awesome, because all three sets contain 10, 50 or 70. The interval from set 1 to set 5 is also totally awesome, because all five sets contain 50, 30 or 40. That interval holds 5 roll sets, and it is the longest totally awesome interval.

Output the indices of the first and the last roll set of the longest totally awesome interval. If several totally awesome intervals have that length, pick the one with the lowest first index. The first roll set has index 0.

Input

The first line of the input gives the number of test cases TT. TT test cases follow. The first line of each test case holds three space separated integers NN, DD and kk. The next line holds N×DN \times D integers. The first DD of them are the rolls of the first roll set, the next DD are the rolls of the second roll set, and so on.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤D≤41 \le D \le 4
  • Every die roll is an integer between 11 and 10510^5.
  • For 6 test cases, 1≤N≤1051 \le N \le 10^5. For all the other test cases, 1≤N≤1031 \le N \le 10^3.
  • 2≤k≤32 \le k \le 3

Output

For each test case, output one line containing "Case #x: y z", where x is the test case number starting from 1, and y and z are the first and the last index of the longest totally awesome interval. If several intervals have that length, output the one with the lowest first index.

Note

The board game Can't Stop was designed by Sid Sackson and has been published by many publishers. Neither Mr. Sackson nor any of the publishers endorses this problem or has any involvement with it.

Examples4

  1. Example 1

    Input
    4
    8 1 2
    1 2 3 2 4 5 4 6
    4 3 2
    1 2 3 4 5 6 7 8 9 10 11 12
    6 2 3
    10 20 50 60 70 30 40 40 30 30 20 40
    10 1 3
    2 4 3 1 4 5 3 1 1 2
    
    Expected output
    Case #1: 1 3
    Case #2: 0 1
    Case #3: 1 5
    Case #4: 1 4
    
  2. Example 2

    Input
    3
    1 1 2
    5
    1 4 3
    100000 1 100000 1
    2 1 2
    7 7
    
    Expected output
    Case #1: 0 0
    Case #2: 0 0
    Case #3: 0 1
    
  3. Example 3

    Input
    2
    4 1 2
    1 2 1 3
    6 1 2
    1 2 1 2 1 3
    
    Expected output
    Case #1: 0 2
    Case #2: 0 4
    
  4. Example 4

    Input
    2
    8 1 2
    7 1 2 1 5 3 4 3
    7 2 3
    1 2 3 4 5 6 7 8 9 10 11 12 13 14
    
    Expected output
    Case #1: 1 3
    Case #2: 0 2