CodeForces - 620C Pearls in a Row 贪心 STL
2 seconds
256 megabytes
standard input
standard output
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/printf instead 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
这题找规律,从第一个数开始向后搜索,当有一个数字重复时则记录该位置,再从下一个数字开始向后重新搜索。可使用STL中的set函数
AC代码
#include<stdio.h>
#include<set>
using namespace std;
int pos[300005];
set<int>a;
int main()
{
int n,m=0,b;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&b);
if(a.count(b)==0)//检查b是否出现过
{
a.insert(b);//在末尾插入b
}
else
{
pos[m]=i;
m++;
a.clear();//清除a中数据
} }
if(m==0)
{
printf("-1\n");
}
else
{
pos[m-1]=n;
printf("%d\n",m);
printf("1 %d\n",pos[0]);
for(int i=0;i<m-1;i++)
{
printf("%d %d\n",pos[i]+1,pos[i+1]);
}
}
return 0;
}
最近刚学的STL,本来想用循环来搜索重复数字的,结果超时,不过可作为思路
#include<stdio.h>
int a[300005],pos[300005];
int main()
{
int n,m=0;
pos[m]=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=pos[m]+1;j<i;j++)
{
if(a[j]==a[i])
{
m++;
pos[m]=i;
}
}
}
if(m==0)
{
printf("-1\n");
}
else
{
pos[m]=n;
printf("%d\n",m);
printf("1 %d\n",pos[1]);
for(int i=1;i<m;i++)
{
printf("%d %d\n",pos[i]+1,pos[i+1]);
}
}
return 0;
}
CodeForces - 620C Pearls in a Row 贪心 STL的更多相关文章
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 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 ...
- cf 620C Pearls in a Row(贪心)
d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...
- Codeforces Round #595 (Div. 3)D1D2 贪心 STL
一道用STL的贪心,正好可以用来学习使用STL库 题目大意:给出n条可以内含,相交,分离的线段,如果重叠条数超过k次则为坏点,n,k<2e5 所以我们贪心的想我们从左往右遍历,如果重合部分条数超 ...
- 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 ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
随机推荐
- CSS中的选择器(笔记)
1.通配符选择器(*):通配符选择器是用来选择所有元素,也可以选择某个元素下的所有元素.所有浏览器都支持通配符选择器. ;;} .dome *{padding: 2px;} 2.元素选择器(Ele): ...
- 【leetcode 简单】 第一百五十题 两个列表的最小索引总和
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...
- 【leetcode 简单】 第七十一题 二叉树的所有路径
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", ...
- Win7下VS2010不能链接问题
装了2012准备学VC++窗体开发,然后发现手边只有VS2010的教程,于是卸掉VS2012改装VS2010,结果发现不管写啥,链接时都报错“error Link1123 转到coff期间失败”. 于 ...
- Python标准库笔记(7) — copy模块
copy-对象拷贝模块:提供了浅拷贝和深拷贝复制对象的功能, 分别对应模块中的两个函数 copy() 和 deepcopy(). 1.浅拷贝(Shallow Copies) copy() 创建的 浅拷 ...
- 二、springboot配置
一.启动类 在包根目录下添加启动类,必须包含main方法,再添加Spring Boot启动方法: SpringApplication.run(SampleController.class, args) ...
- 04 Go 1.4 Release Notes
Go 1.4 Release Notes Introduction to Go 1.4 Changes to the language For-range loops Method calls on ...
- 说一下怎么搭建外网来访问SVN服务器
一.搭建SVN服务器 1.所需软件 TortoiseSVN,下载地址http://tortoisesvn.net/downloads.html TortoiseSVN中文语言包,下载地址http:// ...
- Python_oldboy_自动化运维之路(二)
本节内容: 1.pycharm工具的使用 2.进制运算 3.表达式if ...else语句 4.表达式for 循环 5.break and continue 6.表达式while 循环 1.pycha ...
- Python基础一(基本类型和运算符)
在说Python的基本类型钱我们先说下Python注释方式有哪几 Python注释 行注释 #行注释 行注释用以#开头,#右边的所有文字当做说明,而不是真正要执行的程序,起辅助说明作用 # 我是注释, ...