C. Pearls in a Row

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

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

题意:
给你一个序列,用相同的值作为首尾分割成子段,如样例三中的121,121. 求最多个数的子序列。
题解:简单的贪心,但由于题目给的数据范围是1e9因此,采用普通的数组办法不可取,故可用STL中的set容器解题。
 #include <stdio.h>
#include <set>
#include <algorithm>
#define MAXX 300010
using namespace std; int a[MAXX];
struct yuu
{
int l,r;
}par[MAXX]; int main()
{
int n; while(~scanf("%d", &n))
{
for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
} set <int> r;
int count=;
par[].l=;
par[].r=;
for(int i=; i<=n; i++)
{
if(r.find(a[i])!=r.end())
{
par[count].r=i;
count++;
par[count].l=par[count-].r+;
par[count].r=-;
r.clear();
}
else
{
r.insert(a[i]);
}
} if(par[count].r==- && count-)
{
count--;
par[count].r=n;
} if(par[count].r==)
printf("-1\n");
else
{
printf("%d\n",count);
for(int i=; i<=count; i++)
{
printf("%d %d\n",par[i].l, par[i].r);
}
}
}
}

 
 

Codeforces 620C EDU C.Pearls in a Row ( set + greed )的更多相关文章

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

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

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

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

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

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

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

  6. C. Pearls in a Row

    C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. CF620C Pearls in a Row

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

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

  9. Pearls in a Row CodeForces 620C 水题

    题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...

随机推荐

  1. c# dictionnary根据value查找对应的key

    属性方法中并没有包含此功能,因此需要自己自定义一个方法: string regionName = ""; if (ControlForm.swichLanguage.Contain ...

  2. ci上传图片

    o_upload.php <?php /** * Created by PhpStorm. * User: brady * Date: 2018/3/15 * Time: 14:10 */ cl ...

  3. 第136天:Web前端面试题总结(理论)

    Web前端面试题总结 HTML+CSS理论知识 1.讲讲输入完网址按下回车,到看到网页这个过程中发生了什么 a. 域名解析 b. 发起TCP的3次握手 c. 建立TCP连接后发起http请求 d. 服 ...

  4. HDU4810_Wall Painting

    题目很简单. 给你n个数,输出n个答案,第i个答案表示从n个数里取遍i个数的异或值的和. 其实每一个数最多也就32位,把所有的数分解,保存每一位总共有多少个1,最后要是这一位的异或结果为1,那么在所有 ...

  5. 两种方法实现TAB菜单及文件操作

    1,自定义属性的方法实现----TAB菜单操作 cursor:pointer; 鼠标的小手 <!DOCTYPE html> <html lang="en"> ...

  6. Mybatis笔记四:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'

    错误异常:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for pr ...

  7. 【BZOJ2878】【NOI2012】迷失游乐园(动态规划)

    [BZOJ2878][NOI2012]迷失游乐园(动态规划) 题面 BZOJ 题解 记得以前考试的时候做过这道题目 这题的暴力还是非常显然的,每次\(dfs\)一下就好了. 时间复杂度\(O(n^2) ...

  8. [杂谈]ACM启程

    此处省略一大段传奇的经历. 只需要知道的是,现在再次开始使用本博客的唯一原因就是——我进大学有搞ACM的打算. 其实本来是没有的,受到某学长的指引和推荐,我觉得这条路在当前确切是绝对的优选. 2年没碰 ...

  9. Emgu.CV.CvInvoke”的类型初始值设定项引发异常

    http://zhidao.baidu.com/link?url=VHkw3qZxp7HumQX_r-4ljPiy-N4A7yNK1Xn5q6tjPb16WvBGy6RFKrmKEhtgJ2PACAk ...

  10. JAVA导出Excel(支持多sheet)

    一.批量导出: /** * * @Title: expExcel * @Description: 批量导出客户信息 * @param @param params * @param @param req ...