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

题意:给你一串数字,要你分段,要求每个段中至少有两个相同的数字,要你求最多能分多少段。
思路:贪心,从前面开始循环将出现的数字记录,然后遇到相同的就跳出,这就是一段,然后将前面记录的数字清空,重复前面的操作
这样符合最优原则,遇到有两个了相同的就分为一段。要注意最后一段。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<cstdio>
6 #include<climits>
7 #include<math.h>
8 #include<queue>
9 #include<string.h>
10 #include<stack>
11 #include<vector>
12 #include<map>
13 #include<ext/hash_map>
14 #define sc(x) scanf("%I64d",&x)
15 #define pr(x) printf("%I64d",x);
16 #define prr(x) printf("%I64d\n",x);
17 #define prrr(x) printf(" %I64d",x);
18 #define FOR(i,p,q) for(int i=p;i<=q;i++)
19 int cmp(const void*p,const void*q);
20 using namespace std;
21 typedef long long ll;
22 typedef struct pp
23 {
24 int x;
25 int y;
26 int flag;
27 } ss;
28 ss bb[3*100005];
29 ss aa[6*100005];
30 ss dd[3*100005];
31 map<int,int>my;//用map记录是否前面出现了某个数字
32 ss rk[3*100005];
33 int main(void)
34 {
35 int n,i,j,k,p,q;
36 while(scanf("%d",&k)!=EOF)
37 {my.clear();
38 for(i=0; i<k; i++)
39 {
40 scanf("%d",&bb[i].x);
41 bb[i].x;
42 bb[i].y=i;
43 }
44 qsort(bb,k,sizeof(ss),cmp);//这里的离散化可以不需要,直接映射就可以了。
45 int cnt=0;
46 dd[bb[0].y].x=cnt;
47 dd[bb[0].y].y=bb[0].y;
48 for(i=1; i<k; i++)
49 {
50 if(bb[i].x!=bb[i-1].x)
51 {
52 cnt++;
53 }
54 dd[bb[i].y].x=cnt;
55 dd[bb[i].y].y=bb[i].y;
56 }
57 int fr=0;
58 int sum=0;
59 for(i=0; i<k; i++)
60 {
61 if(my[dd[i].x]==0)
62 {
63 my[dd[i].x]=1;
64 }
65 else if(my[dd[i].x])
66 {
67 sum++;
68 rk[sum].x=fr;
69 rk[sum].y=i;
70 fr=i+1;
71 my.clear();//有一段后清空映射,因为段不能相交
72 }
73 }
74 if(sum==0)
75 {
76 printf("-1\n");
77 }
78 else
79 {if(rk[sum].y!=k-1)//最后一段要特别注意,如果没到端尾,要到端尾
80 {
81 rk[sum].y=k-1;
82 }
83 printf("%d\n",sum);
84 for(i=1; i<=sum; i++)
85 {
86 printf("%d %d\n",rk[i].x+1,rk[i].y+1);
87 }
88 }
89 }
90 return 0;
91 }
92 int cmp(const void*p,const void*q)
93 {
94 ss*n=(ss*)p;
95 ss*m=(ss*)q;
96 return n->x-m->x;
97 }

 

Codeforce C. Pearls in a Row的更多相关文章

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

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

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

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

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

  5. C. Pearls in a Row

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

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

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

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

  8. CF620C Pearls in a Row

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

  9. CodeForces 620C Pearls in a Row

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

随机推荐

  1. 《手把手教你》系列技巧篇(四十六)-java+ selenium自动化测试-web页面定位toast-下篇(详解教程)

    1.简介 终于经过宏哥的不懈努力,偶然发现了一个toast的web页面,所以直接就用这个页面来夯实一下,上一篇学过的知识-处理toast元素. 2.安居客 事先声明啊,宏哥没有收他们的广告费啊,纯粹是 ...

  2. UE4之Slate:App默认窗口的创建流程

    UE4版本:4.24.3源码编译 Windows10 + VS2019开发环境 在先前分享的基础上,现在来梳理下App启动时默认窗口的创建流程,以及相关的类.对象之间的抽象层级: 纯C++工程配置 S ...

  3. LetNet、Alex、VggNet分析及其pytorch实现

    简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...

  4. 日常Java 2021/10/30

    Java泛型 Java泛型(generics)是JDK5中引入的一个新特性,泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型.泛型的本质是参数化类型,也就是说所操作的数据类型 ...

  5. adjective

    形容词用来描述名词或代词:副词用来描述剩下的(动词.形容词.副词和整句).adverb: to word. Adjectives are used almost exclusively to modi ...

  6. day10 ajax的基本使用

    day10 ajax的基本使用 今日内容 字段参数之choices(重要) 多对多的三种创建方式 MTV与MVC理论 ajax语法结构(固定的) 请求参数contentType ajax如何传文件及j ...

  7. hadoop/spark面试题

    总结于网络 转自:https://www.cnblogs.com/jchubby/p/5449379.html 1.简答说一下hadoop的map-reduce编程模型 首先map task会从本地文 ...

  8. 对于Linq关键字和await,async异步关键字的扩展使用

    最近在看neuecc大佬写的一些库:https://neuecc.medium.com/,其中对await,async以及linq一些关键字实现了自定义化使用, 使其不需要引用对应命名空间,不需要多线 ...

  9. oracle 日期语言格式化

    TO_DATE ('17-JUN-87', 'dd-mm-yy', 'NLS_DATE_LANGUAGE = American')

  10. 使用Lock接口来解决线程安全的问题

    package cn.itcast.demo16.Demo09.Lock;import java.util.concurrent.locks.Lock;import java.util.concurr ...