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

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 处理 注意最后一段的右区间一定为n 2000ms 超时代码
#include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
int l;
int r;
}m[300005];
int a[300005];
int main()
{
scanf("%d",&n);
int st=0;
int jishu=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=st+1;j<i;j++)
{
if(a[j]==a[i])
{
m[jishu].l=st+1;
m[jishu].r=i;
jishu++;
st=i;
}
}
}
if(jishu==0)
printf("-1\n");
else
{
printf("%d\n",jishu);
for(int i=0;i<jishu-1;i++)
{
printf("%d %d\n",m[i].l,m[i].r);
}
printf("%d %d\n",m[jishu-1].l,n);
}
return 0;
}

  

set 处理  218ms

#include<bits/stdc++.h>
using namespace std;
struct node
{
int l;
int r;
}m[300005];
int exm;
set<int>s;
set<int>::iterator it;
int n;
int main()
{
scanf("%d",&n);
int jishu=0;
for(int i=1;i<=n;)
{
m[jishu].l=i;
while(i<=n)
{
scanf("%d",&exm);
i++;
it=s.find(exm);
if(it!=s.end())
{
s.clear();
m[jishu].r=i-1;
jishu++;
break;
}
else
s.insert(exm);
}
}
if(jishu==0)
printf("-1\n");
else
{
printf("%d\n",jishu);
for(int i=0;i<jishu-1;i++)
{
printf("%d %d\n",m[i].l,m[i].r);
}
printf("%d %d\n",m[jishu-1].l,n);
}
return 0;
}

  

Educational Codeforces Round 6 C的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

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

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

  7. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  8. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. GIT: 分布式开发 代码管理工具使用命令大全

    代码管理工具: GIT     什么是GIT? Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目 Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常 ...

  2. yii的学习笔记 基本结构 自用

    Yii 学习笔记 W:YII是什么? Q:Yii 是一个基于组件的高性能 PHP 框架,用于快速开发大型 Web 应用.它使Web开发中的 可复用度最大化,可以显著提高你的Web应用开发速度.Yii ...

  3. SSH:远程登陆

    SSH用于计算机之间的加密登录的前提是公钥为真,所以存在中间人攻击中间人攻击:与https协议不同,SSH协议的公钥是没有CA公证的,当对公钥的请求被中间截获时,中间人可以发出伪造公钥干坏事而不被识破 ...

  4. vs_code 快捷键

    一般的Ctrl+Shift+P,F1显示命令面板按Ctrl+P快速打开,到文件.Ctrl + Shift + N新窗口/实例Ctrl + Shift + W /关闭窗口实例Ctrl +.用户设置Ctr ...

  5. 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11

    Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...

  6. Python3 Tkinter-Toplevel

    1.创建 Toplevel与Frame类似,但是它包含窗体属性(如Title) from tkinter import * root=Tk() tl=Toplevel() Label(tl,text= ...

  7. 150命令之线上查询及帮助命令 man hellp

    150命令之线上查询及帮助命令 man 查询命令的帮助 man + 命令 NAME        ls - list directory contents 命令+命令简单说明   SYNOPSIS   ...

  8. Python中的print

    Python 3.X的print 在Python 3.X中,print是一个内置函数,完整的声明形式如下: print([object, ...][, sep=' '][, end='\n'][, f ...

  9. Block的声明与定义语法

    Block的声明 Block的声明与函数指针的声明类似 返回值类型(^变量名)(参数列表) Block的定义 ^返回值类型(参数列表) { 表达式 } 其中: 1 如果返回值类型是void,可以省略 ...

  10. JSR303中的来验证数据信息

    spring mvc之实现简单的用户管理三 博客分类: spring spring mvc spring mvc dispatcherServlet springspring mvcbean vali ...