CF620C Pearls in a Row

洛谷评测传送门

题目描述

There are nn pearls in a row. Let's enumerate them with integers from 11 to nn from the left to the right. The pearl number ii has the type a_{i}a**i .

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 nn ( 1<=n<=3·10^{5}1<=n<=3⋅105 ) — the number of pearls in a row.

The second line contains nn integers a_{i}a**i ( 1<=a_{i}<=10^{9}1<=a**i<=109 ) – the type of the ii -th pearl.

输出格式

On the first line print integer kk — the maximal number of segments in a partition of the row.

Each of the next kk lines should contain two integers l_{j},r_{j}l**j,r**j ( 1<=l_{j}<=r_{j}<=n1<=l**j<=r**j<=n ) — the number of the leftmost and the rightmost pearls in the jj -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".

题意翻译

现在有N个数,你的任务是将这N个数尽可能切割成多段。每一段必须包括两个相同的数。

数据输入量比较大,建议使用scanf和printf。

Input 单组测试数据。数据第一行为N(1 ≤ N ≤ 3·105) 。

数据的第二行包括N个数ai(1 ≤ ai ≤ 109) 。

Output 输出的第一行为尽可能切割的最大段数K。

接下来K行,每行为两个整数lj, rj (1 ≤ lj ≤ rj ≤ n) ,表示切割的区间范围

如果存在多个合法的切割方法,输出任意一个即可。

如果不能切割成合法的情况,输出"-1".

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

题解:

2019.11.11光棍节模拟赛T1 100分场可海星

这是我自从参加\(JDFZ\,\,CSP-S\)模拟赛之后第一次秒切T1的场!!炒鸡鸡冻!

我觉得思路真的特别好想:

一边输入一边标记出现的数,如果碰到已经打上标记的数就清空标记数组,\(++cnt\)并且开一个\(last\)变量存区间分割点。

但是\(bool\)数组开\(1e9\)就会炸...

咋办呢?

我们发现\(STL\)给我们提供了一个特别好用的容器\(set\),专门用来维护元素重复的集合。

里面提供了一个特别好用的函数\(find()\),能返回等于元素的迭代器。如果返回的迭代器不等于\(s.end()\),说明有这个元素,就可以加进答案里。

在\(CF\)交的时候还出现了一种始料未及的情况,就是这种做法可能会出现不覆盖满的情况,所以退出来的时候要暴力地把\(ans[cnt].r\)变成\(n\)。

完整代码如下:

#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
const int maxn=3*1e5+10;
int n,last,cnt;
int a[maxn];
struct node
{
int l,r;
}ans[maxn];
set<int> s;
set<int>::iterator it;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
last=1;
for(int i=1;i<=n;i++)
{
if((it=s.find(a[i]))!=s.end())
{
ans[++cnt].l=last,ans[cnt].r=i;
s.clear();
last=i+1;
}
else
s.insert(a[i]);
}
if(!cnt)
{
printf("-1");
return 0;
}
ans[cnt].r=n;
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
printf("%d %d\n",ans[i].l,ans[i].r);
return 0;
}

CF620C Pearls in a Row的更多相关文章

  1. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. C. Pearls in a Row

    C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. 【32.26%】【codeforces 620C】Pearls in a Row

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 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 ...

  9. CodeForces 620C Pearls in a Row

    水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...

随机推荐

  1. python网络编程:UDP方式传输数据

    UDP --- 用户数据报协议(User Datagram Protocol),是一个无连接的简单的面向数据报的运输层协议. UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能 ...

  2. Java每日一面(Part1:计算机网络)[19/11/13]

    作者:晨钟暮鼓c个人微信公众号:程序猿的月光宝盒 1. HTTP相关[1] 1.1 HTTP简介 ​ HTTP协议,即超文本传输协议,属于应用层的协议,他是基于请求和响应模式的无状态的 应用层协议. ...

  3. spark shell操作

    RDD有两种类型的操作 ,分别是Transformation(返回一个新的RDD)和Action(返回values). 1.Transformation:根据已有RDD创建新的RDD数据集build ...

  4. js数组对象过滤——filter,find,some,every

    1.filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素.     原数组不变     不会对空数组进行检测 let arr1 = [1,2,3,4] let ...

  5. QT在linux下获取网络类型

    开发中遇到这样一个需求,需要判断当前网络的类型(wifi或者4G或者网线),在这里给大家一块分享下: 1.这里有一个linux指令:nmcli(大家自行百度即可) 2.nmcli device sta ...

  6. 8 个 Tips 让你更好的进行 Code Review

    摘要: Code Review 可以提高代码质量. 原文:Elevenbeans 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 原文地址:https://kellysutton.co ...

  7. R-6 线性回归模型流程

    本节内容: 0:小知识 1:新数据要如何进行分析 2:第二步骤:理解数据 3:第三步骤:相关分析 4:特殊点 0:小知识 0.1:我们说对分析一个数据一般是分步骤的:那么我们可以对其中的步骤进行打标签 ...

  8. 【西北师大-2108Java】第十二次作业成绩汇总

    [西北师大-2108Java]第十二次作业成绩汇总 作业题目 面向对象程序设计(JAVA) 第14周学习指导及要求 实验目的与要求 (1)掌握GUI布局管理器用法: (2)掌握Java Swing文本 ...

  9. 在浏览器地址栏输入www.baidu.com到打开百度首页这期间到底发生了什么?

    刚才无意间看到这么一个面试题,觉得有点意思,我想从五层网络模型的角度说说我的看法. 1.首先通过DNS域名系统向域名服务器发送域名解析请求来得到百度的IP地址39.156.69.79:2.系统通过AR ...

  10. Python:爬取网站图片并保存至本地

    Python:爬取网页图片并保存至本地 python3爬取网页中的图片到本地的过程如下: 1.爬取网页 2.获取图片地址 3.爬取图片内容并保存到本地 实例:爬取百度贴吧首页图片. 代码如下: imp ...