CF620C.Pearls in a Row

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

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/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

一行中有 nn 颗珍珠。我们从左到右依次用整数 11 到 nn 对它们编号。第 ii 颗珍珠的类型为 aia_i。

我们将连续的一段珍珠称为段。若一段中包含两颗类型相同的珍珠,则称该段为好段。

请将这行珍珠划分成尽可能多的好段。注意:每颗珍珠必须且仅能属于划分中的一个段。

由于输入/输出规模可能非常大,建议使用快速的输入/输出方法:例如,在 C++ 中优先使用 scanf/printf 而非 cin/cout;在 Java 中优先使用 BufferedReader/PrintWriter 而非 Scanner/System.out。

输入格式

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.

第一行包含一个整数 nn(1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5)—— 表示一排珍珠的数量。

第二行包含 nn 个整数 aia_i(1≤ai≤1091 \leq a_i \leq 10^9)—— 表示第 ii 颗珍珠的类型。

输出格式

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

第一行输出整数 kk —— 该珍珠序列划分中段的最大数量。

接下来的 kk 行,每行输出两个整数 lj, rjl_j,\,r_j(满足 1≤lj≤rj≤n1 \leq l_j \leq r_j \leq n),表示第 jj 段中最左侧与最右侧珍珠的编号。

注意:你应输出珍珠序列的一个合法划分,即每颗珍珠必须恰好属于一个段,且每个段内必须包含两颗类型相同的珍珠。

若存在多个最优解,输出任意一个即可;各段的输出顺序可以任意。

若不存在合法的划分,则输出数字“−1-1”。

输入输出样例

  • 输入#1

    5
    1 2 3 4 1

    输出#1

    1
    1 5
  • 输入#2

    5
    1 2 3 4 5

    输出#2

    -1
  • 输入#3

    7
    1 2 1 3 1 2 1

    输出#3

    2
    1 3
    4 7

输入解题思路,AI测评打分。不知道怎么写?

首页