cf Educational Codeforces Round 6 C Pearls in a Row

C. Pearls in a Row
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Sample test(s)
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
 
         
题意:将给定的序列划分成一段一段的。每一段要求包含2个相同的数字。求划分情况。
          
          
          
          
思路:很容易就可以想到要使用set。比较麻烦的是,最后可能出现
           
           
           
           
7
1 2 1 1 2 1 3
这样子的例子。
解决方法其实很简单 ,最后一个答案的右端直接改成n-1即可。
一直wa的原因是因为,自己没理解好题意,以为必须有且只有2个相同的数字。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
#include<map>

using namespace std;

#define N 10005

struct data
{
    int le,ri;
    data(){}
    data(int a,int b){le=a,ri=b;}
};
int a[400000];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    set<int >se;
    vector<data >ve;
    int st=0;
    int flag=0;
    int ff=0;
    for(int i=0;i<n;i++)
    {
        if(se.find(a[i])!=se.end())
            {
            se.clear();
            ve.push_back(data(st,i));
            st=i+1;

            }
        else
            {
                se.insert(a[i]);
            }
    }



     if(ve.size()==0)
    {
        puts("-1");
        return 0;;
    }
    ve[ve.size()-1].ri=n-1;

    printf("%d\n",ve.size());
    for(int i=0;i<ve.size();i++)
    {

        printf("%d %d\n",ve[i].le+1,ve[i].ri+1);

    }



    return 0;
}

你可能感兴趣的:(cf Educational Codeforces Round 6 C Pearls in a Row)