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. 在deepin系统中制作桌面快捷方式

    在使用deepin-wine 安装一些软件的时候,每次启动都需要到.deepinwine目录下运行deepin-wine xx.exe.笔者在安装过HeidiSql之后,一直苦于这种情况.比较好的解决 ...

  2. leetcode-打家劫舍(动态规划)

    你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每 ...

  3. Python爬虫模拟登录带验证码网站

    问题分析: 1.爬取网站时经常会遇到需要登录的问题,这是就需要用到模拟登录的相关方法.python提供了强大的url库,想做到这个并不难.这里以登录学校教务系统为例,做一个简单的例子. 2.首先得明白 ...

  4. 同台服务器 部署多个tomcat 需要做的修改

    需要修改以下加粗部分: 1:访问端口 8080->8081 2:shutdown 端口 8005->8015 3: AJP端口 8001->8010 <?xml version ...

  5. spark-submit配置说明

    <Spark 官方文档>Spark配置 spark-1.6.0 原文地址 Spark配置 Spark有以下三种方式修改配置: Spark properties (Spark属性)可以控制绝 ...

  6. Python入门(3)

    一.列表 列表是用来储存和处理多个数据的数据类型,我们可以像下面这样来创建一个列表: my_list = [1, 2, 3] 列表和数学中的集合很像,但是,列表中的数据是可以重复,并且他们是有序的,列 ...

  7. python SyntaxError: Non-ASCII character '\xe8' in file C:\Users\nwpujun\PycharmProjects\projects\hrl1\hrlAgent\src\li_nn.py on line 52

    解决方法:在文件头部加上这样的一句话 # -*- coding: utf-8 -*- 注意:加在其他的位置可能没用,我就是这样的

  8. c#积累之测试

    初来上班,免不了看别人代码.快速搞懂别人代码是我现在受到的一大挑战.寻摸着规律,发现一边进行调试,一边进行行行注释的逻辑判断不失为一种妙招. c#调试用的是vs2012.f11键和f10和f5键的应用 ...

  9. BluetoothClass详解

    一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...

  10. II 3.1 连接到服务器

    II 3.1 连接到服务器 package socket; import java.io.IOException; import java.io.InputStream; import java.ne ...