A10237 | Pearls in a Row
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains integer $n$ ( $1<=n<=3·10^{5}$ ) — the number of pearls in a row.
The second line contains $n$ integers $a_{i}$ ( $1<=a_{i}<=10^{9}$ ) – the type of the $i$ -th pearl.
The second line contains $n$ integers $a_{i}$ ( $1<=a_{i}<=10^{9}$ ) – the type of the $i$ -th pearl.
输出格式
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".
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".
输入输出样例
输入 #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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted