zoukankan      html  css  js  c++  java
  • 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;  
    }  

  • 相关阅读:
    班服 状压DP NOIP模拟赛
    记录奥林比克/课程录制 洛谷P2255 [USACO14JAN]
    字符串的展开 vijos1379 NOIP2007 字符串 模拟
    树网的核 Vijos1362 NOIP2007 树结构 直径 暴搜
    浏览器(Web Navigation) codevs 5373 POJ 1028 简单模拟
    FPS集合 Codgic1351 动态规划 DP NOIP模拟赛
    基于VBA的Excel抽奖软件
    【CodeForces】Round #436
    【NOI OpenJudge】【1.3】编程基础之算术表达式与顺序执行
    【NOI OpenJudge】【1.2】编程基础之变量定义、赋值及转换
  • 原文地址:https://www.cnblogs.com/csushl/p/9386632.html
Copyright © 2011-2022 走看看