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

51 2 3 4 1

output

11 5

input

51 2 3 4 5

output

-1

input

71 2 1 3 1 2 1

output

21 34 7题意:给你一个序列,用相同的值作为首尾分割成子段,如样例三中的121,121. 求最多个数的子序列。题解:简单的贪心,但由于题目给的数据范围是1e9因此,采用普通的数组办法不可取,故可用STL中的set容器解题。
 1 #include <stdio.h>
 2 #include <set>
 3 #include <algorithm>
 4 #define MAXX 300010
 5 using namespace std;
 6
 7 int a[MAXX];
 8 struct yuu
 9 {
10     int l,r;
11 }par[MAXX];
12
13 int main()
14 {
15     int n;
16
17     while(~scanf("%d", &n))
18     {
19         for(int i=1; i<=n; i++)
20         {
21             scanf("%d", &a[i]);
22         }
23
24         set <int> r;
25         int count=1;
26         par[1].l=1;
27         par[0].r=0;
28         for(int i=1; i<=n; i++)
29         {
30             if(r.find(a[i])!=r.end())
31             {
32                 par[count].r=i;
33                 count++;
34                 par[count].l=par[count-1].r+1;
35                 par[count].r=-1;
36                 r.clear();
37             }
38             else
39             {
40                 r.insert(a[i]);
41             }
42         }
43
44         if(par[count].r==-1 && count-1)
45         {
46             count--;
47             par[count].r=n;
48         }
49
50
51         if(par[count].r==0)
52             printf("-1\n");
53         else
54         {
55             printf("%d\n",count);
56             for(int i=1; i<=count; i++)
57             {
58                 printf("%d %d\n",par[i].l, par[i].r);
59             }
60         }
61     }
62 }

 
 
时间: 2024-10-28 10:52:35

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

Pearls in a Row CodeForces 620C 水题

题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意:就是给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对儿相等的数字,每个数字都属于某个部分,输出划分的部分数量以及对应区间. 很简单一道题,输入的数据都不用存的,输入一个检测一个就好了,用map打标记,用容器存一下要输出的区间端点值,最后挨个儿输出就好了 代码如下: #include<iostream> #include<map> #

cf 620C Pearls in a Row(贪心)

d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3,第二段是4~7. 即分成这2段:1 2 1,3 1 2 1 s.很不错的一道贪心的题.当时没怎么细想,后来看了tourist的代码后得知. 可以证明,满足贪心选择性质和最优子结构性质. 贪心策略是:从前向后遍历,每次选择最小长度的符合条件的段. c. #include<iostream> #in

codeforces 620C

题意:给你n个数,一段子序列拥有两个相同的数就称为happy segment,求最多的happy segment,没有的话就输出-1,否则第一行输出happy segment的个数a,接下来a行输出每个happy segment的起始位置和终止位置. 思路:用stl的set做,只需记录每个happy segment的终止位置就好了, 1 #include<iostream> 2 #include<set> 3 const int qq=3e5+10; 4 using namespa

Educational Codeforces Round 6

620A - Professor GukiZ's Robot    20171122 \(ans=max(\left | x2-x1 \right |,\left | y2-y1 \right |)\) #include<stdlib.h> #include<stdio.h> #include<math.h> #include<cstring> #include<iostream> #include<algorithm> using

Codeforces Round #484 (Div. 2) A. Row

A. Row time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You're given a row with nn chairs. We call a seating of people "maximal" if the two following conditions hold: There are no neigh

CodeForces - 402B Trees in a Row (暴力)

题意:给定n个数,要求修改其中最少的数,使得这n个数满足ai + 1 - ai = k. 分析: 暴力,1000*1000. 1.这n个数,就是一个首项为a1,公差为k的等差数列.k已知,如果确定了a1,就能确定整个数列. 2.1 ≤ ai ≤ 1000,因此,可以从1~1000中枚举a1,将形成的数列与给定的数列比较,统计两数列对应下标中不同数字的个数. 3.不同数字的个数最少的那个数列就是最终要修改成的数列,然后输出对应下标的那个数的变化值即可. #include<cstdio> #inc

CodeForces 347A Difference Row (水题)

题意:给定 n 个数,让你找出一个排列满足每个数相邻作差之和最大,并且要求字典序最小. 析:这个表达式很简单,就是把重新组合一下,就成了x1-xn,那么很简单,x1是最大的,xn是最小的,中间排序就好. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <cstring> using namespace std;

CodeForces 510 A. Fox And Snake(模拟啊 )

题目链接:http://codeforces.com/problemset/problem/510/A Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead. A snake is a pattern on a n by m

CodeForces 1B. Spreadsheets(模拟)

题目链接:http://codeforces.com/problemset/problem/1/B B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard input output standard output In the popular spreadsheets systems (for example, in Excel) the following