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\)。

完整代码如下:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<set>
  4. using namespace std;
  5. const int maxn=3*1e5+10;
  6. int n,last,cnt;
  7. int a[maxn];
  8. struct node
  9. {
  10. int l,r;
  11. }ans[maxn];
  12. set<int> s;
  13. set<int>::iterator it;
  14. int main()
  15. {
  16. scanf("%d",&n);
  17. for(int i=1;i<=n;i++)
  18. scanf("%d",&a[i]);
  19. last=1;
  20. for(int i=1;i<=n;i++)
  21. {
  22. if((it=s.find(a[i]))!=s.end())
  23. {
  24. ans[++cnt].l=last,ans[cnt].r=i;
  25. s.clear();
  26. last=i+1;
  27. }
  28. else
  29. s.insert(a[i]);
  30. }
  31. if(!cnt)
  32. {
  33. printf("-1");
  34. return 0;
  35. }
  36. ans[cnt].r=n;
  37. printf("%d\n",cnt);
  38. for(int i=1;i<=cnt;i++)
  39. printf("%d %d\n",ans[i].l,ans[i].r);
  40. return 0;
  41. }

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. 「SAP技术」A项目关联公司间退货STO流程

    [SAP技术]A项目关联公司间退货STO流程 1)创建公司间退货STO单据. 如下图示的公司间退货STO 4500000572, 2),VL10B, 创建交货单. 如下图交货单号:80044918, ...

  2. Windows下RabbitMQ 的下载、配置、Java实现生产者和消费者例子

    RabbitMQ是一个轻量级的消息代理中间件,支持多种消息通信协议,支持分布式部署,支持运行于多个操作系统,具有灵活.高可用等特性.RabbitMQ支持多种协议,其中最为重要的是高级消息队列协议(AM ...

  3. Flutter速记

    一.安装 参考https://www.jianshu.com/p/cda416e2fc0d         二.换图标   三.打包 参考:https://www.cnblogs.com/shuang ...

  4. Eclipse中如何添加相对路径的外部jar包

    在eclipse中进行java编程的时候,常常需要引用外部jar包.而采用相对路径引用jar包可以大大方便java工程的拷贝,这样使得java工程从一个路径转移到另一个路径时不用大费周章的修改外包ja ...

  5. 洛谷 UVA10226 Hardwood Species

    洛谷 UVA10226 Hardwood Species 洛谷评测传送门 题目描述 PDF 输入格式 输出格式 输入输出样例 输入 #1复制 输出 #1复制 题目翻译: 给定若干字符串,输出格式为:( ...

  6. table+分页+模糊查询

    这个分页超级棒嘞. 网页链接:http://www.cssmoban.com/cssthemes/7528.shtml

  7. HTTP常见响应状态码及解释、常用请求头及解释

    1.HTTP常见响应状态码及解释2XX Success(成功状态码) 200 表示从客户端发来的请求在服务器端被正常处理204 该状态码表示服务器接收的请求已成功处理,但在返回的响应报文中不含实体的主 ...

  8. Python 变量与运算符

    变量 基本概念: 1. 变量,名字,数据的唯一标识2.变量命名: 字母.数字.下划线: 不能以数字开头: 区分大小写: 不能使用保留字和关键字: 命名要有意义:(多个单词时,推荐使用下划线连接) 3. ...

  9. Dicom文件支持中文字符

    Dicom文件的默认字符集编码为ISO-IR6,这种字符集是不支持中文的,当使用Dicom工具修改病人姓名后,名字会成乱码而无法正常显示,如下图: 知道了原因就知道解决办法了,修改Dicom的字符集( ...

  10. Java描述设计模式(13):迭代器模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.迭代器模式 1.基础概念 迭代器模式又叫游标模式,是对象的行为模式.迭代器模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象. 2 ...