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\n");  

    else  

        {  

        printf("%d\n",k);  

            for(int i=0;i<k-1;i++)  

            {  

                printf("%d %d\n",que[i].left,que[i].right);  

            }  

            printf("%d %d\n",que[k-1].left,n);  

        }  

       

    return 0;  

}

Codeforce-620C的更多相关文章

  1. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  2. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  3. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  4. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  5. codeforce 367dev2_c dp

    codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...

  6. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  7. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  8. 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D

    [树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...

  9. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...

  10. Two progressions CodeForce 125D 思维题

    An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...

随机推荐

  1. Comparable接口的实现和使用

    1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它的自然比较方法 .实现此接口的对象列表(和数组)可 ...

  2. shell的运用 : jenkins 编译 打包前端发布 生产(tomcat)

    生产隔离做得非常.....文件上传只能通过固定ip机器的sftp账户上传,账户密码每个月要写申请才能获得. 登陆生产服务只能通过浏览器登陆!!! 发布一次生产,很痛苦. 做了简单的shell来减轻痛苦 ...

  3. oracle基础(基本介绍)

    数据库 磁盘上存储的数据的集合 在物理上表现为数据文件.日志文件和控制文件等 在逻辑上以表空间形式存在 必须首先创建数据库,然后才能使用Oracle 数据库实例 每个启动的数据库都对应一个数据库实例, ...

  4. [ASP.NET Core 3框架揭秘] 文件系统[1]:抽象的“文件系统”

    ASP.NET Core应用 具有很多读取文件的场景,比如配置文件.静态Web资源文件(比如CSS.JavaScript和图片文件等)以及MVC应用的View文件,甚至是直接编译到程序集中的内嵌资源文 ...

  5. PHP代码安全有必要了解下

    攻击者通过构造恶意SQL命令发送到数据库,如果程序未对用户输入的 SQL命令执行判断过滤,那么生成的SQL语句可能会绕过安全性检查,插入其他用于修改后端数据库的语句,并可能执行系统命令,从而对系统造成 ...

  6. [java笔记] 最近学的一些笔记

    1.@Override的用法 2.父类的返回值类型的范围,与子类返回值类型的返回的大小关系: 3.子类方法的权限修饰符,与子类方法的权限修饰符: 4.如果p1是一个对象,p2也是个对象,那么代码p1= ...

  7. Linux运维利器之ClusterShell

    一.简介 实验室机房有大概百台的服务器需要管理,加上需要搭建Hadoop以及Spark集群等,因此,一个轻量级的集群管理软件就显得非常有必要了.经过一段时间的了解以及尝试,最终选择了clustersh ...

  8. __FILE__ basename() 作用

    __FILE__  basename() 作用 __FILE__ 获取当前文件或文件夹的绝对路径 basename(__FILE__) 获取当前文件或文件夹的名称 basename(__FILE__, ...

  9. python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务

    python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...

  10. Excel导入数据库(php版)

    一.环境说明 Apache+php(PHPExcel)+HTML5+JavaScript(jQuery)+MySQL 二.前端预览 三.Excel表格 四.HTML部分 <p>按照Exce ...