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. MOS管驱动详解

    1.常用的几种电平转换方案 2.三极管的电平转换及驱动电路分析 3.三级管老怀 4.关于MOSFET管驱动电路总结 5.一个IIC的5V和3.3V电平转换的经典电路分享 6.mos 7.mos应用 8 ...

  2. POJ 1753 (枚举+DFS)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40632   Accepted: 17647 Descr ...

  3. python3短信接口使用

    import http.client from urllib import parse host = "106.ihuyi.com" sms_send_uri = "/w ...

  4. .Net Core表单定义

    创建表单 <form asp-controller="Account" asp-action="Login" asp-route-returnurl=&q ...

  5. C#实现网段扫描

    目录1.使用的类2.获取本地主机IP地址3.远程查询4.实现网段的扫描 ---------------------------------------------------------------- ...

  6. 自己定义ActionBar标题与菜单中的文字样式

    自己定义标题文字样式 标题样式是ActionBar样式的一部分,所以要先定义ActionBar的样式 <style name="AppTheme" parent=" ...

  7. OpenCV 入门示例之二:播放 AVI 视频

    前言 本文展示一个播放 AVI 视频的程序.( 呵呵是 AVI 视频不是 AV 视频噢! ) 代码示例 // 此头文件包含图像IO函数的声明 #include "highgui.h" ...

  8. CSS常识

    1.给一个div设置边框:border:1px #CCCCCC bold; 2.给DOM加小手:cursor:pointer; 取消小手:cursor:auto;

  9. 用Cocoapods集成XMPPFramework 遇 Module 'KissXML' not found 问题

    用Coacopods集成XMPPFramework完成后Command + B,报Module 'KissXML' not found 一般来说,通过Coacopods集成集成第三方框架,不会再有依赖 ...

  10. This means that only a small number of nodes must be read from disk to retrieve an item.

    http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html Introduction A B-tree is a specialized ...