CodeForces
来源:互联网 发布:大排畸哪些数据看男女 编辑:程序博客网 时间:2024/06/03 15:44
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/printf instead 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 ai (1 ≤ ai ≤ 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 lj, rj (1 ≤ lj ≤ rj ≤ 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”.
Sample Input
5
1 2 3 4 1
5
1 2 3 4 5
7
1 2 1 3 1 2 1
Sample Output
1
1 5
-1
2
1 3
4 7
Hint
题意
把一段序列分成连续的段 要求每段里面有重复的数
求满足条件最多的段
题解:
考虑从第一位开始往后查找 只要遇到与前面出现过的 就把它的位置记录下来
下个段从这一位的下一位开始记录 用set来实现
AC代码
#include <cstdio>#include <iostream>#include <map>#include <set>#include <cstring>#include <algorithm>using namespace std;int p[300005];int main(){ int k; scanf("%d",&k); set<int > s; int x; int l = 0; for (int i = 1; i <= k; ++i){ scanf("%d",&x); if (s.count(x)){ p[l++] = i; s.clear(); } else s.insert(x); } if (l == 0) printf("-1\n"); else { p[l-1] = k; printf("%d\n",l); printf("1 %d\n",p[0]); for (int i = 1; i < l; ++i){ printf("%d %d\n",p[i-1]+1,p[i]); } } return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- 状态模式
- linux常用命令一(文件命令类)
- 翻译:AVPlayerItemOutput二
- (DP,背包,数量不限,装满)Piggy-Bank -- HDOJ
- 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
- CodeForces
- Game (子串问题 模板)
- AlarmManager闹钟管理器
- 管理文件输入输出
- 1009 数字1的数量
- 在控制器的Action中,如何直接使用JsonResult方法中传递到前台的对象
- lowbit
- Java中栈和队列
- springMVC从上传的Excel文件中读取数据