Codeforces 620C EDU C.Pearls in a Row ( set + greed )
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.
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.
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".
5
1 2 3 4 1
1
1 5
5
1 2 3 4 5
-1
7
1 2 1 3 1 2 1
2
1 3
4 7
题意:给你一个序列,用相同的值作为首尾分割成子段,如样例三中的121,121. 求最多个数的子序列。
题解:简单的贪心,但由于题目给的数据范围是1e9因此,采用普通的数组办法不可取,故可用STL中的set容器解题。
#include <stdio.h>
#include <set>
#include <algorithm>
#define MAXX 300010
using namespace std; int a[MAXX];
struct yuu
{
int l,r;
}par[MAXX]; int main()
{
int n; while(~scanf("%d", &n))
{
for(int i=; i<=n; i++)
{
scanf("%d", &a[i]);
} set <int> r;
int count=;
par[].l=;
par[].r=;
for(int i=; i<=n; i++)
{
if(r.find(a[i])!=r.end())
{
par[count].r=i;
count++;
par[count].l=par[count-].r+;
par[count].r=-;
r.clear();
}
else
{
r.insert(a[i]);
}
} if(par[count].r==- && count-)
{
count--;
par[count].r=n;
} if(par[count].r==)
printf("-1\n");
else
{
printf("%d\n",count);
for(int i=; i<=count; i++)
{
printf("%d %d\n",par[i].l, par[i].r);
}
}
}
}
Codeforces 620C EDU C.Pearls in a Row ( set + greed )的更多相关文章
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- 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 ...
- 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 ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- C. Pearls in a Row
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- 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 ...
- Pearls in a Row CodeForces 620C 水题
题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
随机推荐
- ubuntu安装和查看已安装软件
说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...
- “我爱淘”第二冲刺阶段Scrum站立会议4
完成任务: 完成了首页中的推荐功能,推荐的是最近添加的需要卖的书,注册功能实现了它,可以对数据库进行添加. 计划任务: 在客户端实现分类功能,通过学院的分类查看书籍. 遇到问题: 分类功能,根据不同学 ...
- 浅析GCC下C++多重继承 & 虚拟继承的对象内存布局
继承是C++作为OOD程序设计语言的三大特征(封装,继承,多态)之一,单一非多态继承是比较好理解的,本文主要讲解GCC环境下的多重继承和虚拟继承的对象内存布局. 一.多重继承 先看几个类的定义: 01 ...
- 阿里云服务器内部dns可能出错
今天部署一个阿里云服务器,所有配置项都改好了,就是连接不上本机. 反复查找,防火墙端口和网卡接口都配置对了,selinux也关闭了,但就是连接不上阿里云内网的ip. 由于连接是本机,把ip填写为127 ...
- ThinkPHP的调用css,js和图片的路径
按网上的说法,在根目录下建了一个Public目录,把css,js和图片放到Public目录下,然后用__PUBLIC__/...或__ROOT__/Public/...调用.但是发现无论如何改路径都无 ...
- webgl学习笔记二-绘图多点
写在前面 建议先看下第一篇webgl学习笔记一-绘图单点 第一篇文章,介绍了如何用webgl绘图一个点.接下来本文介绍的是如何绘制多个点.形成一个面. webgl提供了一种很方便的机制,即缓冲区对象, ...
- 第124天:移动web端-Bootstrap轮播图插件使用
Bootstrap JS插件使用 > 对于Bootstrap的JS插件,我们只需要将文档实例中的代码粘到我们自己的代码中> 然后作出相应的样式调整 Bootstrap中轮播图插件叫作Car ...
- QoS专题-第2期-QoS实现工具之MQC
QoS实现工具之MQC QoS技术可以对网络中报文进行分类处理,根据优先级提供不同的差分服务,如何实现这种差分服务呢?我们有一种强大的配置方法-模块化QoS命令行MQC(Modular QoS Com ...
- [cogs1065]绿豆蛙的归宿
1065. [Nescafe19] 绿豆蛙的归宿 [题目描述] 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路, ...
- 【刷题】洛谷 P3796 【模板】AC自动机(加强版)
题目描述 有 \(N\) 个由小写字母组成的模式串以及一个文本串 \(T\) .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串 \(T\) 中出现的次数最多. 输入输出格式 输入格式 ...