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

Sample test(s)
input
  1. 5
    1 2 3 4 1
output
  1. 1
    1 5
input
  1. 5
    1 2 3 4 5
output
  1. -1
input
  1. 7
    1 2 1 3 1 2 1
output
  1. 2
    1 3
    4 7

题意:将这些算分成一段一段的,每段最多包含两个相同的数字,最多有多少段,和怎么分的段;

思路:用map记录这个数字在当前这段出现的次数,用队列存当前这段的数字,当有一个数字出现第二次时,清空队列而且保存起点和终点位置;

AC代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e5+5;
  4. int a[5*N];
  5. struct node
  6. {
  7. int a,pos;
  8. };
  9. struct po
  10. {
  11. int le,ri;
  12. };
  13. po ans[5*N];
  14. int main()
  15. {
  16. map<int,int>mp;
  17. queue<node>qu;
  18. node x;
  19. int n;
  20. int cnt=0;
  21. scanf("%d",&n);
  22. for(int i=1;i<=n;i++)
  23. {
  24. scanf("%d",&a[i]);
  25. }
  26. for(int i=1;i<=n;i++)
  27. {
  28. if(mp[a[i]]==0)
  29. {
  30. mp[a[i]]++;
  31. x.a=a[i];
  32. x.pos=i;
  33. qu.push(x);
  34. }
  35. else
  36. {
  37. mp[a[i]]++;
  38. x.a=a[i];
  39. x.pos=i;
  40. qu.push(x);
  41. int l,r;
  42. l=qu.front().pos;
  43. while(!qu.empty())
  44. {
  45. x=qu.front();
  46. mp[x.a]--;
  47. qu.pop();
  48. }
  49. r=x.pos;
  50. cnt++;
  51. ans[cnt].le=l;
  52. ans[cnt].ri=r;
  53. }
  54. }
  55. if(!cnt)cout<<"-1"<<endl;
  56. else
  57. {
  58. printf("%d\n",cnt);
  59. for(int i=1;i<cnt;i++)
  60. {
  61. printf("%d %d\n",ans[i].le,ans[i].ri);
  62. }
  63. printf("%d %d\n",ans[cnt].le,n);
  64. }
  65. return 0;
  66. }

codeforces C. Pearls in a Row map的应用的更多相关文章

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

  2. CodeForces 620C Pearls in a Row

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

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

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

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

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

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

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

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

  8. 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. CF620C Pearls in a Row

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

随机推荐

  1. 返回泛型集合的SqlDBHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Entity; ...

  2. highcharts基本介绍

    转自:http://www.cnblogs.com/jyh317/p/4189773.html 一.highcharts简介 Highcharts是一款纯javascript编写的图表库,能够很简单便 ...

  3. OJ 1101 谁是中间的那个

    前言:主要考察排序用法 sort(cow+1,cow+1+n,cmp);//数组按cmp方法排序 Description 一天,农夫乔伊像往常一样来到了他的牧场,他突然对他的奶牛产奶量产生了兴趣.他想 ...

  4. HibernateQL

    查询语言---QL(Query Language)   NativeSQL-------功能最强大 HQL--Hibernate QL EJB QL (JP QL)---HQL的一个子集 QBC--- ...

  5. R和Python小数的保留

    R: 1.保留几位有效数字: signif(x,digits) 2.保留几位小数: round(x,digits) Python: 1.“%.2f”%a

  6. 数据结构与算法分析:C语言描述(原书第2版 简体中文版!!!) PDF+源代码+习题答案

    转自:http://www.linuxidc.com/Linux/2014-04/99735.htm 数据结构与算法分析:C语言描述(原书第2版中文版!!!) PDF+源代码+习题答案 数据结构与算法 ...

  7. char,uchar,0xff

    如果:char test = 0xFF: 此时:test != 0xFF://因为test为char类型,0xFF为int,所以编译器会将test转为int(-1),所以不等于 如果:uchar te ...

  8. INSPIRED启示录 读书笔记 - 第12章 产品探索

    软件项目可以划分为两个阶段 探索产品阶段:弄清楚要开发什么产品(定义正确的产品) 在探索产品的阶段,产品经理负责分析各种创意,广泛收集用户需求,了解如何运用新技术,拿出产品原型并加以测试 从全局视角思 ...

  9. java-Object类中的方法

    1.Object类中有哪些方法? protected Object  clone():创建一个返回此对象的副本 boolean  equals(Obejct obj):只是其他对象与此对象是否相等 p ...

  10. python统计代码行数

    以前写了一个java的统计代码行数的小程序,最近在看python,于是就参考前辈的代码,写了一个统计文件夹下面各种程序的代码的小程序,这里贴出来供大家参考 参考链接: https://gist.git ...