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 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
随机推荐
- [BinaryTree] 二叉树常考知识点
1.二叉树第i层至多有2^(i-1)个结点(i>=1). 2.深度为k的二叉树上,至多含2^k-1个结点(k>=1) 3.n0 = n2 + 1(度) 4.满二叉树:深度为k且含有2^k- ...
- delphi使用SQL的教程4(使用Params属性为参数赋值 )
17.4.1 使用Params属性为参数赋值 TQuery部件具有一个Params属性,它们在设计时不可用,在程序运行过程中可用,并且是动态建立的,当为TQuery部件编写动态SQL 语句时, D ...
- 【前端学习笔记】JavaScript 常用方法兼容性封装
获取样式函数封装 function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; } else{ r ...
- 集合里面的 E是泛型 暂且认为是object
集合里面的 E是泛型 暂且认为是object
- 管理与技术未必不可兼得,一个20年IT老兵的码农生涯
作者|康德胜 我是一个喜欢写代码但几乎不太有机会写代码的CTO,也是一个看得懂财务报表.通过所有CFA(金融特许分析师)考试并获得FRM(金融风险经理)认证的拿到金融MBA的CTO,如果我有幸被称作码 ...
- 求n!中因子k的个数
思路: 求n的阶乘某个因子k的个数,如果n比较小,可以直接算出来,但是如果n很大,此时n!超出了数据的表示范围,这种直接求的方法肯定行不通.其实n!可以表示成统一的方式. n!=(km)*(m!)*a ...
- java学习5-jar包的下载以及导入
1.出现未导入包的情况 ,表示当前jdk不 2.百度下载jar包 3.File. 未完待续http://blog.csdn.net/a153375250/article/details/5085104 ...
- NHibernate常见错误
Oracle 下必须用 Sequence [PrimaryKey(PrimaryKeyType.Sequence,"ID")] 1.提示 ORA-02289: 序列不存在 -- C ...
- Hive(二)hive的基本操作
一.DDL操作(定义操作) 1.创建表 (1)建表语法结构 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name[(col_name data_type ...
- [POI2008]MAF-Mafia
Description 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人也不同. ...