avatar
fireworks99
keep hungry keep foolish

Codeforces C.Pearls in a Row

Description

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 ai.

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.

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.

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row.

The second line contains n integers a**i (1 ≤ a**i ≤ 109) – the type of the i-th pearl.

Output

On the first line print integer k — the maximal number of segments in a partition of the row.

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.

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.

If there are several optimal solutions print any of them. You can print the segments in any order.

If there are no correct partitions of the row print the number “-1”.

Examples

input

5

1 2 3 4 1

output

1

1 5

input

5

1 2 3 4 5

output

-1

input

7

1 2 1 3 1 2 1

output

2

1 3

4 7

题意

要把一个数字序列分成几个序列(每个序列均含有相同元素),尽可能多地分(也就是让每个序列只含有一组相同元素)。找不到输出 -1 ,否则输出序列个数与起止点

思路

可以利用 set 自带除重的特点,将序列中的数字向set里放,每放一次检测一次大小,大了很正常,没变大说明出现了同样的元素,这就有了一个序列

坑点(末尾处理)

如果原序列是

7

1 2 1 3 4 3 6

那么第一个符合要求的序列是 1 2 1,第二个是3 4 3 6,而不是3 4 3

#include <set>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

struct node
{
    int left, right;
}ans[1000005];

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        bool flag = 0;
        set<int> st;
        int tem = st.size();
        int tot = 0;
        ans[tot].left = 1;
        for(int i = 1; i <= n; ++i)
        {
            int a;
            scanf("%d", &a);
            st.insert(a);
            if(st.size() > tem)
                tem = st.size();
            else
            {
                flag = 1;
                st.clear();
                ans[tot].right = i;
                tot++;
                ans[tot].left = i + 1;
                tem = st.size();
            }
        }
        if(!flag)
            cout << "-1" << '\n';
        else
        {
            cout << tot << '\n';
            for(int i = 0; i < tot - 1; ++i)
                cout << ans[i].left << ' ' << ans[i].right << '\n';
            if(ans[tot - 1].right != n)
                cout << ans[tot - 1].left << ' ' << n << '\n';
        }
    }
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy