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

题意:将这些算分成一段一段的,每段最多包含两个相同的数字,最多有多少段,和怎么分的段;

思路:用map记录这个数字在当前这段出现的次数,用队列存当前这段的数字,当有一个数字出现第二次时,清空队列而且保存起点和终点位置;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[5*N];
struct node
{
int a,pos;
};
struct po
{
int le,ri;
};
po ans[5*N];
int main()
{
map<int,int>mp;
queue<node>qu;
node x;
int n;
int cnt=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(mp[a[i]]==0)
{
mp[a[i]]++;
x.a=a[i];
x.pos=i;
qu.push(x);
}
else
{
mp[a[i]]++;
x.a=a[i];
x.pos=i;
qu.push(x);
int l,r;
l=qu.front().pos;
while(!qu.empty())
{
x=qu.front();
mp[x.a]--;
qu.pop();
}
r=x.pos;
cnt++;
ans[cnt].le=l;
ans[cnt].ri=r;
}
}
if(!cnt)cout<<"-1"<<endl;
else
{
printf("%d\n",cnt);
for(int i=1;i<cnt;i++)
{
printf("%d %d\n",ans[i].le,ans[i].ri);
}
printf("%d %d\n",ans[cnt].le,n);
}
return 0;
}

codeforces C. Pearls in a Row map的应用的更多相关文章

  1. 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 ...

  2. CodeForces 620C Pearls in a Row

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

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

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

  4. 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 ...

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

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

  6. 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 ...

  7. 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 ...

  8. 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. CF620C Pearls in a Row

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

随机推荐

  1. GPS USB驱动串口被占用

    1.一般是装了错误的驱动,显示如下 2.实际装好应该是显示的 3.驱动选择,先卸载了上面的virtual驱动,安装下面箭头指向的驱动 这里的卸载很重要,先点设备管理器的--查看--显示隐藏设备, 然后 ...

  2. django的模板系统过滤器笔记

    -------------------django内建的过滤器-------------------1.add 使用形式为:{{ value | add: "2"}}意义:将val ...

  3. 使用TortoiseGit查看以前Commit的各个快照(snapshot)

    Swith/Checkout提供了这个功能. 比如从bcbc66627334204f879eff99f68e70af0ca7907e回退到dc3f82f2532fcb95e4f24c9f9c331a7 ...

  4. 01 Spring框架 基本介绍

    相信学习java,并且走Web道路的道友都应该知道Spring的大名,它的地位相信也不需要我在这里多说什么,接下来的文章就Spring的配置和使用来进行一些讲解. 首先学习框架我们都要考虑和做到以下几 ...

  5. 反射,hashlib模块,正则匹配,冒泡,选择,插入排序

    一.反射(自省) 首先通过一个例子来看一下本文中可能用到的对象和相关概念. import sys # 模块,sys指向这个模块对象import inspectdef foo(): pass # 函数, ...

  6. PAT 天梯赛 L1-031. 到底是不是太胖了 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-031 AC代码 #include <iostream> #include <cstdio&g ...

  7. 每天一个Linux命令(56)yum命令

          用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级.     (1)用法:     用法:  yum  [参数] [软件名]     (2)功能:     功能:  yum ...

  8. 键盘没有Home键和End键的完美解决办法

    最近新入手一个笔记本,发现键盘没有Home/End,这两个键虽然不是必用,但也是用顺手了,特别是选择一行,到行首,行尾的时候甚是方便 作为一枚程序员,怎么能够妥协? 于是开始研究 方案一 通过观察笔记 ...

  9. 【收藏】SearchCrawler By James Holmes

    转自Crawling the Web with Java By James Holmes 无需任何扩展包,可直接运行. import java.awt.*; import java.awt.event ...

  10. 释放Linux系统缓存

    清理Linux缓存使用下面的命令 sync; echo 3 > /proc/sys/vm/drop_caches 需求与原理 下面介绍buffer与cache的差别: A buffer is s ...