C. Pearls in a Row
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 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".
Examples
input
51 2 3 4 1
output
11 5
input
51 2 3 4 5
output
-1
input
71 2 1 3 1 2 1
output
21 34 7题意:给你一个序列,用相同的值作为首尾分割成子段,如样例三中的121,121. 求最多个数的子序列。题解:简单的贪心,但由于题目给的数据范围是1e9因此,采用普通的数组办法不可取,故可用STL中的set容器解题。
1 #include <stdio.h> 2 #include <set> 3 #include <algorithm> 4 #define MAXX 300010 5 using namespace std; 6 7 int a[MAXX]; 8 struct yuu 9 { 10 int l,r; 11 }par[MAXX]; 12 13 int main() 14 { 15 int n; 16 17 while(~scanf("%d", &n)) 18 { 19 for(int i=1; i<=n; i++) 20 { 21 scanf("%d", &a[i]); 22 } 23 24 set <int> r; 25 int count=1; 26 par[1].l=1; 27 par[0].r=0; 28 for(int i=1; i<=n; i++) 29 { 30 if(r.find(a[i])!=r.end()) 31 { 32 par[count].r=i; 33 count++; 34 par[count].l=par[count-1].r+1; 35 par[count].r=-1; 36 r.clear(); 37 } 38 else 39 { 40 r.insert(a[i]); 41 } 42 } 43 44 if(par[count].r==-1 && count-1) 45 { 46 count--; 47 par[count].r=n; 48 } 49 50 51 if(par[count].r==0) 52 printf("-1\n"); 53 else 54 { 55 printf("%d\n",count); 56 for(int i=1; i<=count; i++) 57 { 58 printf("%d %d\n",par[i].l, par[i].r); 59 } 60 } 61 } 62 }