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 ...
随机推荐
- openresty安装文档
一.OpenResty简介 OpenResty是一个基于 Nginx与 Lua的高性能 Web平台,其内部集成了大量精良的 Lua 库.第三方模块以及大多数的依赖项.用于方便地搭建能够处理超高并 ...
- mysql终结篇
一.mysql中not null unique和primary key 的区别 1.not null unique 是给一个字段设置非空且唯一的特性,当表中字段没有设置primary key的主键特性 ...
- Django~1
一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有 ...
- Funq之Lambda表达式2
Last month I started a series of posts covering some of the new VB and C# language features that are ...
- 每天一个Linux命令(61)killall命令
killall命令用进程的名字来杀死进程. (1)用法: 用法: killall [ -egiqvw ] [ -signal ] [进程名称] 格式:killall -< ...
- 每天一个Linux命令(49)traceroute命令
traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes. (1)用法: 用法: traceroute [参数] [主机] (2)功能: ...
- Linux Shell编程 awk命令
概述 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是l ...
- jquery图片过滤归类应用
在线演示 本地下载
- mysql下merge分表
1.merge简介分表就是把N条记录的表,分成若干个分表,各个分表记录的总和仍为N. 分表的方法有很多,用merge来分表,是最简单的一种方式.merge是mysql的一种存储引擎,它把一组MyISA ...
- linux下的cacti安装(centos7)
1 cacti运行环境准备 cacti需要php+apache+mysql+snmp+RRDTool,以及cacti本身.cacti本体是用php开发的网站,通过snmp对远端设备信息进行采集.apa ...