C. Pearls in a Row
C. Pearls in a Rowtime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputOn 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".
Sample test(s)input5
1 2 3 4 1output1
1 5input5
1 2 3 4 5output-1input7
1 2 1 3 1 2 1output2
1 3
4 7
题意:
一串珍珠,分为若干小段,每段至少存在两个相同种类的珍珠,求最多可以分多少段。
要求输入珍珠总数目n和n个珍珠的种类,输出最多可分的段数,和每段珍珠的起点和终点。
本题我们可用set容器储存珍珠串,每当输入珍珠与前串含有种类相同时,则记录首尾两点,总段数+1。
附AC代码:
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std; const int MAX=; struct interval{//定义good区间
int l,r;
}a[MAX]; int n;
set<long long> q;//定义set容器 int main(){
while(~scanf("%d",&n)){
q.clear();//每次清空
long long t;
int sum=;
int left=;
for(int i=;i<=n;i++){
scanf("%d",&t);
if(q.count(t)){//count返回set内该元素个数,当含有时则标记区间,清空容器;没有则插入元素
a[sum].l=left;
a[sum].r=i;
sum++;
left=i+;
q.clear();
}
else
q.insert(t);
}
if(a[sum-].r<n)//注意最后一个区间的处理
a[sum-].r=n;
if(sum>){
printf("%d\n",sum);
for(int i=;i<sum;i++){
printf("%d %d\n",a[i].l,a[i].r);
}
}
else
printf("-1\n");
}
return ;
}
C. Pearls in a Row的更多相关文章
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- 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 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 ...
- 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 ...
- 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 ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- 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 ...
- CodeForces 620C Pearls in a Row
水题,每当出现重复就分割开来,最后留下的尾巴给最后一段 #include<cstdio> #include<cstring> #include<cmath> #in ...
随机推荐
- 实例讲解SVN分支和合并问题(转)
本节向大家简单描述一下SVN分支和合并方面的知识,在学习SVN的过程中SVN分支和合并时经常遇到的问题,在这里和大家分享一下,希望本文对大家有用. 关于主线同SVN分支合并的概念及如何使用的误区此问题 ...
- Django-extra的用法
## select提供简单数据 # SELECT age, (age > 18) as is_adult FROM myapp_person; Person.objects.all().extr ...
- caffe2--ubuntu16.04--14.04--install
Install Welcome to Caffe2! Get started with deep learning today by following the step by step guide ...
- Nginx详细的安装教程(linux)
转:https://blog.csdn.net/u013641234/article/details/73838472 Nginx作为一个web服务器,目前使用最多的就利用其负载均衡,本篇着重讲解的是 ...
- 剑指Offer面试题16(Java版):反转链表
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点. 解决与链表相关的问题总是有大量的指针操作.而指针操作的代码总是easy出错的. 非常多的面试官喜欢出链表相关的问题,就是 ...
- React Native学习(二)之View
React Native组件解析(二)之View 0.JSX React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能. ...
- Detours3.0 文档翻译
http://blog.csdn.net/buck84/article/details/8289991 拦截二进制函数 Detours库能够在执行过程中动态拦截函数调用.detours将目标函数前几个 ...
- Hadoop安全
kerberos-hadoop配置常见问题汇总 注意事项 常见问题如下(其中前面两点最多): 各目录属主组属性修改. 对于hadoop,需要改为yarn:hadoop/mapred:hdoop/hdf ...
- iOS 工程中 Other Linker Flags
对于64位机子和iPhone OS应用 解决方法是使用-all_load 或者 -force_load. -all_load强迫链接器从它能看见的所有文档中加载所有的对象文件,甚至那些没有OC代码的文 ...
- Tomcat之catalina.out日志分割
可参考:http://meiling.blog.51cto.com/6220221/1911769 本人尚未验证.