• Codeforce-620C


    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

    题目大意:给出一个整数序列,定义一个子串中包含两个相同的数字就是 goodsegments,问这个整数序列最多可以分成几个 good segments,输出分割的个数,和每一次分割的左右端点。

    题解:我们可以利用set<>容器的特性来处理。开始set为空往里面存数据如果出现重复则这一组数为 goodsegments。ps:若最后有剩余应该并入最后一组。


    AC代码为:

    #include<iostream>  
    #include<set>
    using namespace std;  
    const int N=1e6+5;  
    struct node  
    {  
        int left,right;  
    }que[N];
      
    int main()  
    {  
       int n,k,x;  
      
         scanf("%d",&n);   
        k=0;  
      
        int i=1;  
        set<int>Q;  
        set<int>::iterator it;  
      
        while(i<=n)  
        {  
            que[k].left=i;  
            while(i<=n)  
            {  
                scanf("%d",&x);  
                i++;  
                it=Q.find(x);  
                if(it!=Q.end())  
                {  
                    Q.clear();  
                que[k++].right=i-1;  
                    break;  
                }  
                else  
                {  
                    Q.insert(x);  
                }  
      
            }  
        }  
        if(k==0)  
            printf("-1 ");  
        else  
            {  
            printf("%d ",k);  
                for(int i=0;i<k-1;i++)  
                {  
                    printf("%d %d ",que[i].left,que[i].right);  
                }  
                printf("%d %d ",que[k-1].left,n);  
            }  
           
        return 0;  
    }  

  • 相关阅读:
    表、栈和队列基础
    学习记录随机算法
    202202linux基础知识点
    js去重的几种方法
    Gjc_02转WGS84坐标系(高精度)
    拖拽地图/点坐标定位(高德地图)
    利用canvas进行的图片压缩
    uniapp学习笔记(创建调试打包各个端口版本)
    uniapp页面栈
    svg路径
  • 原文地址:https://www.cnblogs.com/csushl/p/9386632.html
Copyright © 2020-2023  润新知