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. 【springmvc+mybatis项目实战】杰信商贸-5.生产厂家DAO+SERVICE+CONTROLLER+JSP+配置文件

    上一篇我们创建了工程和一个Factory的po对象(javaBean),我们也写好了Mapper的映射文件,接下来我们来完成生产厂家的DAO与SERVICE,以及CONTROLLER,还有做显示的JS ...

  2. 一键部署pxe环境

    系统:Centos6.5 环境:VMware Workstation12 #!/bin/bash # Please prepare CentOS ISO image first # root pass ...

  3. 上楼梯问题(递归C++)

    [问题描述] 小明上楼梯,一次可以迈1步,2步和3步,假设楼梯共有n个台阶,输出他所有的走法. [代码展示] #include<iostream>using namespace std;i ...

  4. Linux 添加虚拟网卡

    使用的Linux版本是Centos 7: [root@vnode33 bin]# cat /etc/redhat-release CentOS Linux release (Core) 使用ifcon ...

  5. centos配置iptables

    第一次配置前消除默认的规则 #这个一定要先做,不然清空后可能会悲剧 iptables -P INPUT ACCEPT #清空默认所有规则 iptables -F #清空自定义的所有规则 iptable ...

  6. js中的数组对象排序

    一.普通数组排序 js中用方法sort()为数组排序.sort()方法有一个可选参数,是用来确定元素顺序的函数.如果这个参数被省略,那么数组中的元素将按照ASCII字符顺序进行排序.如: var ar ...

  7. Java学习个人备忘录之文档注释

    文档注释 单行注释用 // 多行注释有两种,第一种是 /* 内容 */,第二种是/** 内容 */. 这两种多行注释的区别是/** 内容 */这种注释可以生成一个该文件的注释文档,下面是演示代码. A ...

  8. 【Docker 命令】- create命令

    docker create :创建一个新的容器但不启动它 语法 docker create [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS同run命令 实例 使用 ...

  9. 刚装的vs无法运行正确的程序

  10. 在ios 上 按钮 disabled 样式显示异常

    将input,button或textarea设置为disabled后,在iphone手机上样式将被覆写-webkit-appearance:none; 文字的颜色还是灰色. 原本在android 上 ...