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

input
5
1 2 3 4 1
output
1
1 5
 
题意:
  给你n个小珠子,每个珠子有不同的颜色,现在告诉你一段区间内有2个相同颜色的珠子那么这段区间可以独立成一段,现在问你最多能形成几段?
题解:
  set乱搞一通,出现了就截取,每个珠子都要属于最后的答案中一段里面,注意最后一段就好了
 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ; vector<pair<int,int > > ans;
map<int ,int > mp;
set<int > s;
int main() {
int x,n,l;
scanf("%d",&n);
l = ;
for(int i = ; i <= n; i++) {
scanf("%d",&x);
if(s.count(x)) {
ans.push_back(make_pair(l,i));
s.clear();mp.clear();
l = i+;
}
else {
mp[x] = i;
s.insert(x);
}
}
if(!ans.size()) puts("-1");
else {
printf("%d\n",ans.size());
if(ans[ans.size()-].second != n) ans[ans.size()-].second = n;
for(int i = ; i < ans.size(); i++) printf("%d %d\n",ans[i].first,ans[i].second);
} return ;
}

Educational Codeforces Round 6 C. Pearls in a Row set的更多相关文章

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

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

  2. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  6. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  8. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. Spork: Pig on Spark实现分析

    介绍 Spork是Pig on Spark的highly experimental版本号,依赖的版本号也比較久,如之前文章里所说.眼下我把Spork维护在自己的github上:flare-spork. ...

  2. unity3D游戏开发实战原创视频讲座系列9之塔防类游戏开发第一季

    解说文件夹 塔防游戏0基础篇... 第一讲  游戏演示和资源介绍... 第二讲  游戏场景的完毕... 第三讲  预制体的制作... 第四讲  敌人的随机产生和按路径行走... 第五讲  塔防工具的产 ...

  3. ROS-SLAM仿真-hector

    前言:hector_slam可以很好的在空中机器人,手持构图设备及特种机器人中运行. hector_slam不需要订阅里程计信息/odmo消息,而是直接使用激光估算里程计信息,因此,当机器人速度较快时 ...

  4. 编译安装FFmpeg 要支持xvid、x264、mp3、ogg、amr、faac

    编译安装FFmpeg 要支持xvid.x264.mp3.ogg.amr.faac libfaac    faac格式的编解码包libmp3lame    mp3格式编解码包libopencore-am ...

  5. hadoop配置历史服务器

    此文档不建议当教程,仅供参考 配置历史服务器 我是在hadoop1机器上配置的 配置mapred-site.xml <property> <name>mapreduce.job ...

  6. c#设计模式(1)

    本文摘取自吕震宇的博客园文章,版权归吕震宇仅供个人学习参考.转载请标明原作者吕震宇. 这学期开设设计模式课程,将课件放上来. 课本:<C#设计模式>,电子工业出版社,ISBN 7-5053 ...

  7. C# 特性(Attribute)

    C# 特性(Attribute) 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法.结构.枚举.组件等)的行为信息的声明性标签.您可以通过使用特性向程序添加声明性信息.一个声明 ...

  8. SpringMVC视频

    视频内容: 1.下载spring mvc以及spring mvc示例演示http://pan.baidu.com/s/1kTHRfDH 2.配置完善&初步探究控制器拦截http://pan.b ...

  9. 常用的Linux命令汇总

    1. 进入某个文件夹 2.查找某个文件或内容 3.查看文件内容 4.kill进程 启动tomcat  停止tomcat 1. 进入某个文件夹 比如有个目录,路径是:   /home/user1/doc ...

  10. 移动互联网iOS工程师必须知道的三点

    如果十年磨一剑,那么现在起作为一名iOS工程师,以下三点你必须要知道: 1.现在开始学swift正是时候,永远不要怕晚 因为…新时代的程序语言Swift有很多优势,长江后浪推前浪,Swift上手快,开 ...