620C.md (1908B)
1 Source: https://codeforces.com/problemset/problem/620/C 2 3 C. Pearls in a Row 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 There are *n* pearls in a row. Let's enumerate them with integers from 1 to *n* from the left to the right. The pearl number *i* has the type *a**i*. 22 23 Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type. 24 25 Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition. 26 27 As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. 28 29 Input 30 31 The first line contains integer *n* (1 ≤ *n* ≤ 3·105) — the number of pearls in a row. 32 33 The second line contains *n* integers *a**i* (1 ≤ *a**i* ≤ 109) – the type of the *i*-th pearl. 34 35 Output 36 37 On the first line print integer *k* — the maximal number of segments in a partition of the row. 38 39 Each of the next *k* lines should contain two integers *l**j*, *r**j* (1 ≤ *l**j* ≤ *r**j* ≤ *n*) — the number of the leftmost and the rightmost pearls in the *j*-th segment. 40 41 Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type. 42 43 If there are several optimal solutions print any of them. You can print the segments in any order. 44 45 If there are no correct partitions of the row print the number "-1". 46 47 Examples 48 49 Input 50 51 ``` 52 5 53 1 2 3 4 1 54 ``` 55 56 Output 57 58 ``` 59 1 60 1 5 61 ``` 62 63 Input 64 65 ``` 66 5 67 1 2 3 4 5 68 ``` 69 70 Output 71 72 ``` 73 -1 74 ``` 75 76 Input 77 78 ``` 79 7 80 1 2 1 3 1 2 1 81 ``` 82 83 Output 84 85 ``` 86 2 87 1 3 88 4 7 89 ```