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

Examples
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

这题找规律,从第一个数开始向后搜索,当有一个数字重复时则记录该位置,再从下一个数字开始向后重新搜索。可使用STL中的set函数

AC代码

#include<stdio.h>
#include<set>
using namespace std;
int pos[300005];
set<int>a;
int main()
{
int n,m=0,b;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&b);
if(a.count(b)==0)//检查b是否出现过
{
a.insert(b);//在末尾插入b
}
else
{
pos[m]=i;
m++;
a.clear();//清除a中数据
} }
if(m==0)
{
printf("-1\n");
}
else
{
pos[m-1]=n;
printf("%d\n",m);
printf("1 %d\n",pos[0]);
for(int i=0;i<m-1;i++)
{
printf("%d %d\n",pos[i]+1,pos[i+1]);
}
}
return 0;
}

最近刚学的STL,本来想用循环来搜索重复数字的,结果超时,不过可作为思路

#include<stdio.h>
int a[300005],pos[300005];
int main()
{
int n,m=0;
pos[m]=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=pos[m]+1;j<i;j++)
{
if(a[j]==a[i])
{
m++;
pos[m]=i;
}
}
}
if(m==0)
{
printf("-1\n");
}
else
{
pos[m]=n;
printf("%d\n",m);
printf("1 %d\n",pos[1]);
for(int i=1;i<m;i++)
{
printf("%d %d\n",pos[i]+1,pos[i+1]);
}
}
return 0;
}

CodeForces - 620C Pearls in a Row 贪心 STL的更多相关文章

  1. CodeForces 620C Pearls in a Row

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

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

  3. cf 620C Pearls in a Row(贪心)

    d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...

  4. Codeforces Round #595 (Div. 3)D1D2 贪心 STL

    一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...

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

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

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

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

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

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

  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. js封装Cookie操作

    var CookieUtil = { // 设置cookie set : function (name, value, expires, domain, path, secure) { var coo ...

  2. SpringCloud微服务简介(一)

    Spring Cloud简单认识 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及部署,并通过 ...

  3. codeforces997C Sky full of stars

    传送门:http://codeforces.com/problemset/problem/997/C [题解] 注意在把$i=0$或$j=0$分开考虑的时候,3上面的指数应该是$n(n-j)+j$ 至 ...

  4. 【转】WPF的知识

    [-] 闲话WPF之二XAML概述 闲话WPF之五XAML中的类型转换 闲话WPF之十六WPF中的资源 2 闲话WPF之十九WPF中的传递事件 1 闲话WPF之二十WPF中的传递事件 2 闲话WPF之 ...

  5. Eclipse配置C++环境

    由于实在不想用(界面太丑,超级强迫症),前段时间JAVA一直用eclipse,感觉这个IDE非常友好,看上去很舒服,下载的时候发现有C++版本,于是折腾了一会儿,谷歌上发现好多教程,但是大部分比较老, ...

  6. python概念-Socket到底有多骚

    Socket究竟是什么呢? 简单来说Socket就是用来完成客户端与服务器之间的通信 例如浏览器访问网页,例如网络游戏等一切基于客户端服务器来实现的C/S架构程序 Socket是基于互联网OSI七层协 ...

  7. UNIX环境高级编程 第3章 文件I/O

    前面两章说明了UNIX系统体系和标准及其实现,本章具体讨论UNIX系统I/O实现,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write. ...

  8. C++ Primer 5th 第14章 重载运算与类型转换

    当运算符作用域类类型的对象时,可以通过运算符重载来重新定义该运算符的含义.重载运算符的意义在于我们和用户能够更简洁的书写和更方便的使用代码. 基本概念 重载的运算符是具有特殊名字的函数:函数名由关键词 ...

  9. js数组排序 reverse()和sort()方法的使用

    WEB前端|js数组排序reverse()和sort()方法的使用,数组中已经存在两个可以直接用来重排序的方法:reverse()和sort(). reverse()方法会对反转数组项的顺序. var ...

  10. python 面试题2

    问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...