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 ...
随机推荐
- 集成富文本编辑器XSS预防过滤措施
# https://github.com/phith0n/python-xss-filter import re import copy from html.parser import HTMLPar ...
- 常用MS-SQL写法整理
这里整理日常会用到的一些写法,一些常规的group by,系统函数等用法不在这里做记录了,大家有什么好的写法也可以分享下 1 sql操作xml内容(sp_xml_preparedocument和ope ...
- STL之map、set灵活使用
1.LA 5908/UVA1517 Tracking RFIDs 题意:给出s个传感器的位置,以及其感应范围.如果某个方向上有墙,则该方向上感应距离减1.现在有w个墙,给出p个物品的位置,问其能被几个 ...
- iframe与父窗口之间数据互相获取
Js/Jquery获取iframe中的元素 博客分类: jquery javascript jquery 在web开发中,经常会用到iframe,难免会碰到需要在父窗口中使用iframe中的元素.或 ...
- mysql 触发器 存储过程 java调用
触发器和存储过程是为了提高SQL的运行效率. SQL语句先编译.后执行,而触发器与存储过程都会提前预编译完成,且只编译一次,供反复调用. 随着时代的进步,硬件与带宽的提升,触发器和存储过程提升效率并不 ...
- MySQL数据文件介绍及存放位置
怎样查看MySql数据库物理文件存放位置? 使用命令行查找: show global variables like '%datadir%'; 我查找的位置:C:\ProgramData\MySQL\M ...
- ETL应用:一种一次获取一个平台接口文件的方法
ETL应用场景中,若对端接口文件未能提供,任务会处于循环等待,直到对端提供为止,该方法极大的消耗了系统资源.为此想到了一种方法,一次获取一个平台的文件,实现思路如下: 1.第一次获取对端平台提供目录下 ...
- python的常用的内置函数
使用内置函数的好处:简单,快速. 1.zip():以多个序列为参数,返回元祖列表. 长度:在多个序列长度不一时,以最短的为准. 常见用途:构建多参数列表,构建字典. 2.map():在python2旧 ...
- Python的return self和return一个新的对象区别
目的:设计一个有理数相加.如3/5 + 7/15 = 80/75 return self 输入: class Rational0: def __init__(self, num, den=1): se ...
- Harbor 镜像管理专家
Harbor是一个企业级的镜像管理仓库,是VMware主导的一个开源项目(github地址:https://github.com/vmware/harbor). Harbor提供了以下功能特性: Cl ...