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. javascript在html直接传值

    function getUrlParam(url, name) { var pattern = new RegExp("[?&]" + name + "\=([^ ...

  2. struts2学习之基础笔记7

    第十二章 Struts 2的标记库 1 OGNL简介 Object-Grephic Navigtor Language 图对象导航语言 作用:图对象导航语言是Struts 2标记库中为其相应标记属性进 ...

  3. Core Java(七)

    面向对象特性整理 知识点:一. static修饰符 static修饰符可以用来修饰类的成员变量.成员方法和代码块.            . 用static修饰的成员变量表示静态变量,可以直接通过类名 ...

  4. 谈谈javascript中原型继承

    什么是继承?拿来主义:自己没有,别人有,把别人的拿过来使用或者让其成为自己的 如何实现继承的方式 原型继承 混入继承 经典继承 1. 混入继承 由于一个对象可以继承自任意的对象,即:o可以继承自对象o ...

  5. Error:Execution failed for task ':app:processDebugManifest'. 合并冲突

    1. Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : Attrib ...

  6. 七牛上图片总是net::ERR_NAME_NOT_RESOLVED

    七牛上图片总是net::ERR_NAME_NOT_RESOLVED >> php这个答案描述的挺清楚的:http://www.goodpm.net/postreply/php/101000 ...

  7. paratest

    class Program { static void Main(string[] args) { long result = 0; Stopwatch Watch = new Stopwatch() ...

  8. Core Graphics框架 利用Quartz 2D绘图

    首先,什么是Core Graphics和Quartz 2D? Core Graphics:是基于Quartz 2D绘图引擎的一个C语言的API绘图框架.它也是iOS开发中最基本的框架(Framewor ...

  9. Python 进行网络编程

    Date: 2019-06-10 Author: Sun 1. Python TCP通信实现 socket()函数 Python 中,我们用 socket()函数来创建套接字,语法格式如下: sock ...

  10. luoguP4512 【模板】多项式除法 NTT+多项式求逆+多项式除法

    Code: #include<bits/stdc++.h> #define maxn 300000 #define ll long long #define MOD 998244353 # ...