codeforces C. Pearls in a Row map的应用
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/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
题意:将这些算分成一段一段的,每段最多包含两个相同的数字,最多有多少段,和怎么分的段;
思路:用map记录这个数字在当前这段出现的次数,用队列存当前这段的数字,当有一个数字出现第二次时,清空队列而且保存起点和终点位置;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[5*N];
struct node
{
int a,pos;
};
struct po
{
int le,ri;
};
po ans[5*N];
int main()
{
map<int,int>mp;
queue<node>qu;
node x;
int n;
int cnt=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(mp[a[i]]==0)
{
mp[a[i]]++;
x.a=a[i];
x.pos=i;
qu.push(x);
}
else
{
mp[a[i]]++;
x.a=a[i];
x.pos=i;
qu.push(x);
int l,r;
l=qu.front().pos;
while(!qu.empty())
{
x=qu.front();
mp[x.a]--;
qu.pop();
}
r=x.pos;
cnt++;
ans[cnt].le=l;
ans[cnt].ri=r;
}
}
if(!cnt)cout<<"-1"<<endl;
else
{
printf("%d\n",cnt);
for(int i=1;i<cnt;i++)
{
printf("%d %d\n",ans[i].le,ans[i].ri);
}
printf("%d %d\n",ans[cnt].le,n);
}
return 0;
}
codeforces C. Pearls in a Row map的应用的更多相关文章
- 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 ...
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
- 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 ...
- 【32.26%】【codeforces 620C】Pearls in a Row
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- GPS坐标(WGS84)转换百度坐标(BD09) python测试
基础知识坐标系说明: WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系. GCJ02:是由中国国家测绘局制订的地理信息系统的坐标系统.由WGS84坐标系经加密后的坐标 ...
- 前端之 JQuery
一.基本选择器 1.#id 概述: 根据给定的ID匹配一个元素. 使用任何的元字符(如 !"#$%&'()*+,./:;<=>?@[\]^`{|}~)作为名称的文本部分, ...
- 17 南宁区域赛 F - The Chosen One 【规律】
题目链接 https://nanti.jisuanke.com/t/19972 题意 给出一个n 然后将 n 个数 标号为 1 -> n 按顺序排列 每次抽掉 奇数位的数 然后求最后剩下那个数字 ...
- SpringBoot编辑代码时不重启服务
@SpringBootApplication @ComponentScan("com.sicdt") public class SicSignWebApplication { pu ...
- Laravel 学习笔记之 Composer 自动加载
说明:本文主要以Laravel的容器类Container为例做简单说明Composer的自动加载机制. Composer的自动加载机制 1.初始化一个composer项目 在一个空目录下compose ...
- Python编程-多线程
一.python并发编程之多线程 1.threading模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 1.1 开启线程的 ...
- SOA 面向服务架构 阅读笔记(二)
SOA并不能保证企业的员工更加轻松,企业的收益更加客观. 6.软件组件 6.1 组件和组件的作用 通过可重用的软件代码-组件,可以构建灵活的软件. 6.2 软件组件又称为应用程序,程序,函数,模 ...
- Eclipse开发快捷键精选
1.alt+?或alt+/:自动补全代码或者提示代码2.ctrl+o:快速outline视图3.ctrl+shift+r:打开资源列表4.ctrl+shift+f:格式化代码5.ctrl+e:快速转换 ...
- java深入探究10-文件上传组件FileUpload,邮件开发
1.文件上传组件FileUpload 1)java提供了文件上传的工具包 需要引入:commons-fileupload-1.2.1.jar(文件上床组件核心包) commons-oi-1.4(封装了 ...
- linux jdk安装。
我使用的centos 7: 安转sun公司的jdk要先检查系统中是否安装jdk,一般来说Centos系统会默认会安装OpenJDK,但是openJDK部分内容 与SUN JDK不兼容,因此下面进行重新 ...