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

题意:

一串珍珠,分为若干小段,每段至少存在两个相同种类的珍珠,求最多可以分多少段。

要求输入珍珠总数目n和n个珍珠的种类,输出最多可分的段数,和每段珍珠的起点和终点。

本题我们可用set容器储存珍珠串,每当输入珍珠与前串含有种类相同时,则记录首尾两点,总段数+1。

附AC代码:

 #include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std; const int MAX=; struct interval{//定义good区间
int l,r;
}a[MAX]; int n;
set<long long> q;//定义set容器 int main(){
while(~scanf("%d",&n)){
q.clear();//每次清空
long long t;
int sum=;
int left=;
for(int i=;i<=n;i++){
scanf("%d",&t);
if(q.count(t)){//count返回set内该元素个数,当含有时则标记区间,清空容器;没有则插入元素
a[sum].l=left;
a[sum].r=i;
sum++;
left=i+;
q.clear();
}
else
q.insert(t);
}
if(a[sum-].r<n)//注意最后一个区间的处理
a[sum-].r=n;
if(sum>){
printf("%d\n",sum);
for(int i=;i<sum;i++){
printf("%d %d\n",a[i].l,a[i].r);
}
}
else
printf("-1\n");
}
return ;
}

C. Pearls in a Row的更多相关文章

  1. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  2. CodeForces - 620C Pearls in a Row 贪心 STL

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

  3. Codeforces 620C EDU C.Pearls in a Row ( set + greed )

    C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...

  4. codeforces C. Pearls in a Row map的应用

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

  5. Educational Codeforces Round 6 C. Pearls in a Row set

    C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...

  6. 【32.26%】【codeforces 620C】Pearls in a Row

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CF620C Pearls in a Row

    CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...

  8. Codeforce C. Pearls in a Row

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

  9. CodeForces 620C Pearls in a Row

    水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...

随机推荐

  1. hdu5387(2015多校8)--Clock(模拟)

    题目链接:点击打开链接 题目大意:给出一个时间,问在钟表上这个时间的时候.时针和分针的角度,时针和秒针的角度.分针和秒针的角度.假设不是整数以分数的形式输出. 假设依照最小的格来算,那么: 1s对于秒 ...

  2. git mirror的创建与使用

    please donwload repo mirro as follow steps, thanks 1.mirror server,server IP:192.168.0.123 1.1 -- de ...

  3. EasyDarwin开源流媒体server将select改为epoll的方法

    本文来自EasyDarwin团队Fantasy(fantasy(at)easydarwin.org) 一. EasyDarwin网络模型介绍 EventContext负责监听全部网络读写事件.Even ...

  4. 三星note3 N900刷机包 4.4.2 ZSUDNE3 官方原汁原味 稳定流畅

    ROM介绍 此ROM基于最新的4.4.2 ZSUDNE3 制作,加入一些必要功能,其它性能基本与官方无差距,各方面感觉都非常不错了.此ROM本人自用,所以制作风格有点个人倾向.不论什么建议或者问题欢迎 ...

  5. activity fragment 转场动画

    http://www.cnblogs.com/avenwu/p/3372736.html v4 fragment fragmentTransaction.setCustomAnimations(R.a ...

  6. LeetCode(125)题解--Valid Palindrome

    https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...

  7. Navicat Premium创建MySQL存储过程

    1.使用Navicat Premium打开创建函数向导,操作:连接名——数据库——函数——新建函数 2.选择过程——输入存储过程参数——完成(这一步可以不填写参数,编写存储过程代码的时候设置参数) 3 ...

  8. SecureCRT中使用VBs脚本自动telnet登陆

    查看SecureCRT帮助文档: Help-> Help Topics->Scripting -> Script Objects Reference -> Session Ob ...

  9. 区分:WebElement, MobileElement, AndroidElement, and iosElement

    区分:WebElement, MobileElement, AndroidElement, and iosElement 原文地址:https://discuss.appium.io/t/differ ...

  10. Docker容器的数据卷(data volume),数据卷容器,数据卷的备份和还原。

    Docker容器的数据卷(data volume),数据卷容器,数据卷的备份和还原. 数据卷就是数据(一个文件或者文件夹). Docker的理念之一是将应用与其运行的环境打包,docker容器的生命周 ...