Pearls in a Row CodeForces - 620C
2017-05-18 21:37
507 查看
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”.
Example
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
(贪心)用 set 判断一下这个数是否有过,有就清空set,然后再继续判断,
注意特判下最后一段
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”.
Example
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
(贪心)用 set 判断一下这个数是否有过,有就清空set,然后再继续判断,
注意特判下最后一段
#include<stdio.h> #include<set> using namespace std; int n; int a[300010]; typedef struct node{ int x,y; }Node; Node p[151000]; int main() { set<int>s; s.clear(); //set<int>::iterator ite; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); int x=0; int y; int cnt=1; p[0].x=-1; p[0].y=-1; for(int i=0;i<n;i++) { if(s.find(a[i]) == s.end()) s.insert(a[i]); else { p[cnt].x=p[cnt-1].y+1; p[cnt].y=i; cnt++; s.clear(); } } if(s.size() != 0) p[cnt-1].y=n-1; if(cnt==1) printf("-1\n"); else { printf("%d\n",cnt-1); for(int i=1;i<cnt;i++) printf("%d %d\n",p[i].x+1,p[i].y+1); } return 0; }
相关文章推荐
- Educational Codeforces Round 6 620C Pearls in a Row(stl)
- Educational Codeforces Round 6-C. Pearls in a Row(贪心)
- 【Educational Codeforces Round 6C】【DP or 贪心】Pearls in a Row n个数分最多区间使得每个区间都有重复数
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- cf Educational Codeforces Round 6 C Pearls in a Row
- Codeforces - Educational Codeforces Round 6C - Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- CodeForces 620C Pearls in a Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- Codeforce C. Pearls in a Row
- Educational Codeforces Round 6 C. Pearls in a Row
- codeforces Trees in a Row
- codeforces Round # 236(DIV 2)B. Trees in a Row
- Codeforces #349 Div2 C Reberland Linguistics(twice in a row)
- Educational Codeforces Round 25 B. Five-In-a-Row
- codeforces B. Trees in a Row
- G - Five-In-a-Row CodeForces - 825B
- CodeForces - 825B Five-In-a-Row